19597: Parse multipart/form-data request body.
[arvados.git] / lib / controller / router / request_test.go
index 89238f656345aa2d7ccf19b4889ead4955f17034..82f1fb8e89df9e8085b0a0e90a8a467365c0caa0 100644 (file)
@@ -8,11 +8,12 @@ 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/arvadostest"
        check "gopkg.in/check.v1"
 )
 
@@ -49,10 +50,26 @@ func (tr *testReq) Request() *http.Request {
        } else if tr.json {
                if tr.jsonAttrsTop {
                        for k, v := range tr.attrs {
-                               param[k] = v
+                               if tr.jsonStringParam {
+                                       j, err := json.Marshal(v)
+                                       if err != nil {
+                                               panic(err)
+                                       }
+                                       param[k] = string(j)
+                               } else {
+                                       param[k] = v
+                               }
                        }
                } else if tr.attrs != nil {
-                       param[tr.attrsKey] = tr.attrs
+                       if tr.jsonStringParam {
+                               j, err := json.Marshal(tr.attrs)
+                               if err != nil {
+                                       panic(err)
+                               }
+                               param[tr.attrsKey] = string(j)
+                       } else {
+                               param[tr.attrsKey] = tr.attrs
+                       }
                }
                tr.body = bytes.NewBuffer(nil)
                err := json.NewEncoder(tr.body).Encode(param)
@@ -100,7 +117,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 {
@@ -115,9 +132,18 @@ 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()