diff options
| author | Shulhan <ms@kilabit.info> | 2022-03-31 01:34:37 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-03-31 01:34:37 +0700 |
| commit | d8057042e6c707e28a57508b6bfad0a6c4f3bd01 (patch) | |
| tree | 9dddfc19febf0bfe32b454a15aade88cae6b430d /lib/http/server_test.go | |
| parent | 8a8de54d66fd14d1fbdb7e8598c597f871230980 (diff) | |
| download | pakakeh.go-d8057042e6c707e28a57508b6bfad0a6c4f3bd01.tar.xz | |
lib/http: rewrite ServerOptions FSHandler to handle request in general
This changes rename ServerOptions field FSAuthHandler to FSHandler and
rewrite their usage.
The FSHandler define the function to inspect each GET request to Server
MemFS instance.
The node parameter contains the requested file inside the memfs.
If the handler return true, server will continue processing the node
(writing the Node content type, body, and so on).
If the handler return false, server stop processing the node and return
immediately, which means the function should have already handle writing
the header, status code, and/or body.
Diffstat (limited to 'lib/http/server_test.go')
| -rw-r--r-- | lib/http/server_test.go | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/lib/http/server_test.go b/lib/http/server_test.go index a63bdb92..d613d2a8 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -788,12 +788,13 @@ func TestStatusError(t *testing.T) { } } -// TestServer_HandleFS_Auth test GET on memfs with authorization. -func TestServer_HandleFS_Auth(t *testing.T) { +// TestServer_Options_HandleFS test GET on memfs with authorization. +func TestServer_Options_HandleFS(t *testing.T) { type testCase struct { cookieSid *http.Cookie desc string reqPath string + expResBody string expStatusCode int } @@ -808,14 +809,17 @@ func TestServer_HandleFS_Auth(t *testing.T) { desc: "With public path", reqPath: "/index.html", expStatusCode: http.StatusOK, + expResBody: "<html><body>Hello, world!</body></html>\n", }, { desc: "With /auth.txt", reqPath: "/auth.txt", expStatusCode: http.StatusOK, + expResBody: "Hello, auth.txt!\n", }, { - desc: "With /auth path no cookie", + desc: "With /auth path no cookie, redirected to /", reqPath: "/auth", - expStatusCode: http.StatusUnauthorized, + expStatusCode: http.StatusOK, + expResBody: "<html><body>Hello, world!</body></html>\n", }, { desc: "With /auth path and cookie", reqPath: "/auth", @@ -824,6 +828,7 @@ func TestServer_HandleFS_Auth(t *testing.T) { Value: "authz", }, expStatusCode: http.StatusOK, + expResBody: "<html><body>Hello, authorized world!</body></html>\n", }, { desc: "With invalid /auth path and cookie", reqPath: "/auth/notexist", @@ -840,6 +845,7 @@ func TestServer_HandleFS_Auth(t *testing.T) { Value: "authz", }, expStatusCode: http.StatusOK, + expResBody: "<html><body>Hello, /auth/sub!</body></html>\n", }} for _, c = range cases { @@ -858,5 +864,12 @@ func TestServer_HandleFS_Auth(t *testing.T) { } test.Assert(t, c.desc, c.expStatusCode, res.StatusCode) + + gotBody, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Fatalf("%s: %s", c.desc, err) + } + + test.Assert(t, "response body", c.expResBody, string(gotBody)) } } |
