Merge branch '21031-test-provision-fix'. Refs #21031
[arvados.git] / lib / controller / router / request_test.go
index 118415cb40a2211c702ecf5fbfe0b0256da542b4..b689eb681f8bf17ce0ff4581028fdc3570730e23 100644 (file)
@@ -8,11 +8,13 @@ import (
        "bytes"
        "encoding/json"
        "io"
+       "mime/multipart"
        "net/http"
        "net/http/httptest"
        "net/url"
 
-       "git.curoverse.com/arvados.git/sdk/go/arvadostest"
+       "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 {