10990: Add tests for byte range requests that start at byte >0.
[arvados.git] / services / keep-web / ranges_test.go
1 package main
2
3 import (
4         "fmt"
5         "net/http"
6         "net/http/httptest"
7
8         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
9         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
10         "git.curoverse.com/arvados.git/sdk/go/keepclient"
11         check "gopkg.in/check.v1"
12 )
13
14 func (s *IntegrationSuite) TestRanges(c *check.C) {
15         blocksize := 1000000
16         var uuid string
17         {
18                 testdata := make([]byte, blocksize)
19                 for i := 0; i < blocksize; i++ {
20                         testdata[i] = byte(' ')
21                 }
22                 copy(testdata[:3], []byte("foo"))
23                 arv, err := arvadosclient.MakeArvadosClient()
24                 c.Assert(err, check.Equals, nil)
25                 arv.ApiToken = arvadostest.ActiveToken
26                 kc, err := keepclient.MakeKeepClient(arv)
27                 c.Assert(err, check.Equals, nil)
28                 loc, _, err := kc.PutB(testdata[:])
29                 c.Assert(err, check.Equals, nil)
30
31                 mtext := "."
32                 for i := 0; i < 4; i++ {
33                         mtext = mtext + " " + loc
34                 }
35                 mtext = mtext + fmt.Sprintf(" 0:%d:testdata.bin\n", blocksize*4)
36                 coll := map[string]interface{}{}
37                 err = arv.Create("collections",
38                         map[string]interface{}{
39                                 "collection": map[string]interface{}{
40                                         "name":          "test data for keep-web TestRanges",
41                                         "manifest_text": mtext,
42                                 },
43                         }, &coll)
44                 c.Assert(err, check.Equals, nil)
45                 uuid = coll["uuid"].(string)
46                 defer arv.Delete("collections", uuid, nil, nil)
47         }
48
49         url := mustParseURL("http://" + uuid + ".collections.example.com/testdata.bin")
50         for _, trial := range []struct {
51                 header     string
52                 expectObey bool
53                 expectBody string
54         }{
55                 {"0-2", true, "foo"},
56                 {"1-4", true, "oo  "},
57                 {"z-y", false, ""},
58                 {"1000000-1000003", true, "foo "},
59                 {"999999-1000003", true, " foo "},
60                 {"2000000-2000003", true, "foo "},
61                 {"1999999-2000002", true, " foo"},
62                 {"3999998-3999999", true, "  "},
63                 {"3999998-4000004", true, "  "},
64                 {"3999998-", true, "  "},
65         } {
66                 c.Logf("%+v", trial)
67                 resp := httptest.NewRecorder()
68                 req := &http.Request{
69                         Method:     "GET",
70                         URL:        url,
71                         Host:       url.Host,
72                         RequestURI: url.RequestURI(),
73                         Header: http.Header{
74                                 "Authorization": {"OAuth2 " + arvadostest.ActiveToken},
75                                 "Range":         {"bytes=" + trial.header},
76                         },
77                 }
78                 s.testServer.Handler.ServeHTTP(resp, req)
79                 if trial.expectObey {
80                         c.Check(resp.Code, check.Equals, http.StatusPartialContent)
81                         c.Check(resp.Body.Len(), check.Equals, len(trial.expectBody))
82                         c.Check(resp.Body.String()[:len(trial.expectBody)], check.Equals, trial.expectBody)
83                 } else {
84                         c.Check(resp.Code, check.Equals, http.StatusOK)
85                         c.Check(resp.Body.Len(), check.Equals, blocksize*4)
86                 }
87         }
88 }