| 2043 | } |
| 2044 | |
| 2045 | func TestRequestMap(t *testing.T) { |
| 2046 | is := is.New(t) |
| 2047 | ctx := context.Background() |
| 2048 | dir := t.TempDir() |
| 2049 | td := testdir.New(dir) |
| 2050 | td.Files["controller/posts/comments/controller.go"] = ` |
| 2051 | package comments |
| 2052 | type Controller struct {} |
| 2053 | type Input struct { |
| 2054 | PostID int ` + "`" + `json:"post_id"` + "`" + ` |
| 2055 | Order string ` + "`" + `json:"order"` + "`" + ` |
| 2056 | Author *string ` + "`" + `json:"author"` + "`" + ` |
| 2057 | } |
| 2058 | func (c *Controller) Index(in *Input) *Input { |
| 2059 | return in |
| 2060 | } |
| 2061 | func (c *Controller) Create(in *Input) *Input { |
| 2062 | return in |
| 2063 | } |
| 2064 | ` |
| 2065 | is.NoErr(td.Write(ctx)) |
| 2066 | cli := testcli.New(dir) |
| 2067 | app, err := cli.Start(ctx, "run") |
| 2068 | is.NoErr(err) |
| 2069 | defer app.Close() |
| 2070 | res, err := app.GetJSON("/posts/10/comments?order=asc&author=Alice") |
| 2071 | is.NoErr(err) |
| 2072 | is.NoErr(res.Diff(` |
| 2073 | HTTP/1.1 200 OK |
| 2074 | Content-Type: application/json |
| 2075 | |
| 2076 | {"post_id":10,"order":"asc","author":"Alice"} |
| 2077 | `)) |
| 2078 | res, err = app.PostJSON("/posts/10/comments?order=asc", bytes.NewBufferString(`{"author":"Alice"}`)) |
| 2079 | is.NoErr(err) |
| 2080 | is.NoErr(res.Diff(` |
| 2081 | HTTP/1.1 200 OK |
| 2082 | Content-Type: application/json |
| 2083 | |
| 2084 | {"post_id":10,"order":"asc","author":"Alice"} |
| 2085 | `)) |
| 2086 | // Test optional |
| 2087 | res, err = app.PostJSON("/posts/10/comments?order=asc", nil) |
| 2088 | is.NoErr(err) |
| 2089 | is.NoErr(res.Diff(` |
| 2090 | HTTP/1.1 200 OK |
| 2091 | Content-Type: application/json |
| 2092 | |
| 2093 | {"post_id":10,"order":"asc","author":null} |
| 2094 | `)) |
| 2095 | is.NoErr(app.Close()) |
| 2096 | } |
| 2097 | |
| 2098 | func TestComplexInput(t *testing.T) { |
| 2099 | is := is.New(t) |