diff options
| author | Shulhan <ms@kilabit.info> | 2023-03-11 15:51:03 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-03-12 11:35:19 +0700 |
| commit | 3c2f1c8044776eb68d9a1e2992eb37157b1018a6 (patch) | |
| tree | d44ee3cc08de58c17529db45e773c6686606c094 /lib/http/server_test.go | |
| parent | 61786b2a6c1a7c9d11ee18cab4a804afdce9ffa5 (diff) | |
| download | pakakeh.go-3c2f1c8044776eb68d9a1e2992eb37157b1018a6.tar.xz | |
lib/http: add support for HTTP Range in Server
For HTTP Server using HandleFS, the Range request is handled
automatically.
For other HTTP server, user can use the HandleRange function.
The HandleRange function handle [HTTP Range] request using "bytes" unit.
The body parameter contains the content of resource being requested that
accept Seek method.
If the Request method is not GET, or no Range in header request it will
return all the body [RFC7233 S-3.1].
The contentType is optional, if its empty, it will detected by
http.ResponseWriter during Write.
[HTTP Range]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
[RFC7233 S-3.1]: https://datatracker.ietf.org/doc/html/rfc7233#section-3.1
# Conflicts:
# CHANGELOG.adoc
Diffstat (limited to 'lib/http/server_test.go')
| -rw-r--r-- | lib/http/server_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/http/server_test.go b/lib/http/server_test.go index 5117391b..7bde8303 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -898,3 +898,61 @@ func TestServer_Options_HandleFS(t *testing.T) { test.Assert(t, "response body", c.expResBody, string(gotBody)) } } + +func TestServer_handleRange(t *testing.T) { + var ( + clOpts = &ClientOptions{ + ServerUrl: testServerUrl, + } + cl = NewClient(clOpts) + skipHeaders = []string{HeaderDate, HeaderETag} + + listTestData []*test.Data + tdata *test.Data + httpRes *http.Response + resBody []byte + err error + ) + + listTestData, err = test.LoadDataDir(`testdata/server/range/`) + if err != nil { + t.Fatal(err) + } + + for _, tdata = range listTestData { + t.Log(tdata.Name) + + var ( + header = http.Header{} + headerRange = tdata.Input[`header_range`] + ) + + header.Set(HeaderRange, string(headerRange)) + + httpRes, resBody, err = cl.Get(`/index.html`, header, nil) + if err != nil { + t.Fatal(err) + } + + var ( + tag = `http_headers` + exp = tdata.Output[tag] + got = dumpHttpResponse(httpRes, skipHeaders) + ) + test.Assert(t, tag, string(exp), got) + + tag = `http_body` + exp = tdata.Output[tag] + + // Replace the response body CRLF with LF. + resBody = bytes.ReplaceAll(resBody, []byte("\r\n"), []byte("\n")) + + test.Assert(t, tag, string(exp), string(resBody)) + + tag = `all_body` + exp = tdata.Output[tag] + got = dumpMultipartBody(httpRes) + + test.Assert(t, tag, string(exp), got) + } +} |
