MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestParseHTTPRangeHeader

Function TestParseHTTPRangeHeader

rest/handler_test.go:24–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22)
23
24func TestParseHTTPRangeHeader(t *testing.T) {
25 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
26 type testcase struct {
27 header string
28 contentLength uint64
29 status int
30 start uint64
31 end uint64
32 }
33 testcases := []testcase{
34 // No Range: header at all:
35 {"", 100, http.StatusOK, 0, 0},
36
37 // Syntactically invalid Range headers are ignored:
38 {"lolwut", 100, http.StatusOK, 0, 0},
39 {"inches=-", 100, http.StatusOK, 0, 0},
40 {"bytes=-", 100, http.StatusOK, 0, 0},
41 {"bytes=50-bar", 100, http.StatusOK, 0, 0},
42 {"bytes=50-49", 100, http.StatusOK, 0, 0}, // invalid, not unsatisfiable
43 {"bytes=99999999999999999999-1", 100, http.StatusOK, 0, 0}, // again, invalid
44
45 // These requests return the entire document:
46 {"bytes=0-", 100, http.StatusOK, 0, 0},
47 {"bytes=0-99", 100, http.StatusOK, 0, 0},
48 {"bytes=-100", 100, http.StatusOK, 0, 0},
49 {"bytes=-99999999999999999999", 100, http.StatusOK, 0, 0},
50
51 // Not satisfiable:
52 {"bytes=100-", 100, http.StatusRequestedRangeNotSatisfiable, 0, 0},
53 {"bytes=100-200", 100, http.StatusRequestedRangeNotSatisfiable, 0, 0},
54 {"bytes=100-99999999999999999999", 100, http.StatusRequestedRangeNotSatisfiable, 0, 0},
55 {"bytes=-0", 100, http.StatusRequestedRangeNotSatisfiable, 0, 0},
56
57 {"bytes=10-", 100, http.StatusPartialContent, 10, 99},
58 {"bytes=-10", 100, http.StatusPartialContent, 90, 99},
59 {"bytes=0-0", 100, http.StatusPartialContent, 0, 0},
60 {"bytes=50-60", 100, http.StatusPartialContent, 50, 60},
61 {"bytes=99-", 100, http.StatusPartialContent, 99, 99},
62 {"bytes=99-200", 100, http.StatusPartialContent, 99, 99},
63 {"bytes=90-200", 100, http.StatusPartialContent, 90, 99},
64 {"bytes=90-99999999999999999999", 100, http.StatusPartialContent, 90, 99},
65 {"bytes=2-98", 100, http.StatusPartialContent, 2, 98},
66
67 // Test with empty content:
68 {"bytes=-1", 0, http.StatusOK, 0, 0},
69 {"bytes=-10", 0, http.StatusOK, 0, 0},
70 {"bytes=0-0", 0, http.StatusRequestedRangeNotSatisfiable, 0, 0},
71 {"bytes=0-49", 0, http.StatusRequestedRangeNotSatisfiable, 0, 0},
72 {"bytes=1-1", 0, http.StatusRequestedRangeNotSatisfiable, 0, 0},
73 {"bytes=-0", 0, http.StatusRequestedRangeNotSatisfiable, 0, 0},
74 }
75
76 for _, expected := range testcases {
77 status, start, end := parseHTTPRangeHeader(expected.header, expected.contentLength)
78 t.Logf("*** Range: %s --> %d %d-%d", expected.header, status, start, end)
79 assert.Equal(t, expected.status, status)
80 if status == http.StatusPartialContent {
81 assert.Equal(t, expected.start, start)

Callers

nothing calls this directly

Calls 3

parseHTTPRangeHeaderFunction · 0.85
LogfMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected