X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b6d7efab2c4bffa3fabd55b166e44cca8ac1391f..f4fdee016f05ca986c55c7d769ce2f1a6bec5541:/lib/controller/router/request_test.go diff --git a/lib/controller/router/request_test.go b/lib/controller/router/request_test.go index 4544a6bb65..b689eb681f 100644 --- a/lib/controller/router/request_test.go +++ b/lib/controller/router/request_test.go @@ -8,10 +8,12 @@ import ( "bytes" "encoding/json" "io" + "mime/multipart" "net/http" "net/http/httptest" "net/url" + "git.arvados.org/arvados.git/sdk/go/arvados" "git.arvados.org/arvados.git/sdk/go/arvadostest" check "gopkg.in/check.v1" ) @@ -116,7 +118,7 @@ func (tr *testReq) Request() *http.Request { } if tr.json { req.Header.Set("Content-Type", "application/json") - } else { + } else if tr.header.Get("Content-Type") == "" { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") } for k, v := range tr.header { @@ -131,20 +133,30 @@ func (tr *testReq) bodyContent() string { func (s *RouterSuite) TestAttrsInBody(c *check.C) { attrs := map[string]interface{}{"foo": "bar"} + + multipartBody := new(bytes.Buffer) + multipartWriter := multipart.NewWriter(multipartBody) + multipartWriter.WriteField("attrs", `{"foo":"bar"}`) + multipartWriter.Close() + for _, tr := range []testReq{ {attrsKey: "model_name", json: true, attrs: attrs}, {attrsKey: "model_name", json: true, attrs: attrs, jsonAttrsTop: true}, {attrsKey: "model_name", json: true, attrs: attrs, jsonAttrsTop: true, jsonStringParam: true}, {attrsKey: "model_name", json: true, attrs: attrs, jsonAttrsTop: false, jsonStringParam: true}, + {body: multipartBody, header: http.Header{"Content-Type": []string{multipartWriter.FormDataContentType()}}}, } { c.Logf("tr: %#v", tr) req := tr.Request() - params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + var opts struct{ Attrs struct{ Foo string } } + params, err := s.rtr.loadRequestParams(req, tr.attrsKey, &opts) c.Logf("params: %#v", params) c.Assert(err, check.IsNil) c.Check(params, check.NotNil) - c.Assert(params["attrs"], check.FitsTypeOf, map[string]interface{}{}) - c.Check(params["attrs"].(map[string]interface{})["foo"], check.Equals, "bar") + c.Check(opts.Attrs.Foo, check.Equals, "bar") + if c.Check(params["attrs"], check.FitsTypeOf, map[string]interface{}{}) { + c.Check(params["attrs"].(map[string]interface{})["foo"], check.Equals, "bar") + } } } @@ -161,11 +173,14 @@ func (s *RouterSuite) TestBoolParam(c *check.C) { c.Logf("#%d, tr: %#v", i, tr) req := tr.Request() c.Logf("tr.body: %s", tr.bodyContent()) - params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + var opts struct{ EnsureUniqueName bool } + params, err := s.rtr.loadRequestParams(req, tr.attrsKey, &opts) c.Logf("params: %#v", params) c.Assert(err, check.IsNil) - c.Check(params, check.NotNil) - c.Check(params[testKey], check.Equals, false) + c.Check(opts.EnsureUniqueName, check.Equals, false) + if c.Check(params, check.NotNil) { + c.Check(params[testKey], check.Equals, false) + } } for i, tr := range []testReq{ @@ -177,11 +192,16 @@ func (s *RouterSuite) TestBoolParam(c *check.C) { c.Logf("#%d, tr: %#v", i, tr) req := tr.Request() c.Logf("tr.body: %s", tr.bodyContent()) - params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + var opts struct { + EnsureUniqueName bool `json:"ensure_unique_name"` + } + params, err := s.rtr.loadRequestParams(req, tr.attrsKey, &opts) c.Logf("params: %#v", params) c.Assert(err, check.IsNil) - c.Check(params, check.NotNil) - c.Check(params[testKey], check.Equals, true) + c.Check(opts.EnsureUniqueName, check.Equals, true) + if c.Check(params, check.NotNil) { + c.Check(params[testKey], check.Equals, true) + } } } @@ -196,7 +216,7 @@ func (s *RouterSuite) TestOrderParam(c *check.C) { } { c.Logf("#%d, tr: %#v", i, tr) req := tr.Request() - params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + params, err := s.rtr.loadRequestParams(req, tr.attrsKey, nil) c.Assert(err, check.IsNil) c.Assert(params, check.NotNil) if order, ok := params["order"]; ok && order != nil { @@ -213,8 +233,10 @@ func (s *RouterSuite) TestOrderParam(c *check.C) { } { c.Logf("#%d, tr: %#v", i, tr) req := tr.Request() - params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + var opts arvados.ListOptions + params, err := s.rtr.loadRequestParams(req, tr.attrsKey, &opts) c.Assert(err, check.IsNil) + c.Check(opts.Order, check.DeepEquals, []string{"foo", "bar desc"}) if _, ok := params["order"].([]string); ok { c.Check(params["order"], check.DeepEquals, []string{"foo", "bar desc"}) } else {