diff options
| author | Shulhan <ms@kilabit.info> | 2021-12-26 01:53:30 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-12-26 01:53:30 +0700 |
| commit | d7f415e3d9fff5ba6427294b6d6ba8e555056b76 (patch) | |
| tree | ef7fa2d6df62b77c0c3c7650a799179b52c7d7c3 | |
| parent | 682c9326cc717ac5b698f36f8d46cfbb394cd4a6 (diff) | |
| download | pakakeh.go-d7f415e3d9fff5ba6427294b6d6ba8e555056b76.tar.xz | |
lib/http: realign all structs
Changes,
* Client: from 56 to 48 bytes (-8 bytes)
* CORSOptions: from 104 to 88 bytes (-16 bytes)
* Endpoint: from 64 to 32 bytes (-32 bytes)
* EndpointRequest: from 72 to 56 bytes (-16 bytes)
* route: from 56 to 32 bytes (-24 bytes)
Other changes is struct on unit tests.
| -rw-r--r-- | lib/http/client.go | 6 | ||||
| -rw-r--r-- | lib/http/cors_options.go | 5 | ||||
| -rw-r--r-- | lib/http/endpoint.go | 24 | ||||
| -rw-r--r-- | lib/http/endpoint_request.go | 4 | ||||
| -rw-r--r-- | lib/http/requestmethod_test.go | 40 | ||||
| -rw-r--r-- | lib/http/requesttype_test.go | 12 | ||||
| -rw-r--r-- | lib/http/response_test.go | 4 | ||||
| -rw-r--r-- | lib/http/responsetype_test.go | 26 | ||||
| -rw-r--r-- | lib/http/route.go | 2 | ||||
| -rw-r--r-- | lib/http/route_test.go | 2 | ||||
| -rw-r--r-- | lib/http/server_test.go | 18 |
11 files changed, 72 insertions, 71 deletions
diff --git a/lib/http/client.go b/lib/http/client.go index 8cd23eb9..16ab3fe7 100644 --- a/lib/http/client.go +++ b/lib/http/client.go @@ -39,13 +39,13 @@ const ( // including setting default headers, uncompressing response body. // type Client struct { - *http.Client - flateReader io.ReadCloser gzipReader *gzip.Reader - serverURL string defHeaders http.Header + *http.Client + + serverURL string } // diff --git a/lib/http/cors_options.go b/lib/http/cors_options.go index 910c2fe8..c4145d8a 100644 --- a/lib/http/cors_options.go +++ b/lib/http/cors_options.go @@ -9,6 +9,9 @@ package http // access its resources. // type CORSOptions struct { + exposeHeaders string + maxAge string + // AllowOrigins contains global list of cross-site Origin that are // allowed during preflight requests by the OPTIONS method. // The list is case-sensitive. @@ -25,13 +28,11 @@ type CORSOptions struct { // This list will be send when browser request OPTIONS without // request-method. ExposeHeaders []string - exposeHeaders string // MaxAge gives the value in seconds for how long the response to // the preflight request can be cached for without sending another // preflight request. MaxAge int - maxAge string // AllowCredentials indicates whether or not the actual request // can be made using credentials. diff --git a/lib/http/endpoint.go b/lib/http/endpoint.go index f7cc2868..6e23352d 100644 --- a/lib/http/endpoint.go +++ b/lib/http/endpoint.go @@ -20,8 +20,16 @@ import ( // evaluators from server. // type Endpoint struct { - // Method contains HTTP method, default to GET. - Method RequestMethod + // ErrorHandler define the function that will handle the error + // returned from Call. + ErrorHandler CallbackErrorHandler + + // Eval define evaluator for route that will be called after global + // evaluators and before callback. + Eval Evaluator + + // Call is the main process of route. + Call Callback // Path contains route to be served, default to "/" if its empty. Path string @@ -32,16 +40,8 @@ type Endpoint struct { // ResponseType contains type of request, default to ResponseTypeNone. ResponseType ResponseType - // Eval define evaluator for route that will be called after global - // evaluators and before callback. - Eval Evaluator - - // Call is the main process of route. - Call Callback - - // ErrorHandler define the function that will handle the error - // returned from Call. - ErrorHandler CallbackErrorHandler + // Method contains HTTP method, default to GET. + Method RequestMethod } // diff --git a/lib/http/endpoint_request.go b/lib/http/endpoint_request.go index fd300a6d..1c67eb74 100644 --- a/lib/http/endpoint_request.go +++ b/lib/http/endpoint_request.go @@ -16,9 +16,9 @@ import "net/http" // The Error field is used by CallbackErrorHandler. // type EndpointRequest struct { - Endpoint *Endpoint HttpWriter http.ResponseWriter + Error error + Endpoint *Endpoint HttpRequest *http.Request RequestBody []byte - Error error } diff --git a/lib/http/requestmethod_test.go b/lib/http/requestmethod_test.go index e0ef4cae..630afffa 100644 --- a/lib/http/requestmethod_test.go +++ b/lib/http/requestmethod_test.go @@ -12,28 +12,28 @@ import ( func TestRequestMethod_String(t *testing.T) { cases := []struct { - m RequestMethod exp string + m RequestMethod }{ - {0, "GET"}, - {1, "CONNECT"}, - {2, "DELETE"}, - {3, "HEAD"}, - {4, "OPTIONS"}, - {5, "PATCH"}, - {6, "POST"}, - {7, "PUT"}, - {8, "TRACE"}, - {9, ""}, - {RequestMethodGet, "GET"}, - {RequestMethodConnect, "CONNECT"}, - {RequestMethodDelete, "DELETE"}, - {RequestMethodHead, "HEAD"}, - {RequestMethodOptions, "OPTIONS"}, - {RequestMethodPatch, "PATCH"}, - {RequestMethodPost, "POST"}, - {RequestMethodPut, "PUT"}, - {RequestMethodTrace, "TRACE"}, + {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 { diff --git a/lib/http/requesttype_test.go b/lib/http/requesttype_test.go index 85f30f3e..8bf5a420 100644 --- a/lib/http/requesttype_test.go +++ b/lib/http/requesttype_test.go @@ -12,14 +12,14 @@ import ( func TestRequestType_String(t *testing.T) { cases := []struct { - rt RequestType exp string + rt RequestType }{ - {0, ""}, - {1, ""}, - {2, ContentTypeForm}, - {3, ContentTypeMultipartForm}, - {4, ContentTypeJSON}, + {rt: 0, exp: ""}, + {rt: 1, exp: ""}, + {rt: 2, exp: ContentTypeForm}, + {rt: 3, exp: ContentTypeMultipartForm}, + {rt: 4, exp: ContentTypeJSON}, } for _, c := range cases { diff --git a/lib/http/response_test.go b/lib/http/response_test.go index 4625b35d..433f2201 100644 --- a/lib/http/response_test.go +++ b/lib/http/response_test.go @@ -13,11 +13,11 @@ import ( func TestParseResponseHeader(t *testing.T) { cases := []struct { + expResp *http.Response + expErr string desc string raw []byte - expResp *http.Response expRest []byte - expErr string }{{ desc: "With empty input", }, { diff --git a/lib/http/responsetype_test.go b/lib/http/responsetype_test.go index 2a2926c3..044ed27c 100644 --- a/lib/http/responsetype_test.go +++ b/lib/http/responsetype_test.go @@ -12,21 +12,21 @@ import ( func TestResponseType_String(t *testing.T) { cases := []struct { - restype ResponseType exp string + restype ResponseType }{ - {0, ""}, - {1, ContentTypeBinary}, - {2, ContentTypeHTML}, - {3, ContentTypeJSON}, - {4, ContentTypePlain}, - {5, ContentTypeXML}, - {ResponseTypeNone, ""}, - {ResponseTypeBinary, ContentTypeBinary}, - {ResponseTypeHTML, ContentTypeHTML}, - {ResponseTypeJSON, ContentTypeJSON}, - {ResponseTypePlain, ContentTypePlain}, - {ResponseTypeXML, ContentTypeXML}, + {restype: 0, exp: ""}, + {restype: 1, exp: ContentTypeBinary}, + {restype: 2, exp: ContentTypeHTML}, + {restype: 3, exp: ContentTypeJSON}, + {restype: 4, exp: ContentTypePlain}, + {restype: 5, exp: ContentTypeXML}, + {restype: ResponseTypeNone, exp: ""}, + {restype: ResponseTypeBinary, exp: ContentTypeBinary}, + {restype: ResponseTypeHTML, exp: ContentTypeHTML}, + {restype: ResponseTypeJSON, exp: ContentTypeJSON}, + {restype: ResponseTypePlain, exp: ContentTypePlain}, + {restype: ResponseTypeXML, exp: ContentTypeXML}, } for _, c := range cases { diff --git a/lib/http/route.go b/lib/http/route.go index 3eef6b4c..e5091be6 100644 --- a/lib/http/route.go +++ b/lib/http/route.go @@ -12,10 +12,10 @@ import ( // route represent the route to endpoint. // type route struct { + endpoint *Endpoint // endpoint of route. path string // path contains Endpoint's path that has been cleaned up. nodes []*node // nodes contains sub-path. nkey int // nkey contains the number of keys in nodes. - endpoint *Endpoint // endpoint of route. } // diff --git a/lib/http/route_test.go b/lib/http/route_test.go index 6a161ea5..413e27f5 100644 --- a/lib/http/route_test.go +++ b/lib/http/route_test.go @@ -120,9 +120,9 @@ func TestNewRoute(t *testing.T) { func TestRoute_parse(t *testing.T) { type testPath struct { + expVals map[string]string path string expOK bool - expVals map[string]string } cases := []struct { diff --git a/lib/http/server_test.go b/lib/http/server_test.go index 1f87fa45..9b398b62 100644 --- a/lib/http/server_test.go +++ b/lib/http/server_test.go @@ -17,13 +17,13 @@ import ( func TestRegisterDelete(t *testing.T) { cases := []struct { + ep *Endpoint desc string reqURL string - ep *Endpoint - expStatusCode int expContentType string expBody string expError string + expStatusCode int }{{ desc: "With new endpoint", ep: &Endpoint{ @@ -256,8 +256,8 @@ func TestRegisterGet(t *testing.T) { cases := []struct { desc string reqURL string - expStatusCode int expBody string + expStatusCode int }{{ desc: "With root path", reqURL: "http://127.0.0.1:8080/", @@ -325,10 +325,10 @@ func TestRegisterHead(t *testing.T) { cases := []struct { desc string reqURL string - expStatusCode int expBody string expContentType []string expContentLength []string + expStatusCode int }{{ desc: "With root path", reqURL: "http://127.0.0.1:8080/", @@ -393,8 +393,8 @@ func TestRegisterPatch(t *testing.T) { cases := []struct { desc string reqURL string - expStatusCode int expBody string + expStatusCode int }{{ desc: "With root path", reqURL: "http://127.0.0.1:8080/", @@ -456,8 +456,8 @@ func TestRegisterPost(t *testing.T) { desc string reqURL string reqBody string - expStatusCode int expBody string + expStatusCode int }{{ desc: "With root path", reqURL: "http://127.0.0.1:8080/", @@ -526,8 +526,8 @@ func TestRegisterPut(t *testing.T) { cases := []struct { desc string reqURL string - expStatusCode int expBody string + expStatusCode int }{{ desc: "With root path", reqURL: "http://127.0.0.1:8080/", @@ -596,8 +596,8 @@ func TestServeHTTPOptions(t *testing.T) { cases := []struct { desc string reqURL string - expStatusCode int expAllow string + expStatusCode int }{{ desc: "With root path", reqURL: "http://127.0.0.1:8080/", @@ -726,8 +726,8 @@ func TestStatusError(t *testing.T) { cases := []struct { desc string reqURL string - expStatusCode int expBody string + expStatusCode int }{{ desc: "With registered error no body", reqURL: "http://127.0.0.1:8080/error/no-body?k=v", |
