MCPcopy Index your code
hub / github.com/livebud/bud / TestRequestMap

Function TestRequestMap

framework/controller/controller_test.go:2045–2096  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2043}
2044
2045func 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
2098func TestComplexInput(t *testing.T) {
2099 is := is.New(t)

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NoErrMethod · 0.80
GetJSONMethod · 0.80
DiffMethod · 0.80
PostJSONMethod · 0.80
CloseMethod · 0.65
WriteMethod · 0.45
StartMethod · 0.45

Tested by

no test coverage detected