diff options
| author | Shulhan <ms@kilabit.info> | 2018-12-16 17:26:24 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-12-16 17:30:46 +0700 |
| commit | 9b5ea5a879dd24d7fefaaa95511cde62ab3507fc (patch) | |
| tree | e1b21f6548d6190618aa6ea3fa5092e20b77abd5 /lib/http/server_test.go | |
| parent | 2442b00fa4843ab6887e4726fa5a1ec588065192 (diff) | |
| download | pakakeh.go-9b5ea5a879dd24d7fefaaa95511cde62ab3507fc.tar.xz | |
lib/http: handle HTTP request OPTIONS
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 d737eb3f..6bf0fc87 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -415,3 +415,61 @@ func TestRegisterPut(t *testing.T) { test.Assert(t, "Body", string(c.expBody), string(body), true) } } + +func TestServeHTTPOptions(t *testing.T) { + cb := func(req *http.Request, reqBody []byte) ( + resBody []byte, e error, + ) { + s := fmt.Sprintf("%s\n", req.Form) + s += fmt.Sprintf("%v\n", req.MultipartForm) + s += fmt.Sprintf("%s", reqBody) + return []byte(s), nil + } + + client := &http.Client{} + + testServer.RegisterDelete("/options", ResponseTypePlain, cb) + testServer.RegisterPatch("/options", RequestTypeQuery, + ResponseTypePlain, cb) + + cases := []struct { + desc string + reqURL string + expStatusCode int + expAllow string + }{{ + desc: "With root path", + reqURL: "http://127.0.0.1:8080/", + expStatusCode: http.StatusOK, + expAllow: "GET, HEAD, OPTIONS", + }, { + desc: "With registered PATCH and subtree root", + reqURL: "http://127.0.0.1:8080/options/", + expStatusCode: http.StatusNotFound, + }, { + desc: "With registered PATCH and query", + reqURL: "http://127.0.0.1:8080/options?k=v", + expStatusCode: http.StatusOK, + expAllow: "DELETE, OPTIONS, PATCH", + }} + + for _, c := range cases { + t.Log(c.desc) + + req, e := http.NewRequest(http.MethodOptions, c.reqURL, nil) + if e != nil { + t.Fatal(e) + } + + res, e := client.Do(req) + if e != nil { + t.Fatal(e) + } + + gotAllow := res.Header.Get("Allow") + + test.Assert(t, "StatusCode", c.expStatusCode, res.StatusCode, + true) + test.Assert(t, "Allow", c.expAllow, gotAllow, true) + } +} |
