Merge branch '4363-less-filename-munging' closes #4363
[arvados.git] / services / keepstore / handler_test.go
1 // Tests for Keep HTTP handlers:
2 //
3 //     GetBlockHandler
4 //     PutBlockHandler
5 //     IndexHandler
6 //
7 // The HTTP handlers are responsible for enforcing permission policy,
8 // so these tests must exercise all possible permission permutations.
9
10 package main
11
12 import (
13         "bytes"
14         "encoding/json"
15         "fmt"
16         "net/http"
17         "net/http/httptest"
18         "os"
19         "regexp"
20         "strings"
21         "testing"
22         "time"
23 )
24
25 // A RequestTester represents the parameters for an HTTP request to
26 // be issued on behalf of a unit test.
27 type RequestTester struct {
28         uri          string
29         api_token    string
30         method       string
31         request_body []byte
32 }
33
34 // Test GetBlockHandler on the following situations:
35 //   - permissions off, unauthenticated request, unsigned locator
36 //   - permissions on, authenticated request, signed locator
37 //   - permissions on, authenticated request, unsigned locator
38 //   - permissions on, unauthenticated request, signed locator
39 //   - permissions on, authenticated request, expired locator
40 //
41 func TestGetHandler(t *testing.T) {
42         defer teardown()
43
44         // Prepare two test Keep volumes. Our block is stored on the second volume.
45         KeepVM = MakeTestVolumeManager(2)
46         defer KeepVM.Quit()
47
48         vols := KeepVM.Volumes()
49         if err := vols[0].Put(TEST_HASH, TEST_BLOCK); err != nil {
50                 t.Error(err)
51         }
52
53         // Create locators for testing.
54         // Turn on permission settings so we can generate signed locators.
55         enforce_permissions = true
56         PermissionSecret = []byte(known_key)
57         permission_ttl = time.Duration(300) * time.Second
58
59         var (
60                 unsigned_locator  = "/" + TEST_HASH
61                 valid_timestamp   = time.Now().Add(permission_ttl)
62                 expired_timestamp = time.Now().Add(-time.Hour)
63                 signed_locator    = "/" + SignLocator(TEST_HASH, known_token, valid_timestamp)
64                 expired_locator   = "/" + SignLocator(TEST_HASH, known_token, expired_timestamp)
65         )
66
67         // -----------------
68         // Test unauthenticated request with permissions off.
69         enforce_permissions = false
70
71         // Unauthenticated request, unsigned locator
72         // => OK
73         response := IssueRequest(
74                 &RequestTester{
75                         method: "GET",
76                         uri:    unsigned_locator,
77                 })
78         ExpectStatusCode(t,
79                 "Unauthenticated request, unsigned locator", http.StatusOK, response)
80         ExpectBody(t,
81                 "Unauthenticated request, unsigned locator",
82                 string(TEST_BLOCK),
83                 response)
84         received_xbs := response.Header().Get("X-Block-Size")
85         expected_xbs := fmt.Sprintf("%d", len(TEST_BLOCK))
86         if received_xbs != expected_xbs {
87                 t.Errorf("expected X-Block-Size %s, got %s", expected_xbs, received_xbs)
88         }
89
90         // ----------------
91         // Permissions: on.
92         enforce_permissions = true
93
94         // Authenticated request, signed locator
95         // => OK
96         response = IssueRequest(&RequestTester{
97                 method:    "GET",
98                 uri:       signed_locator,
99                 api_token: known_token,
100         })
101         ExpectStatusCode(t,
102                 "Authenticated request, signed locator", http.StatusOK, response)
103         ExpectBody(t,
104                 "Authenticated request, signed locator", string(TEST_BLOCK), response)
105         received_xbs = response.Header().Get("X-Block-Size")
106         expected_xbs = fmt.Sprintf("%d", len(TEST_BLOCK))
107         if received_xbs != expected_xbs {
108                 t.Errorf("expected X-Block-Size %s, got %s", expected_xbs, received_xbs)
109         }
110
111         // Authenticated request, unsigned locator
112         // => PermissionError
113         response = IssueRequest(&RequestTester{
114                 method:    "GET",
115                 uri:       unsigned_locator,
116                 api_token: known_token,
117         })
118         ExpectStatusCode(t, "unsigned locator", PermissionError.HTTPCode, response)
119
120         // Unauthenticated request, signed locator
121         // => PermissionError
122         response = IssueRequest(&RequestTester{
123                 method: "GET",
124                 uri:    signed_locator,
125         })
126         ExpectStatusCode(t,
127                 "Unauthenticated request, signed locator",
128                 PermissionError.HTTPCode, response)
129
130         // Authenticated request, expired locator
131         // => ExpiredError
132         response = IssueRequest(&RequestTester{
133                 method:    "GET",
134                 uri:       expired_locator,
135                 api_token: known_token,
136         })
137         ExpectStatusCode(t,
138                 "Authenticated request, expired locator",
139                 ExpiredError.HTTPCode, response)
140 }
141
142 // Test PutBlockHandler on the following situations:
143 //   - no server key
144 //   - with server key, authenticated request, unsigned locator
145 //   - with server key, unauthenticated request, unsigned locator
146 //
147 func TestPutHandler(t *testing.T) {
148         defer teardown()
149
150         // Prepare two test Keep volumes.
151         KeepVM = MakeTestVolumeManager(2)
152         defer KeepVM.Quit()
153
154         // --------------
155         // No server key.
156
157         // Unauthenticated request, no server key
158         // => OK (unsigned response)
159         unsigned_locator := "/" + TEST_HASH
160         response := IssueRequest(
161                 &RequestTester{
162                         method:       "PUT",
163                         uri:          unsigned_locator,
164                         request_body: TEST_BLOCK,
165                 })
166
167         ExpectStatusCode(t,
168                 "Unauthenticated request, no server key", http.StatusOK, response)
169         ExpectBody(t,
170                 "Unauthenticated request, no server key",
171                 TEST_HASH_PUT_RESPONSE, response)
172
173         // ------------------
174         // With a server key.
175
176         PermissionSecret = []byte(known_key)
177         permission_ttl = time.Duration(300) * time.Second
178
179         // When a permission key is available, the locator returned
180         // from an authenticated PUT request will be signed.
181
182         // Authenticated PUT, signed locator
183         // => OK (signed response)
184         response = IssueRequest(
185                 &RequestTester{
186                         method:       "PUT",
187                         uri:          unsigned_locator,
188                         request_body: TEST_BLOCK,
189                         api_token:    known_token,
190                 })
191
192         ExpectStatusCode(t,
193                 "Authenticated PUT, signed locator, with server key",
194                 http.StatusOK, response)
195         response_locator := strings.TrimSpace(response.Body.String())
196         if !VerifySignature(response_locator, known_token) {
197                 t.Errorf("Authenticated PUT, signed locator, with server key:\n"+
198                         "response '%s' does not contain a valid signature",
199                         response_locator)
200         }
201
202         // Unauthenticated PUT, unsigned locator
203         // => OK
204         response = IssueRequest(
205                 &RequestTester{
206                         method:       "PUT",
207                         uri:          unsigned_locator,
208                         request_body: TEST_BLOCK,
209                 })
210
211         ExpectStatusCode(t,
212                 "Unauthenticated PUT, unsigned locator, with server key",
213                 http.StatusOK, response)
214         ExpectBody(t,
215                 "Unauthenticated PUT, unsigned locator, with server key",
216                 TEST_HASH_PUT_RESPONSE, response)
217 }
218
219 // Test /index requests:
220 //   - unauthenticated /index request
221 //   - unauthenticated /index/prefix request
222 //   - authenticated   /index request        | non-superuser
223 //   - authenticated   /index/prefix request | non-superuser
224 //   - authenticated   /index request        | superuser
225 //   - authenticated   /index/prefix request | superuser
226 //
227 // The only /index requests that should succeed are those issued by the
228 // superuser. They should pass regardless of the value of enforce_permissions.
229 //
230 func TestIndexHandler(t *testing.T) {
231         defer teardown()
232
233         // Set up Keep volumes and populate them.
234         // Include multiple blocks on different volumes, and
235         // some metadata files (which should be omitted from index listings)
236         KeepVM = MakeTestVolumeManager(2)
237         defer KeepVM.Quit()
238
239         vols := KeepVM.Volumes()
240         vols[0].Put(TEST_HASH, TEST_BLOCK)
241         vols[1].Put(TEST_HASH_2, TEST_BLOCK_2)
242         vols[0].Put(TEST_HASH+".meta", []byte("metadata"))
243         vols[1].Put(TEST_HASH_2+".meta", []byte("metadata"))
244
245         data_manager_token = "DATA MANAGER TOKEN"
246
247         unauthenticated_req := &RequestTester{
248                 method: "GET",
249                 uri:    "/index",
250         }
251         authenticated_req := &RequestTester{
252                 method:    "GET",
253                 uri:       "/index",
254                 api_token: known_token,
255         }
256         superuser_req := &RequestTester{
257                 method:    "GET",
258                 uri:       "/index",
259                 api_token: data_manager_token,
260         }
261         unauth_prefix_req := &RequestTester{
262                 method: "GET",
263                 uri:    "/index/" + TEST_HASH[0:3],
264         }
265         auth_prefix_req := &RequestTester{
266                 method:    "GET",
267                 uri:       "/index/" + TEST_HASH[0:3],
268                 api_token: known_token,
269         }
270         superuser_prefix_req := &RequestTester{
271                 method:    "GET",
272                 uri:       "/index/" + TEST_HASH[0:3],
273                 api_token: data_manager_token,
274         }
275
276         // -------------------------------------------------------------
277         // Only the superuser should be allowed to issue /index requests.
278
279   // ---------------------------
280   // enforce_permissions enabled
281         // This setting should not affect tests passing.
282   enforce_permissions = true
283
284         // unauthenticated /index request
285         // => UnauthorizedError
286         response := IssueRequest(unauthenticated_req)
287         ExpectStatusCode(t,
288                 "enforce_permissions on, unauthenticated request",
289                 UnauthorizedError.HTTPCode,
290                 response)
291
292         // unauthenticated /index/prefix request
293         // => UnauthorizedError
294         response = IssueRequest(unauth_prefix_req)
295         ExpectStatusCode(t,
296                 "permissions on, unauthenticated /index/prefix request",
297                 UnauthorizedError.HTTPCode,
298                 response)
299
300         // authenticated /index request, non-superuser
301         // => UnauthorizedError
302         response = IssueRequest(authenticated_req)
303         ExpectStatusCode(t,
304                 "permissions on, authenticated request, non-superuser",
305                 UnauthorizedError.HTTPCode,
306                 response)
307
308         // authenticated /index/prefix request, non-superuser
309         // => UnauthorizedError
310         response = IssueRequest(auth_prefix_req)
311         ExpectStatusCode(t,
312                 "permissions on, authenticated /index/prefix request, non-superuser",
313                 UnauthorizedError.HTTPCode,
314                 response)
315
316         // superuser /index request
317         // => OK
318         response = IssueRequest(superuser_req)
319         ExpectStatusCode(t,
320                 "permissions on, superuser request",
321                 http.StatusOK,
322                 response)
323
324         // ----------------------------
325         // enforce_permissions disabled
326         // Valid Request should still pass.
327         enforce_permissions = false
328
329         // superuser /index request
330         // => OK
331         response = IssueRequest(superuser_req)
332         ExpectStatusCode(t,
333                 "permissions on, superuser request",
334                 http.StatusOK,
335                 response)
336
337
338
339         expected := `^` + TEST_HASH + `\+\d+ \d+\n` +
340                 TEST_HASH_2 + `\+\d+ \d+\n$`
341         match, _ := regexp.MatchString(expected, response.Body.String())
342         if !match {
343                 t.Errorf(
344                         "permissions on, superuser request: expected %s, got:\n%s",
345                         expected, response.Body.String())
346         }
347
348         // superuser /index/prefix request
349         // => OK
350         response = IssueRequest(superuser_prefix_req)
351         ExpectStatusCode(t,
352                 "permissions on, superuser request",
353                 http.StatusOK,
354                 response)
355
356         expected = `^` + TEST_HASH + `\+\d+ \d+\n$`
357         match, _ = regexp.MatchString(expected, response.Body.String())
358         if !match {
359                 t.Errorf(
360                         "permissions on, superuser /index/prefix request: expected %s, got:\n%s",
361                         expected, response.Body.String())
362         }
363 }
364
365 // TestDeleteHandler
366 //
367 // Cases tested:
368 //
369 //   With no token and with a non-data-manager token:
370 //   * Delete existing block
371 //     (test for 403 Forbidden, confirm block not deleted)
372 //
373 //   With data manager token:
374 //
375 //   * Delete existing block
376 //     (test for 200 OK, response counts, confirm block deleted)
377 //
378 //   * Delete nonexistent block
379 //     (test for 200 OK, response counts)
380 //
381 //   TODO(twp):
382 //
383 //   * Delete block on read-only and read-write volume
384 //     (test for 200 OK, response with copies_deleted=1,
385 //     copies_failed=1, confirm block deleted only on r/w volume)
386 //
387 //   * Delete block on read-only volume only
388 //     (test for 200 OK, response with copies_deleted=0, copies_failed=1,
389 //     confirm block not deleted)
390 //
391 func TestDeleteHandler(t *testing.T) {
392         defer teardown()
393
394         // Set up Keep volumes and populate them.
395         // Include multiple blocks on different volumes, and
396         // some metadata files (which should be omitted from index listings)
397         KeepVM = MakeTestVolumeManager(2)
398         defer KeepVM.Quit()
399
400         vols := KeepVM.Volumes()
401         vols[0].Put(TEST_HASH, TEST_BLOCK)
402
403         // Explicitly set the permission_ttl to 0 for these
404         // tests, to ensure the MockVolume deletes the blocks
405         // even though they have just been created.
406         permission_ttl = time.Duration(0)
407
408         var user_token = "NOT DATA MANAGER TOKEN"
409         data_manager_token = "DATA MANAGER TOKEN"
410
411         unauth_req := &RequestTester{
412                 method: "DELETE",
413                 uri:    "/" + TEST_HASH,
414         }
415
416         user_req := &RequestTester{
417                 method:    "DELETE",
418                 uri:       "/" + TEST_HASH,
419                 api_token: user_token,
420         }
421
422         superuser_existing_block_req := &RequestTester{
423                 method:    "DELETE",
424                 uri:       "/" + TEST_HASH,
425                 api_token: data_manager_token,
426         }
427
428         superuser_nonexistent_block_req := &RequestTester{
429                 method:    "DELETE",
430                 uri:       "/" + TEST_HASH_2,
431                 api_token: data_manager_token,
432         }
433
434         // Unauthenticated request returns PermissionError.
435         var response *httptest.ResponseRecorder
436         response = IssueRequest(unauth_req)
437         ExpectStatusCode(t,
438                 "unauthenticated request",
439                 PermissionError.HTTPCode,
440                 response)
441
442         // Authenticated non-admin request returns PermissionError.
443         response = IssueRequest(user_req)
444         ExpectStatusCode(t,
445                 "authenticated non-admin request",
446                 PermissionError.HTTPCode,
447                 response)
448
449         // Authenticated admin request for nonexistent block.
450         type deletecounter struct {
451                 Deleted int `json:"copies_deleted"`
452                 Failed  int `json:"copies_failed"`
453         }
454         var response_dc, expected_dc deletecounter
455
456         response = IssueRequest(superuser_nonexistent_block_req)
457         ExpectStatusCode(t,
458                 "data manager request, nonexistent block",
459                 http.StatusNotFound,
460                 response)
461
462         // Authenticated admin request for existing block while never_delete is set.
463         never_delete = true
464         response = IssueRequest(superuser_existing_block_req)
465         ExpectStatusCode(t,
466                 "authenticated request, existing block, method disabled",
467                 MethodDisabledError.HTTPCode,
468                 response)
469         never_delete = false
470
471         // Authenticated admin request for existing block.
472         response = IssueRequest(superuser_existing_block_req)
473         ExpectStatusCode(t,
474                 "data manager request, existing block",
475                 http.StatusOK,
476                 response)
477         // Expect response {"copies_deleted":1,"copies_failed":0}
478         expected_dc = deletecounter{1, 0}
479         json.NewDecoder(response.Body).Decode(&response_dc)
480         if response_dc != expected_dc {
481                 t.Errorf("superuser_existing_block_req\nexpected: %+v\nreceived: %+v",
482                         expected_dc, response_dc)
483         }
484         // Confirm the block has been deleted
485         _, err := vols[0].Get(TEST_HASH)
486         var block_deleted = os.IsNotExist(err)
487         if !block_deleted {
488                 t.Error("superuser_existing_block_req: block not deleted")
489         }
490
491         // A DELETE request on a block newer than permission_ttl should return
492         // success but leave the block on the volume.
493         vols[0].Put(TEST_HASH, TEST_BLOCK)
494         permission_ttl = time.Duration(1) * time.Hour
495
496         response = IssueRequest(superuser_existing_block_req)
497         ExpectStatusCode(t,
498                 "data manager request, existing block",
499                 http.StatusOK,
500                 response)
501         // Expect response {"copies_deleted":1,"copies_failed":0}
502         expected_dc = deletecounter{1, 0}
503         json.NewDecoder(response.Body).Decode(&response_dc)
504         if response_dc != expected_dc {
505                 t.Errorf("superuser_existing_block_req\nexpected: %+v\nreceived: %+v",
506                         expected_dc, response_dc)
507         }
508         // Confirm the block has NOT been deleted.
509         _, err = vols[0].Get(TEST_HASH)
510         if err != nil {
511                 t.Errorf("testing delete on new block: %s\n", err)
512         }
513 }
514
515 // TestPullHandler
516 //
517 // Test handling of the PUT /pull statement.
518 //
519 // Cases tested: syntactically valid and invalid pull lists, from the
520 // data manager and from unprivileged users:
521 //
522 //   1. Valid pull list from an ordinary user
523 //      (expected result: 401 Unauthorized)
524 //
525 //   2. Invalid pull request from an ordinary user
526 //      (expected result: 401 Unauthorized)
527 //
528 //   3. Valid pull request from the data manager
529 //      (expected result: 200 OK with request body "Received 3 pull
530 //      requests"
531 //
532 //   4. Invalid pull request from the data manager
533 //      (expected result: 400 Bad Request)
534 //
535 // Test that in the end, the pull manager received a good pull list with
536 // the expected number of requests.
537 //
538 // TODO(twp): test concurrency: launch 100 goroutines to update the
539 // pull list simultaneously.  Make sure that none of them return 400
540 // Bad Request and that pullq.GetList() returns a valid list.
541 //
542 func TestPullHandler(t *testing.T) {
543         defer teardown()
544
545         var user_token = "USER TOKEN"
546         data_manager_token = "DATA MANAGER TOKEN"
547
548         good_json := []byte(`[
549                 {
550                         "locator":"locator_with_two_servers",
551                         "servers":[
552                                 "server1",
553                                 "server2"
554                         ]
555                 },
556                 {
557                         "locator":"locator_with_no_servers",
558                         "servers":[]
559                 },
560                 {
561                         "locator":"",
562                         "servers":["empty_locator"]
563                 }
564         ]`)
565
566         bad_json := []byte(`{ "key":"I'm a little teapot" }`)
567
568         type pullTest struct {
569                 name          string
570                 req           RequestTester
571                 response_code int
572                 response_body string
573         }
574         var testcases = []pullTest{
575                 {
576                         "Valid pull list from an ordinary user",
577                         RequestTester{"/pull", user_token, "PUT", good_json},
578                         http.StatusUnauthorized,
579                         "Unauthorized\n",
580                 },
581                 {
582                         "Invalid pull request from an ordinary user",
583                         RequestTester{"/pull", user_token, "PUT", bad_json},
584                         http.StatusUnauthorized,
585                         "Unauthorized\n",
586                 },
587                 {
588                         "Valid pull request from the data manager",
589                         RequestTester{"/pull", data_manager_token, "PUT", good_json},
590                         http.StatusOK,
591                         "Received 3 pull requests\n",
592                 },
593                 {
594                         "Invalid pull request from the data manager",
595                         RequestTester{"/pull", data_manager_token, "PUT", bad_json},
596                         http.StatusBadRequest,
597                         "Bad Request\n",
598                 },
599         }
600
601         for _, tst := range testcases {
602                 response := IssueRequest(&tst.req)
603                 ExpectStatusCode(t, tst.name, tst.response_code, response)
604                 ExpectBody(t, tst.name, tst.response_body, response)
605         }
606
607         // The Keep pull manager should have received one good list with 3
608         // requests on it.
609         for i := 0; i < 3; i++ {
610                 item := <-pullq.NextItem
611                 if _, ok := item.(PullRequest); !ok {
612                         t.Errorf("item %v could not be parsed as a PullRequest", item)
613                 }
614         }
615
616         expectChannelEmpty(t, pullq.NextItem)
617 }
618
619 // TestTrashHandler
620 //
621 // Test cases:
622 //
623 // Cases tested: syntactically valid and invalid trash lists, from the
624 // data manager and from unprivileged users:
625 //
626 //   1. Valid trash list from an ordinary user
627 //      (expected result: 401 Unauthorized)
628 //
629 //   2. Invalid trash list from an ordinary user
630 //      (expected result: 401 Unauthorized)
631 //
632 //   3. Valid trash list from the data manager
633 //      (expected result: 200 OK with request body "Received 3 trash
634 //      requests"
635 //
636 //   4. Invalid trash list from the data manager
637 //      (expected result: 400 Bad Request)
638 //
639 // Test that in the end, the trash collector received a good list
640 // trash list with the expected number of requests.
641 //
642 // TODO(twp): test concurrency: launch 100 goroutines to update the
643 // pull list simultaneously.  Make sure that none of them return 400
644 // Bad Request and that replica.Dump() returns a valid list.
645 //
646 func TestTrashHandler(t *testing.T) {
647         defer teardown()
648
649         var user_token = "USER TOKEN"
650         data_manager_token = "DATA MANAGER TOKEN"
651
652         good_json := []byte(`[
653                 {
654                         "locator":"block1",
655                         "block_mtime":1409082153
656                 },
657                 {
658                         "locator":"block2",
659                         "block_mtime":1409082153
660                 },
661                 {
662                         "locator":"block3",
663                         "block_mtime":1409082153
664                 }
665         ]`)
666
667         bad_json := []byte(`I am not a valid JSON string`)
668
669         type trashTest struct {
670                 name          string
671                 req           RequestTester
672                 response_code int
673                 response_body string
674         }
675
676         var testcases = []trashTest{
677                 {
678                         "Valid trash list from an ordinary user",
679                         RequestTester{"/trash", user_token, "PUT", good_json},
680                         http.StatusUnauthorized,
681                         "Unauthorized\n",
682                 },
683                 {
684                         "Invalid trash list from an ordinary user",
685                         RequestTester{"/trash", user_token, "PUT", bad_json},
686                         http.StatusUnauthorized,
687                         "Unauthorized\n",
688                 },
689                 {
690                         "Valid trash list from the data manager",
691                         RequestTester{"/trash", data_manager_token, "PUT", good_json},
692                         http.StatusOK,
693                         "Received 3 trash requests\n",
694                 },
695                 {
696                         "Invalid trash list from the data manager",
697                         RequestTester{"/trash", data_manager_token, "PUT", bad_json},
698                         http.StatusBadRequest,
699                         "Bad Request\n",
700                 },
701         }
702
703         for _, tst := range testcases {
704                 response := IssueRequest(&tst.req)
705                 ExpectStatusCode(t, tst.name, tst.response_code, response)
706                 ExpectBody(t, tst.name, tst.response_body, response)
707         }
708
709         // The trash collector should have received one good list with 3
710         // requests on it.
711         for i := 0; i < 3; i++ {
712                 item := <-trashq.NextItem
713                 if _, ok := item.(TrashRequest); !ok {
714                         t.Errorf("item %v could not be parsed as a TrashRequest", item)
715                 }
716         }
717
718         expectChannelEmpty(t, trashq.NextItem)
719 }
720
721 // ====================
722 // Helper functions
723 // ====================
724
725 // IssueTestRequest executes an HTTP request described by rt, to a
726 // REST router.  It returns the HTTP response to the request.
727 func IssueRequest(rt *RequestTester) *httptest.ResponseRecorder {
728         response := httptest.NewRecorder()
729         body := bytes.NewReader(rt.request_body)
730         req, _ := http.NewRequest(rt.method, rt.uri, body)
731         if rt.api_token != "" {
732                 req.Header.Set("Authorization", "OAuth2 "+rt.api_token)
733         }
734   loggingRouter := MakeLoggingRESTRouter()
735   loggingRouter.ServeHTTP(response, req)
736         return response
737 }
738
739 // ExpectStatusCode checks whether a response has the specified status code,
740 // and reports a test failure if not.
741 func ExpectStatusCode(
742         t *testing.T,
743         testname string,
744         expected_status int,
745         response *httptest.ResponseRecorder) {
746         if response.Code != expected_status {
747                 t.Errorf("%s: expected status %s, got %+v",
748                         testname, expected_status, response)
749         }
750 }
751
752 func ExpectBody(
753         t *testing.T,
754         testname string,
755         expected_body string,
756         response *httptest.ResponseRecorder) {
757         if response.Body.String() != expected_body {
758                 t.Errorf("%s: expected response body '%s', got %+v",
759                         testname, expected_body, response)
760         }
761 }