Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12f274d

Browse files
committed
feat: Ensure that HEAD method is properly handled
HEAD is now handled correctly by the test server and the request is built accordingly
1 parent 4179b2c commit 12f274d

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

internal/http/http.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ type HttpClient struct {
3232
net.Conn
3333
}
3434

35+
func NewHttpClient(address string) (HttpClient, error) {
36+
dial, err := net.Dial("tcp", address)
37+
if err != nil {
38+
return HttpClient{}, err
39+
}
40+
41+
return HttpClient{dial}, nil
42+
}
43+
3544
var supported_methods = []string{"GET", "HEAD"}
3645

3746
func (hc *HttpClient) Do(request Request) (Response, error) {

internal/http/http_test.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package http
22

33
import (
4-
"io"
54
"log/slog"
6-
"net"
75
"net/http"
86
"net/http/httptest"
97
"strings"
108
"testing"
119
)
1210

1311
func XyzHandler(w http.ResponseWriter, r *http.Request) {
14-
resp := "Hello World"
15-
slog.Info("Received request with protocol type", "proto", r.Proto)
16-
w.Header().Add("Content-Type", "text/plain")
17-
w.WriteHeader(HttpStatusCodeOK)
12+
if r.Method == "GET" {
13+
resp := "Hello World"
14+
slog.Info("Received request with protocol type", "proto", r.Proto)
15+
w.Header().Add("Content-Type", "text/plain")
16+
w.WriteHeader(HttpStatusCodeOK)
1817

19-
w.Write(([]byte(resp)))
18+
w.Write(([]byte(resp)))
19+
}
20+
21+
if r.Method == "HEAD" {
22+
w.WriteHeader(200)
23+
}
2024
}
2125

2226
func MiddleWare(next http.Handler) http.Handler {
@@ -26,20 +30,25 @@ func MiddleWare(next http.Handler) http.Handler {
2630
})
2731
}
2832

29-
func TestGet(t *testing.T) {
30-
33+
func BuildServer(t *testing.T) (*httptest.Server, string) {
34+
t.Helper()
3135
mux := http.NewServeMux()
3236
mux.Handle("/", MiddleWare(http.HandlerFunc(XyzHandler)))
3337

3438
server := httptest.NewServer(mux)
39+
url := strings.Split(server.URL, "://")[1]
40+
return server, url
41+
}
42+
43+
func TestGet(t *testing.T) {
44+
server, url := BuildServer(t)
3545
defer server.Close()
3646

37-
url := strings.Split(server.URL, "://")[1]
38-
conn, err := net.Dial("tcp", url)
47+
http_client, err := NewHttpClient(url)
48+
3949
if err != nil {
40-
t.Errorf("failed to dial socket %s", err)
50+
t.Errorf("failed to create new http client %s", err)
4151
}
42-
http_client := HttpClient{conn}
4352
res, err := http_client.Get("/")
4453

4554
if err != nil {
@@ -62,29 +71,21 @@ func TestGet(t *testing.T) {
6271
}
6372

6473
func TestHead(t *testing.T) {
65-
return
66-
server, client := net.Pipe()
67-
http_client := HttpClient{client}
68-
69-
go func() {
70-
defer client.Close()
71-
_, err := http_client.Head("xyz")
72-
73-
if err != nil {
74-
t.Errorf("Unexpected error %s", err)
75-
}
76-
}()
74+
server, url := BuildServer(t)
75+
http_client, err := NewHttpClient(url)
7776

78-
res, err := io.ReadAll(server)
77+
if err != nil {
78+
t.Errorf("unexpected error when creating http client %s", err)
79+
}
80+
defer server.Close()
81+
resp, err := http_client.Head("/")
7982

8083
if err != nil {
8184
t.Errorf("Unexpected error %s", err)
8285
}
8386

84-
expected := "HEAD xyz HTTP/1.1\nHost: localhost\r\n\r\n"
85-
86-
if string(res) != expected {
87-
t.Errorf("got %s want %s", string(res), expected)
87+
if !resp.Ok() {
88+
t.Errorf("got %d want %d", resp.StatusLine.StatusCode, 200)
8889
}
8990
}
9091

0 commit comments

Comments
 (0)