18947: Merge branch 'main'
[arvados.git] / lib / controller / router / router_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package router
6
7 import (
8         "bytes"
9         "encoding/json"
10         "io"
11         "net/http"
12         "net/http/httptest"
13         "net/url"
14         "os"
15         "strings"
16         "testing"
17         "time"
18
19         "git.arvados.org/arvados.git/lib/controller/rpc"
20         "git.arvados.org/arvados.git/sdk/go/arvados"
21         "git.arvados.org/arvados.git/sdk/go/arvadostest"
22         "github.com/gorilla/mux"
23         check "gopkg.in/check.v1"
24 )
25
26 // Gocheck boilerplate
27 func Test(t *testing.T) {
28         check.TestingT(t)
29 }
30
31 var _ = check.Suite(&RouterSuite{})
32
33 type RouterSuite struct {
34         rtr  *router
35         stub arvadostest.APIStub
36 }
37
38 func (s *RouterSuite) SetUpTest(c *check.C) {
39         s.stub = arvadostest.APIStub{}
40         s.rtr = &router{
41                 mux:     mux.NewRouter(),
42                 backend: &s.stub,
43         }
44         s.rtr.addRoutes()
45 }
46
47 func (s *RouterSuite) TestOptions(c *check.C) {
48         token := arvadostest.ActiveToken
49         for _, trial := range []struct {
50                 comment      string // unparsed -- only used to help match test failures to trials
51                 method       string
52                 path         string
53                 header       http.Header
54                 body         string
55                 shouldStatus int // zero value means 200
56                 shouldCall   string
57                 withOptions  interface{}
58         }{
59                 {
60                         method:      "GET",
61                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
62                         shouldCall:  "CollectionGet",
63                         withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection},
64                 },
65                 {
66                         method:      "PUT",
67                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
68                         shouldCall:  "CollectionUpdate",
69                         withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
70                 },
71                 {
72                         method:      "PATCH",
73                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
74                         shouldCall:  "CollectionUpdate",
75                         withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
76                 },
77                 {
78                         method:      "DELETE",
79                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
80                         shouldCall:  "CollectionDelete",
81                         withOptions: arvados.DeleteOptions{UUID: arvadostest.FooCollection},
82                 },
83                 {
84                         method:      "POST",
85                         path:        "/arvados/v1/collections",
86                         shouldCall:  "CollectionCreate",
87                         withOptions: arvados.CreateOptions{},
88                 },
89                 {
90                         method:      "GET",
91                         path:        "/arvados/v1/collections",
92                         shouldCall:  "CollectionList",
93                         withOptions: arvados.ListOptions{Limit: -1},
94                 },
95                 {
96                         method:      "GET",
97                         path:        "/arvados/v1/api_client_authorizations",
98                         shouldCall:  "APIClientAuthorizationList",
99                         withOptions: arvados.ListOptions{Limit: -1},
100                 },
101                 {
102                         method:      "GET",
103                         path:        "/arvados/v1/collections?limit=123&offset=456&include_trash=true&include_old_versions=1",
104                         shouldCall:  "CollectionList",
105                         withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
106                 },
107                 {
108                         method:      "POST",
109                         path:        "/arvados/v1/collections?limit=123&_method=GET",
110                         body:        `{"offset":456,"include_trash":true,"include_old_versions":true}`,
111                         shouldCall:  "CollectionList",
112                         withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
113                 },
114                 {
115                         method:      "POST",
116                         path:        "/arvados/v1/collections?limit=123",
117                         body:        `{"offset":456,"include_trash":true,"include_old_versions":true}`,
118                         header:      http.Header{"X-Http-Method-Override": {"GET"}, "Content-Type": {"application/json"}},
119                         shouldCall:  "CollectionList",
120                         withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
121                 },
122                 {
123                         method:      "POST",
124                         path:        "/arvados/v1/collections?limit=123",
125                         body:        "offset=456&include_trash=true&include_old_versions=1&_method=GET",
126                         header:      http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
127                         shouldCall:  "CollectionList",
128                         withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
129                 },
130                 {
131                         comment:     "form-encoded expression filter in query string",
132                         method:      "GET",
133                         path:        "/arvados/v1/collections?filters=[%22(foo<bar)%22]",
134                         shouldCall:  "CollectionList",
135                         withOptions: arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"(foo<bar)", "=", true}}},
136                 },
137                 {
138                         comment:     "form-encoded expression filter in POST body",
139                         method:      "POST",
140                         path:        "/arvados/v1/collections",
141                         body:        "filters=[\"(foo<bar)\"]&_method=GET",
142                         header:      http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
143                         shouldCall:  "CollectionList",
144                         withOptions: arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"(foo<bar)", "=", true}}},
145                 },
146                 {
147                         comment:     "json-encoded expression filter in POST body",
148                         method:      "POST",
149                         path:        "/arvados/v1/collections?_method=GET",
150                         body:        `{"filters":["(foo<bar)",["bar","=","baz"]],"limit":2}`,
151                         header:      http.Header{"Content-Type": {"application/json"}},
152                         shouldCall:  "CollectionList",
153                         withOptions: arvados.ListOptions{Limit: 2, Filters: []arvados.Filter{{"(foo<bar)", "=", true}, {"bar", "=", "baz"}}},
154                 },
155                 {
156                         comment:     "json-encoded select param in query string",
157                         method:      "GET",
158                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection + "?select=[%22portable_data_hash%22]",
159                         shouldCall:  "CollectionGet",
160                         withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection, Select: []string{"portable_data_hash"}},
161                 },
162                 {
163                         method:       "PATCH",
164                         path:         "/arvados/v1/collections",
165                         shouldStatus: http.StatusMethodNotAllowed,
166                 },
167                 {
168                         method:       "PUT",
169                         path:         "/arvados/v1/collections",
170                         shouldStatus: http.StatusMethodNotAllowed,
171                 },
172                 {
173                         method:       "DELETE",
174                         path:         "/arvados/v1/collections",
175                         shouldStatus: http.StatusMethodNotAllowed,
176                 },
177         } {
178                 // Reset calls captured in previous trial
179                 s.stub = arvadostest.APIStub{}
180
181                 c.Logf("trial: %+v", trial)
182                 comment := check.Commentf("trial comment: %s", trial.comment)
183
184                 _, rr, _ := doRequest(c, s.rtr, token, trial.method, trial.path, trial.header, bytes.NewBufferString(trial.body))
185                 if trial.shouldStatus == 0 {
186                         c.Check(rr.Code, check.Equals, http.StatusOK, comment)
187                 } else {
188                         c.Check(rr.Code, check.Equals, trial.shouldStatus, comment)
189                 }
190                 calls := s.stub.Calls(nil)
191                 if trial.shouldCall == "" {
192                         c.Check(calls, check.HasLen, 0, comment)
193                 } else if len(calls) != 1 {
194                         c.Check(calls, check.HasLen, 1, comment)
195                 } else {
196                         c.Check(calls[0].Method, isMethodNamed, trial.shouldCall, comment)
197                         c.Check(calls[0].Options, check.DeepEquals, trial.withOptions, comment)
198                 }
199         }
200 }
201
202 var _ = check.Suite(&RouterIntegrationSuite{})
203
204 type RouterIntegrationSuite struct {
205         rtr *router
206 }
207
208 func (s *RouterIntegrationSuite) SetUpTest(c *check.C) {
209         cluster := &arvados.Cluster{}
210         cluster.TLS.Insecure = true
211         arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
212         url, _ := url.Parse("https://" + os.Getenv("ARVADOS_TEST_API_HOST"))
213         s.rtr = New(rpc.NewConn("zzzzz", url, true, rpc.PassthroughTokenProvider), Config{})
214 }
215
216 func (s *RouterIntegrationSuite) TearDownSuite(c *check.C) {
217         err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
218         c.Check(err, check.IsNil)
219 }
220
221 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
222         token := arvadostest.ActiveTokenV2
223
224         // Check "get collection" response has "kind" key
225         _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
226         c.Check(rr.Code, check.Equals, http.StatusOK)
227         c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
228         c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
229         c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
230
231         // Check items in list response have a "kind" key regardless
232         // of whether a uuid/pdh is selected.
233         for _, selectj := range []string{
234                 ``,
235                 `,"select":["portable_data_hash"]`,
236                 `,"select":["name"]`,
237                 `,"select":["uuid"]`,
238         } {
239                 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
240                 c.Check(rr.Code, check.Equals, http.StatusOK)
241                 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
242                 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
243                 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
244                 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
245                 c.Check(item0["kind"], check.Equals, "arvados#collection")
246                 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
247                         c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
248                 } else {
249                         c.Check(item0["portable_data_hash"], check.IsNil)
250                 }
251                 if selectj == "" || strings.Contains(selectj, "name") {
252                         c.Check(item0["name"], check.FitsTypeOf, "")
253                 } else {
254                         c.Check(item0["name"], check.IsNil)
255                 }
256                 if selectj == "" || strings.Contains(selectj, "uuid") {
257                         c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
258                 } else {
259                         c.Check(item0["uuid"], check.IsNil)
260                 }
261         }
262
263         // Check "create collection" response has "kind" key
264         _, rr, jresp = doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, bytes.NewBufferString(`ensure_unique_name=true`))
265         c.Check(rr.Code, check.Equals, http.StatusOK)
266         c.Check(jresp["uuid"], check.FitsTypeOf, "")
267         c.Check(jresp["kind"], check.Equals, "arvados#collection")
268 }
269
270 func (s *RouterIntegrationSuite) TestMaxRequestSize(c *check.C) {
271         token := arvadostest.ActiveTokenV2
272         for _, maxRequestSize := range []int{
273                 // Ensure 5M limit is enforced.
274                 5000000,
275                 // Ensure 50M limit is enforced, and that a >25M body
276                 // is accepted even though the default Go request size
277                 // limit is 10M.
278                 50000000,
279         } {
280                 s.rtr.config.MaxRequestSize = maxRequestSize
281                 okstr := "a"
282                 for len(okstr) < maxRequestSize/2 {
283                         okstr = okstr + okstr
284                 }
285
286                 hdr := http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
287
288                 body := bytes.NewBufferString(url.Values{"foo_bar": {okstr}}.Encode())
289                 _, rr, _ := doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, hdr, body)
290                 c.Check(rr.Code, check.Equals, http.StatusOK)
291
292                 body = bytes.NewBufferString(url.Values{"foo_bar": {okstr + okstr}}.Encode())
293                 _, rr, _ = doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, hdr, body)
294                 c.Check(rr.Code, check.Equals, http.StatusRequestEntityTooLarge)
295         }
296 }
297
298 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
299         token := arvadostest.ActiveTokenV2
300
301         _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
302         c.Check(rr.Code, check.Equals, http.StatusOK)
303         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
304         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
305         c.Check(jresp["items"], check.NotNil)
306         c.Check(jresp["items"], check.HasLen, 0)
307
308         _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?filters=[["uuid","in",[]]]`, nil, nil)
309         c.Check(rr.Code, check.Equals, http.StatusOK)
310         c.Check(jresp["items_available"], check.Equals, float64(0))
311         c.Check(jresp["items"], check.NotNil)
312         c.Check(jresp["items"], check.HasLen, 0)
313
314         _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
315         c.Check(rr.Code, check.Equals, http.StatusOK)
316         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
317         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
318         c.Check(jresp["items"], check.HasLen, 2)
319         item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
320         c.Check(item0["uuid"], check.HasLen, 27)
321         c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
322         c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
323         c.Check(item0["mounts"], check.IsNil)
324
325         _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
326         c.Check(rr.Code, check.Equals, http.StatusOK)
327         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
328         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
329         avail := int(jresp["items_available"].(float64))
330         c.Check(jresp["items"], check.HasLen, avail)
331         item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
332         c.Check(item0["uuid"], check.HasLen, 27)
333         c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
334         c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
335         c.Check(item0["mounts"], check.NotNil)
336 }
337
338 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
339         uuid := arvadostest.QueuedContainerUUID
340         token := arvadostest.AdminToken
341         _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
342         c.Check(rr.Code, check.Equals, http.StatusOK)
343         c.Check(jresp["uuid"], check.HasLen, 27)
344         c.Check(jresp["state"], check.Equals, "Locked")
345         _, rr, _ = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
346         c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
347         c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
348         _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
349         c.Check(rr.Code, check.Equals, http.StatusOK)
350         c.Check(jresp["uuid"], check.HasLen, 27)
351         c.Check(jresp["state"], check.Equals, "Queued")
352         c.Check(jresp["environment"], check.IsNil)
353         _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
354         c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
355         c.Check(jresp["uuid"], check.IsNil)
356 }
357
358 func (s *RouterIntegrationSuite) TestWritableBy(c *check.C) {
359         _, rr, jresp := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "GET", `/arvados/v1/users/`+arvadostest.ActiveUserUUID, nil, nil)
360         c.Check(rr.Code, check.Equals, http.StatusOK)
361         c.Check(jresp["writable_by"], check.DeepEquals, []interface{}{"zzzzz-tpzed-000000000000000", "zzzzz-tpzed-xurymjxw79nv3jz", "zzzzz-j7d0g-48foin4vonvc2at"})
362 }
363
364 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
365         uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
366         token := arvadostest.ActiveTokenV2
367
368         _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
369         c.Check(rr.Code, check.Equals, http.StatusOK)
370         c.Check(jresp["uuid"], check.Equals, uuid)
371         expectNS := map[string]int{
372                 "created_at":  596506000, // fixture says 596506247, but truncated by postgresql
373                 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
374         }
375         for key, ns := range expectNS {
376                 mt, ok := jresp[key].(string)
377                 c.Logf("jresp[%q] == %q", key, mt)
378                 c.Assert(ok, check.Equals, true)
379                 t, err := time.Parse(time.RFC3339Nano, mt)
380                 c.Check(err, check.IsNil)
381                 c.Check(t.Nanosecond(), check.Equals, ns)
382         }
383 }
384
385 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
386         uuid := arvadostest.QueuedContainerUUID
387         token := arvadostest.ActiveTokenV2
388         // GET
389         for _, sel := range [][]string{
390                 {"uuid", "command"},
391                 {"uuid", "command", "uuid"},
392         } {
393                 j, err := json.Marshal(sel)
394                 c.Assert(err, check.IsNil)
395                 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
396                 c.Check(rr.Code, check.Equals, http.StatusOK)
397
398                 c.Check(resp["kind"], check.Equals, "arvados#container")
399                 c.Check(resp["uuid"], check.HasLen, 27)
400                 c.Check(resp["command"], check.HasLen, 2)
401                 c.Check(resp["mounts"], check.IsNil)
402                 _, hasMounts := resp["mounts"]
403                 c.Check(hasMounts, check.Equals, false)
404         }
405         // POST & PUT
406         uuid = arvadostest.FooCollection
407         j, err := json.Marshal([]string{"uuid", "description"})
408         c.Assert(err, check.IsNil)
409         for _, method := range []string{"PUT", "POST"} {
410                 desc := "Today is " + time.Now().String()
411                 reqBody := "{\"description\":\"" + desc + "\"}"
412                 var resp map[string]interface{}
413                 var rr *httptest.ResponseRecorder
414                 if method == "PUT" {
415                         _, rr, resp = doRequest(c, s.rtr, token, method, "/arvados/v1/collections/"+uuid+"?select="+string(j), nil, bytes.NewReader([]byte(reqBody)))
416                 } else {
417                         _, rr, resp = doRequest(c, s.rtr, token, method, "/arvados/v1/collections?select="+string(j), nil, bytes.NewReader([]byte(reqBody)))
418                 }
419                 c.Check(rr.Code, check.Equals, http.StatusOK)
420                 c.Check(resp["kind"], check.Equals, "arvados#collection")
421                 c.Check(resp["uuid"], check.HasLen, 27)
422                 c.Check(resp["description"], check.Equals, desc)
423                 c.Check(resp["manifest_text"], check.IsNil)
424         }
425 }
426
427 func (s *RouterIntegrationSuite) TestHEAD(c *check.C) {
428         _, rr, _ := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "HEAD", "/arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
429         c.Check(rr.Code, check.Equals, http.StatusOK)
430 }
431
432 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
433         token := arvadostest.ActiveTokenV2
434         req := (&testReq{
435                 method: "POST",
436                 path:   "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
437                 token:  token,
438         }).Request()
439         rr := httptest.NewRecorder()
440         s.rtr.ServeHTTP(rr, req)
441         c.Check(rr.Code, check.Equals, http.StatusNotFound)
442         c.Logf("body: %q", rr.Body.String())
443         var j map[string]interface{}
444         err := json.Unmarshal(rr.Body.Bytes(), &j)
445         c.Check(err, check.IsNil)
446         c.Logf("decoded: %v", j)
447         c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
448         c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
449 }
450
451 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
452         token := arvadostest.ActiveTokenV2
453         req := (&testReq{
454                 method: "OPTIONS",
455                 path:   "arvados/v1/collections/" + arvadostest.FooCollection,
456                 header: http.Header{"Origin": {"https://example.com"}},
457                 token:  token,
458         }).Request()
459         rr := httptest.NewRecorder()
460         s.rtr.ServeHTTP(rr, req)
461         c.Check(rr.Code, check.Equals, http.StatusOK)
462         c.Check(rr.Body.String(), check.HasLen, 0)
463         c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
464         for _, hdr := range []string{"Authorization", "Content-Type"} {
465                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
466         }
467         for _, method := range []string{"GET", "HEAD", "PUT", "POST", "PATCH", "DELETE"} {
468                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
469         }
470
471         for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
472                 req := (&testReq{
473                         method: "OPTIONS",
474                         path:   unsafe,
475                         header: http.Header{"Origin": {"https://example.com"}},
476                         token:  token,
477                 }).Request()
478                 rr := httptest.NewRecorder()
479                 s.rtr.ServeHTTP(rr, req)
480                 c.Check(rr.Code, check.Equals, http.StatusOK)
481                 c.Check(rr.Body.String(), check.HasLen, 0)
482                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
483                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
484                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
485
486                 req = (&testReq{
487                         method: "POST",
488                         path:   unsafe,
489                         header: http.Header{"Origin": {"https://example.com"}},
490                         token:  token,
491                 }).Request()
492                 rr = httptest.NewRecorder()
493                 s.rtr.ServeHTTP(rr, req)
494                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
495                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
496                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
497         }
498 }
499
500 func doRequest(c *check.C, rtr http.Handler, token, method, path string, hdrs http.Header, body io.Reader) (*http.Request, *httptest.ResponseRecorder, map[string]interface{}) {
501         req := httptest.NewRequest(method, path, body)
502         for k, v := range hdrs {
503                 req.Header[k] = v
504         }
505         req.Header.Set("Authorization", "Bearer "+token)
506         rr := httptest.NewRecorder()
507         rtr.ServeHTTP(rr, req)
508         c.Logf("response body: %s", rr.Body.String())
509         var jresp map[string]interface{}
510         err := json.Unmarshal(rr.Body.Bytes(), &jresp)
511         c.Check(err, check.IsNil)
512         return req, rr, jresp
513 }