diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-15 03:14:55 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-15 03:39:16 +0700 |
| commit | 72f2c183724199c2b749ec8efb59aa20b59d9e96 (patch) | |
| tree | 5c37a4637399d778703c6a2b3f26aeca333ca1e4 /lib/http | |
| parent | b9e12300d9050143fcfaa2193ad91842f035661f (diff) | |
| download | pakakeh.go-72f2c183724199c2b749ec8efb59aa20b59d9e96.tar.xz | |
lib/http: refactoring type of RequestMethod from int to string
The reason is to make storing or encoding the RequestMethod value readable
from user point of view instead of number, 0, 1, 2, etc.
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/client.go | 8 | ||||
| -rw-r--r-- | lib/http/client_request.go | 2 | ||||
| -rw-r--r-- | lib/http/endpoint.go | 32 | ||||
| -rw-r--r-- | lib/http/request_method.go | 45 | ||||
| -rw-r--r-- | lib/http/request_method_test.go | 42 | ||||
| -rw-r--r-- | lib/http/server.go | 3 |
6 files changed, 23 insertions, 109 deletions
diff --git a/lib/http/client.go b/lib/http/client.go index 163b1b02..27c272d3 100644 --- a/lib/http/client.go +++ b/lib/http/client.go @@ -272,7 +272,7 @@ func (client *Client) GenerateHTTPRequest(req ClientRequest) (httpReq *http.Requ ctx = context.Background() ) - httpReq, err = http.NewRequestWithContext(ctx, req.Method.String(), fullURL, body) + httpReq, err = http.NewRequestWithContext(ctx, string(req.Method), fullURL, body) if err != nil { return nil, fmt.Errorf(`%s: %w`, logp, err) } @@ -475,7 +475,11 @@ func (client *Client) doRequest(req ClientRequest) (res *ClientResponse, err err httpReq *http.Request ) - httpReq, err = http.NewRequestWithContext(ctx, req.Method.String(), fullURL, req.body) + if len(req.Method) == 0 { + req.Method = RequestMethodGet + } + + httpReq, err = http.NewRequestWithContext(ctx, string(req.Method), fullURL, req.body) if err != nil { return nil, err } diff --git a/lib/http/client_request.go b/lib/http/client_request.go index 9447576a..9c704c0e 100644 --- a/lib/http/client_request.go +++ b/lib/http/client_request.go @@ -142,7 +142,7 @@ func (creq *ClientRequest) toHTTPRequest(client *Client) (httpReq *http.Request, var ctx = context.Background() - httpReq, err = http.NewRequestWithContext(ctx, creq.Method.String(), path.String(), body) + httpReq, err = http.NewRequestWithContext(ctx, string(creq.Method), path.String(), body) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } diff --git a/lib/http/endpoint.go b/lib/http/endpoint.go index c13d7512..5763cf8d 100644 --- a/lib/http/endpoint.go +++ b/lib/http/endpoint.go @@ -28,6 +28,9 @@ type Endpoint struct { // Call is the main process of route. Call Callback + // Method contains HTTP method, default to GET. + Method RequestMethod + // Path contains route to be served, default to "/" if its empty. Path string @@ -36,35 +39,6 @@ type Endpoint struct { // ResponseType contains type of request, default to ResponseTypeNone. ResponseType ResponseType - - // Method contains HTTP method, default to GET. - Method RequestMethod -} - -// HTTPMethod return the string representation of HTTP method as predefined -// in "net/http". -func (ep *Endpoint) HTTPMethod() string { - switch ep.Method { - case RequestMethodGet: - return http.MethodGet - case RequestMethodConnect: - return http.MethodConnect - case RequestMethodDelete: - return http.MethodDelete - case RequestMethodHead: - return http.MethodHead - case RequestMethodOptions: - return http.MethodOptions - case RequestMethodPatch: - return http.MethodPatch - case RequestMethodPost: - return http.MethodPost - case RequestMethodPut: - return http.MethodPut - case RequestMethodTrace: - return http.MethodTrace - } - return http.MethodGet } func (ep *Endpoint) call( diff --git a/lib/http/request_method.go b/lib/http/request_method.go index 120fd55e..af0ade82 100644 --- a/lib/http/request_method.go +++ b/lib/http/request_method.go @@ -9,42 +9,17 @@ import ( ) // RequestMethod define type of HTTP method. -type RequestMethod int +type RequestMethod string // List of known HTTP methods. const ( - RequestMethodGet RequestMethod = iota - RequestMethodConnect - RequestMethodDelete - RequestMethodHead - RequestMethodOptions - RequestMethodPatch - RequestMethodPost - RequestMethodPut - RequestMethodTrace + RequestMethodConnect RequestMethod = http.MethodConnect + RequestMethodDelete RequestMethod = http.MethodDelete + RequestMethodGet RequestMethod = http.MethodGet + RequestMethodHead RequestMethod = http.MethodHead + RequestMethodOptions RequestMethod = http.MethodOptions + RequestMethodPatch RequestMethod = http.MethodPatch + RequestMethodPost RequestMethod = http.MethodPost + RequestMethodPut RequestMethod = http.MethodPut + RequestMethodTrace RequestMethod = http.MethodTrace ) - -// String return the string representation of request method. -func (rm RequestMethod) String() string { - switch rm { - case RequestMethodGet: - return http.MethodGet - case RequestMethodConnect: - return http.MethodConnect - case RequestMethodDelete: - return http.MethodDelete - case RequestMethodHead: - return http.MethodHead - case RequestMethodOptions: - return http.MethodOptions - case RequestMethodPatch: - return http.MethodPatch - case RequestMethodPost: - return http.MethodPost - case RequestMethodPut: - return http.MethodPut - case RequestMethodTrace: - return http.MethodTrace - } - return "" -} diff --git a/lib/http/request_method_test.go b/lib/http/request_method_test.go deleted file mode 100644 index 3723c6c4..00000000 --- a/lib/http/request_method_test.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package http - -import ( - "testing" - - "git.sr.ht/~shulhan/pakakeh.go/lib/test" -) - -func TestRequestMethod_String(t *testing.T) { - cases := []struct { - exp string - m RequestMethod - }{ - {m: 0, exp: "GET"}, - {m: 1, exp: "CONNECT"}, - {m: 2, exp: "DELETE"}, - {m: 3, exp: "HEAD"}, - {m: 4, exp: "OPTIONS"}, - {m: 5, exp: "PATCH"}, - {m: 6, exp: "POST"}, - {m: 7, exp: "PUT"}, - {m: 8, exp: "TRACE"}, - {m: 9, exp: ""}, - {m: RequestMethodGet, exp: "GET"}, - {m: RequestMethodConnect, exp: "CONNECT"}, - {m: RequestMethodDelete, exp: "DELETE"}, - {m: RequestMethodHead, exp: "HEAD"}, - {m: RequestMethodOptions, exp: "OPTIONS"}, - {m: RequestMethodPatch, exp: "PATCH"}, - {m: RequestMethodPost, exp: "POST"}, - {m: RequestMethodPut, exp: "PUT"}, - {m: RequestMethodTrace, exp: "TRACE"}, - } - - for _, c := range cases { - test.Assert(t, "RequestMethod.String", c.exp, c.m.String()) - } -} diff --git a/lib/http/server.go b/lib/http/server.go index b47ddba3..6cf8c70c 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -101,6 +101,9 @@ func (srv *Server) RegisterEndpoint(ep Endpoint) (err error) { if ep.Call == nil { return errors.New(logp + `: empty Call field`) } + if len(ep.Method) == 0 { + ep.Method = RequestMethodGet + } switch ep.Method { case RequestMethodConnect: |
