aboutsummaryrefslogtreecommitdiff
path: root/lib/http
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-03-08 02:52:16 +0700
committerShulhan <ms@kilabit.info>2024-03-09 01:10:24 +0700
commit29343294a596fd74f6f9cb917c8aac74ccb78094 (patch)
treee9286ed53041b973cb6d767325034f30e504e1f5 /lib/http
parent4e35c509a41cecb207bf6f52e7c9a529fa9f71fb (diff)
downloadpakakeh.go-29343294a596fd74f6f9cb917c8aac74ccb78094.tar.xz
lib/http: refactoring NewServer and NewClient
The NewServer and NewClient now accept non-pointer options, so the caller unable to modify the options once the server or client has been created.
Diffstat (limited to 'lib/http')
-rw-r--r--lib/http/client.go6
-rw-r--r--lib/http/client_test.go2
-rw-r--r--lib/http/endpoint_example_test.go22
-rw-r--r--lib/http/endpoint_response_example_test.go19
-rw-r--r--lib/http/http_test.go29
-rw-r--r--lib/http/server.go16
-rw-r--r--lib/http/server_example_test.go13
-rw-r--r--lib/http/server_test.go14
-rw-r--r--lib/http/sse_endpoint_test.go7
-rw-r--r--lib/http/sseclient/sseclient_test.go2
10 files changed, 69 insertions, 61 deletions
diff --git a/lib/http/client.go b/lib/http/client.go
index eeb26e44..78f3d65a 100644
--- a/lib/http/client.go
+++ b/lib/http/client.go
@@ -40,9 +40,9 @@ type Client struct {
flateReader io.ReadCloser
gzipReader *gzip.Reader
- opts *ClientOptions
-
*http.Client
+
+ opts ClientOptions
}
// NewClient create and initialize new [Client].
@@ -50,7 +50,7 @@ type Client struct {
// The client will have [net.Dialer.KeepAlive] set to 30 seconds, with one
// [http.Transport.MaxIdleConns], and 90 seconds
// [http.Transport.IdleConnTimeout].
-func NewClient(opts *ClientOptions) (client *Client) {
+func NewClient(opts ClientOptions) (client *Client) {
opts.init()
httpTransport := &http.Transport{
diff --git a/lib/http/client_test.go b/lib/http/client_test.go
index 56ff4333..021d309e 100644
--- a/lib/http/client_test.go
+++ b/lib/http/client_test.go
@@ -18,7 +18,7 @@ func TestClient_Download(t *testing.T) {
clientOpts = ClientOptions{
ServerURL: `http://` + testServer.Options.Address,
}
- client = NewClient(&clientOpts)
+ client = NewClient(clientOpts)
out bytes.Buffer
err error
diff --git a/lib/http/endpoint_example_test.go b/lib/http/endpoint_example_test.go
index 3549f488..15b667d3 100644
--- a/lib/http/endpoint_example_test.go
+++ b/lib/http/endpoint_example_test.go
@@ -16,10 +16,14 @@ import (
)
func ExampleEndpoint_errorHandler() {
- serverOpts := &ServerOptions{
- Address: "127.0.0.1:8123",
- }
- server, _ := NewServer(serverOpts)
+ var (
+ serverOpts = ServerOptions{
+ Address: `127.0.0.1:8123`,
+ }
+ server *Server
+ )
+
+ server, _ = NewServer(serverOpts)
var endpointError = Endpoint{
Method: RequestMethodGet,
@@ -53,10 +57,12 @@ func ExampleEndpoint_errorHandler() {
}()
time.Sleep(1 * time.Second)
- clientOpts := &ClientOptions{
- ServerURL: `http://` + serverOpts.Address,
- }
- client := NewClient(clientOpts)
+ var (
+ clientOpts = ClientOptions{
+ ServerURL: `http://` + serverOpts.Address,
+ }
+ client = NewClient(clientOpts)
+ )
params := url.Values{}
params.Set("error", "400:error with status code")
diff --git a/lib/http/endpoint_response_example_test.go b/lib/http/endpoint_response_example_test.go
index 40e9d6d7..2e78c66f 100644
--- a/lib/http/endpoint_response_example_test.go
+++ b/lib/http/endpoint_response_example_test.go
@@ -18,7 +18,12 @@ func ExampleEndpointResponse() {
ID string
}
- server, err := NewServer(&ServerOptions{
+ var (
+ server *Server
+ err error
+ )
+
+ server, err = NewServer(ServerOptions{
Address: "127.0.0.1:7016",
})
if err != nil {
@@ -67,11 +72,13 @@ func ExampleEndpointResponse() {
}()
time.Sleep(1 * time.Second)
- clientOpts := &ClientOptions{
- ServerURL: `http://127.0.0.1:7016`,
- }
- cl := NewClient(clientOpts)
- params := url.Values{}
+ var (
+ clientOpts = ClientOptions{
+ ServerURL: `http://127.0.0.1:7016`,
+ }
+ cl = NewClient(clientOpts)
+ params = url.Values{}
+ )
// Test call endpoint without "id" parameter.
_, resBody, err := cl.Get("/", nil, params) //nolint: bodyclose
diff --git a/lib/http/http_test.go b/lib/http/http_test.go
index c26746e3..c6872fe4 100644
--- a/lib/http/http_test.go
+++ b/lib/http/http_test.go
@@ -52,23 +52,22 @@ var (
func TestMain(m *testing.M) {
var (
- serverAddress = "127.0.0.1:14832"
- err error
- )
-
- opts := &ServerOptions{
- Memfs: &memfs.MemFS{
- Opts: &memfs.Options{
- Root: "./testdata",
- MaxFileSize: 30,
- TryDirect: true,
+ opts = ServerOptions{
+ Memfs: &memfs.MemFS{
+ Opts: &memfs.Options{
+ Root: `./testdata`,
+ MaxFileSize: 30,
+ TryDirect: true,
+ },
},
- },
- HandleFS: handleFS,
- Address: serverAddress,
- }
+ HandleFS: handleFS,
+ Address: `127.0.0.1:14832`,
+ }
+
+ err error
+ )
- testServerURL = `http://` + serverAddress
+ testServerURL = `http://` + opts.Address
testServer, err = NewServer(opts)
if err != nil {
diff --git a/lib/http/server.go b/lib/http/server.go
index d2e69ecc..b47ddba3 100644
--- a/lib/http/server.go
+++ b/lib/http/server.go
@@ -33,24 +33,24 @@ const (
type Server struct {
*http.Server
- // Options for server, set by calling NewServer.
- // This field is exported only for reference, for example logging in
- // the Options when server started.
- // Modifying the value of Options after server has been started may
- // cause undefined effects.
- Options *ServerOptions
-
evals []Evaluator
routeDeletes []*route
routeGets []*route
routePatches []*route
routePosts []*route
routePuts []*route
+
+ // Options for server, set by calling NewServer.
+ // This field is exported only for reference, for example logging in
+ // the Options when server started.
+ // Modifying the value of Options after server has been started may
+ // cause undefined effects.
+ Options ServerOptions
}
// NewServer create and initialize new HTTP server that serve root directory
// with custom connection.
-func NewServer(opts *ServerOptions) (srv *Server, err error) {
+func NewServer(opts ServerOptions) (srv *Server, err error) {
opts.init()
srv = &Server{
diff --git a/lib/http/server_example_test.go b/lib/http/server_example_test.go
index 74a60f2a..460a78fd 100644
--- a/lib/http/server_example_test.go
+++ b/lib/http/server_example_test.go
@@ -21,8 +21,7 @@ func ExampleServer_customHTTPStatusCode() {
exp = CustomResponse{
Status: http.StatusBadRequest,
}
-
- opts = &ServerOptions{
+ opts = ServerOptions{
Address: "127.0.0.1:8123",
}
@@ -67,10 +66,12 @@ func ExampleServer_customHTTPStatusCode() {
// Wait for the server fully started.
time.Sleep(1 * time.Second)
- clientOpts := &ClientOptions{
- ServerURL: `http://127.0.0.1:8123`,
- }
- client := NewClient(clientOpts)
+ var (
+ clientOpts = ClientOptions{
+ ServerURL: `http://127.0.0.1:8123`,
+ }
+ client = NewClient(clientOpts)
+ )
httpRes, resBody, err := client.PostJSON(epCustom.Path, nil, nil) //nolint: bodyclose
if err != nil {
diff --git a/lib/http/server_test.go b/lib/http/server_test.go
index f18b27c6..95995de5 100644
--- a/lib/http/server_test.go
+++ b/lib/http/server_test.go
@@ -950,7 +950,7 @@ func TestServer_Options_HandleFS(t *testing.T) {
func TestServer_handleRange(t *testing.T) {
var (
- clOpts = &ClientOptions{
+ clOpts = ClientOptions{
ServerURL: testServerURL,
}
cl = NewClient(clOpts)
@@ -1026,7 +1026,7 @@ func TestServer_handleRange(t *testing.T) {
func TestServer_handleRange_HEAD(t *testing.T) {
var (
- clOpts = &ClientOptions{
+ clOpts = ClientOptions{
ServerURL: testServerURL,
}
cl = NewClient(clOpts)
@@ -1129,15 +1129,11 @@ func TestServerHandleRangeBig(t *testing.T) {
}
var (
- clOpts = &ClientOptions{
+ clOpts = ClientOptions{
ServerURL: `http://` + serverAddress,
}
- cl *Client
- )
-
- cl = NewClient(clOpts)
+ cl = NewClient(clOpts)
- var (
tag = `HEAD /big`
skipHeaders = []string{HeaderDate}
@@ -1225,7 +1221,7 @@ func runServerFS(t *testing.T, address, dir string) (srv *Server) {
nodeBig.SetModTime(pathBigModTime)
var (
- srvOpts = &ServerOptions{
+ srvOpts = ServerOptions{
Memfs: mfs,
Address: address,
}
diff --git a/lib/http/sse_endpoint_test.go b/lib/http/sse_endpoint_test.go
index 2a9e28f2..ebc2036a 100644
--- a/lib/http/sse_endpoint_test.go
+++ b/lib/http/sse_endpoint_test.go
@@ -11,11 +11,10 @@ import (
)
func TestSSEEndpoint(t *testing.T) {
- var opts = &ServerOptions{
- Address: `127.0.0.1:24168`,
- }
-
var (
+ opts = ServerOptions{
+ Address: `127.0.0.1:24168`,
+ }
httpd *Server
err error
)
diff --git a/lib/http/sseclient/sseclient_test.go b/lib/http/sseclient/sseclient_test.go
index d1ed4e1f..116ddbb5 100644
--- a/lib/http/sseclient/sseclient_test.go
+++ b/lib/http/sseclient/sseclient_test.go
@@ -423,7 +423,7 @@ func testRunSSEServer(t *testing.T, cb libhttp.SSECallback) (srv *libhttp.Server
var address = testGenerateAddress()
var (
- serverOpts = &libhttp.ServerOptions{
+ serverOpts = libhttp.ServerOptions{
Address: address,
}
)