X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4c5c224df0cc4c3aca178499dcc3cd580dbf7298..e470737688e80651a4d0c11fa0caef2b63ad4fd0:/lib/controller/router/request_test.go?ds=sidebyside diff --git a/lib/controller/router/request_test.go b/lib/controller/router/request_test.go index 02cc9ce3f7..118415cb40 100644 --- a/lib/controller/router/request_test.go +++ b/lib/controller/router/request_test.go @@ -49,10 +49,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) @@ -118,6 +134,8 @@ func (s *RouterSuite) TestAttrsInBody(c *check.C) { 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}, } { c.Logf("tr: %#v", tr) req := tr.Request() @@ -166,3 +184,41 @@ func (s *RouterSuite) TestBoolParam(c *check.C) { c.Check(params[testKey], check.Equals, true) } } + +func (s *RouterSuite) TestOrderParam(c *check.C) { + for i, tr := range []testReq{ + {method: "POST", param: map[string]interface{}{"order": ""}, json: true}, + {method: "POST", param: map[string]interface{}{"order": ""}, json: false}, + {method: "POST", param: map[string]interface{}{"order": []string{}}, json: true}, + {method: "POST", param: map[string]interface{}{"order": []string{}}, json: false}, + {method: "POST", param: map[string]interface{}{}, json: true}, + {method: "POST", param: map[string]interface{}{}, json: false}, + } { + c.Logf("#%d, tr: %#v", i, tr) + req := tr.Request() + params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + c.Assert(err, check.IsNil) + c.Assert(params, check.NotNil) + if order, ok := params["order"]; ok && order != nil { + c.Check(order, check.DeepEquals, []interface{}{}) + } + } + + for i, tr := range []testReq{ + {method: "POST", param: map[string]interface{}{"order": "foo,bar desc"}, json: true}, + {method: "POST", param: map[string]interface{}{"order": "foo,bar desc"}, json: false}, + {method: "POST", param: map[string]interface{}{"order": "[\"foo\", \"bar desc\"]"}, json: false}, + {method: "POST", param: map[string]interface{}{"order": []string{"foo", "bar desc"}}, json: true}, + {method: "POST", param: map[string]interface{}{"order": []string{"foo", "bar desc"}}, json: false}, + } { + c.Logf("#%d, tr: %#v", i, tr) + req := tr.Request() + params, err := s.rtr.loadRequestParams(req, tr.attrsKey) + c.Assert(err, check.IsNil) + if _, ok := params["order"].([]string); ok { + c.Check(params["order"], check.DeepEquals, []string{"foo", "bar desc"}) + } else { + c.Check(params["order"], check.DeepEquals, []interface{}{"foo", "bar desc"}) + } + } +}