diff options
| author | Shulhan <ms@kilabit.info> | 2022-03-30 01:49:14 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-03-30 01:49:14 +0700 |
| commit | 8a8de54d66fd14d1fbdb7e8598c597f871230980 (patch) | |
| tree | 1104e23c761563502a2f05be2ef1cda3e0fe12f6 /lib/http/server_test.go | |
| parent | 0c288a5880fbc0fceb792dcfd0747d95163a90dc (diff) | |
| download | pakakeh.go-8a8de54d66fd14d1fbdb7e8598c597f871230980.tar.xz | |
lib/http: implement handler to authorized request to Server Memfs
The FSAuthHandler in the ServerOptions define a function that will
be called to each GET request to Server Memfs instance using the value
from the HTTP Request instance.
If the request is not authorized it must return false and the HTTP
response will be set to 401 Unauthorized with empty body.
Diffstat (limited to 'lib/http/server_test.go')
| -rw-r--r-- | lib/http/server_test.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/http/server_test.go b/lib/http/server_test.go index 7364469d..a63bdb92 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -787,3 +787,76 @@ func TestStatusError(t *testing.T) { test.Assert(t, "Body", c.expBody, string(body)) } } + +// TestServer_HandleFS_Auth test GET on memfs with authorization. +func TestServer_HandleFS_Auth(t *testing.T) { + type testCase struct { + cookieSid *http.Cookie + desc string + reqPath string + expStatusCode int + } + + var ( + c testCase + req *http.Request + res *http.Response + err error + ) + + cases := []testCase{{ + desc: "With public path", + reqPath: "/index.html", + expStatusCode: http.StatusOK, + }, { + desc: "With /auth.txt", + reqPath: "/auth.txt", + expStatusCode: http.StatusOK, + }, { + desc: "With /auth path no cookie", + reqPath: "/auth", + expStatusCode: http.StatusUnauthorized, + }, { + desc: "With /auth path and cookie", + reqPath: "/auth", + cookieSid: &http.Cookie{ + Name: "sid", + Value: "authz", + }, + expStatusCode: http.StatusOK, + }, { + desc: "With invalid /auth path and cookie", + reqPath: "/auth/notexist", + cookieSid: &http.Cookie{ + Name: "sid", + Value: "authz", + }, + expStatusCode: http.StatusNotFound, + }, { + desc: "With /auth/sub path and cookie", + reqPath: "/auth/sub", + cookieSid: &http.Cookie{ + Name: "sid", + Value: "authz", + }, + expStatusCode: http.StatusOK, + }} + + for _, c = range cases { + req, err = http.NewRequest(http.MethodGet, testServerUrl+c.reqPath, nil) + if err != nil { + t.Fatalf("%s: %s", c.desc, err) + } + + if c.cookieSid != nil { + req.AddCookie(c.cookieSid) + } + + res, err = client.Do(req) + if err != nil { + t.Fatalf("%s: %s", c.desc, err) + } + + test.Assert(t, c.desc, c.expStatusCode, res.StatusCode) + } +} |
