diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-09 05:04:37 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-14 23:51:34 +0700 |
| commit | 3ecf5e7b4844a565fce590ebb507f6e7cc98b798 (patch) | |
| tree | f46634592a854fe508a3de4d04b244c44fffc838 /lib/http | |
| parent | 8d9c5ff8958b6adcd01e86f9a48f256df388b9db (diff) | |
| download | pakakeh.go-3ecf5e7b4844a565fce590ebb507f6e7cc98b798.tar.xz | |
lib/http: add test for Server handleDelete
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/server_test.go | 73 | ||||
| -rw-r--r-- | lib/http/testdata/handleDelete_test.txt | 14 |
2 files changed, 87 insertions, 0 deletions
diff --git a/lib/http/server_test.go b/lib/http/server_test.go index 95995de5..219df876 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "errors" + "fmt" "io" "log" "mime" @@ -22,6 +23,7 @@ import ( "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net" "git.sr.ht/~shulhan/pakakeh.go/lib/test" + "git.sr.ht/~shulhan/pakakeh.go/lib/test/httptest" ) func TestRegisterDelete(t *testing.T) { @@ -948,6 +950,77 @@ func TestServer_Options_HandleFS(t *testing.T) { } } +func TestServer_handleDelete(t *testing.T) { + type testCase struct { + tag string + req httptest.SimulateRequest + } + + var ( + srv = &Server{} + err error + ) + + err = srv.RegisterEndpoint(Endpoint{ + Method: RequestMethodDelete, + Path: `/a/b/c/:d/e`, + RequestType: RequestTypeNone, + ResponseType: ResponseTypePlain, + Call: func(epr *EndpointRequest) ([]byte, error) { + var buf bytes.Buffer + fmt.Fprintf(&buf, `Request.Form=%v`, epr.HTTPRequest.Form) + return buf.Bytes(), nil + }, + }) + if err != nil { + t.Fatal(err) + } + + var tdata *test.Data + + tdata, err = test.LoadData(`testdata/handleDelete_test.txt`) + if err != nil { + t.Fatal(err) + } + + var listCase = []testCase{{ + tag: `valid`, + req: httptest.SimulateRequest{ + Method: http.MethodDelete, + Path: `/a/b/c/dddd/e`, + }, + }} + var ( + c testCase + result *httptest.SimulateResult + tag string + exp string + got []byte + ) + for _, c = range listCase { + result, err = httptest.Simulate(srv.ServeHTTP, &c.req) + if err != nil { + t.Fatal(err) + } + + got, err = result.DumpRequest(nil) + if err != nil { + t.Fatal(err) + } + tag = c.tag + `:request_body` + exp = string(tdata.Output[tag]) + test.Assert(t, tag, exp, string(got)) + + got, err = result.DumpResponse(nil) + if err != nil { + t.Fatal(err) + } + tag = c.tag + `:response_body` + exp = string(tdata.Output[tag]) + test.Assert(t, tag, exp, string(got)) + } +} + func TestServer_handleRange(t *testing.T) { var ( clOpts = ClientOptions{ diff --git a/lib/http/testdata/handleDelete_test.txt b/lib/http/testdata/handleDelete_test.txt new file mode 100644 index 00000000..da170afc --- /dev/null +++ b/lib/http/testdata/handleDelete_test.txt @@ -0,0 +1,14 @@ +Test data for handleDelete in Server. + +<<< valid:request_body +DELETE /a/b/c/dddd/e HTTP/1.1 +Host: example.com + + + +<<< valid:response_body +HTTP/1.1 200 OK +Connection: close +Content-Type: text/plain; charset=utf-8 + +Request.Form=map[d:[dddd]] |
