-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
138 lines (130 loc) · 4.17 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package httpserver_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
httpserver "github.com/thesujai/http_server_go"
"github.com/thesujai/http_server_go/mocks"
)
func TestParse(t *testing.T) {
testData := []struct {
name string
requestData string
expectedHttpRequest *httpserver.HttpRequest
expectError bool
err error
}{
{
name: "Valid GET Request",
requestData: "GET /hello HTTP/1.1\r\nContent-Length: 11\r\nContent-Type: text/plain\r\n\r\nHello World",
expectedHttpRequest: &httpserver.HttpRequest{
Method: "GET",
Path: "/hello",
Version: "HTTP/1.1",
Headers: []httpserver.Header{
{"Content-Length": "11"},
{"Content-Type": "text/plain"},
},
ContentLength: 11,
ContentType: "text/plain",
Body: io.NopCloser(io.LimitReader(bytes.NewReader([]byte("Hello World")), 11)),
},
expectError: false,
},
{
name: "Valid POST Request",
requestData: "POST /login HTTP/1.1\r\nContent-Length: 45\r\nContent-Type: application/json\r\n\r\n{\"username\": \"johndoe\", \"password\": \"secret\"}",
expectedHttpRequest: &httpserver.HttpRequest{
Method: "POST",
Path: "/login",
Version: "HTTP/1.1",
Headers: []httpserver.Header{
{"Content-Length": "45"},
{"Content-Type": "application/json"},
},
ContentLength: 45,
ContentType: "application/json",
Body: io.NopCloser(io.LimitReader(bytes.NewReader([]byte(`{"username": "johndoe", "password": "secret"}`)), 45)),
},
expectError: false,
},
{
name: "Invalid request line",
requestData: "GET /hello\r\nContent-Length: 11\r\nContent-Type: text/plain\r\n\r\nHello World",
expectedHttpRequest: nil,
expectError: true,
err: fmt.Errorf("invalid request line"),
},
{
name: "Missing Body",
requestData: "GET /hello HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
expectedHttpRequest: &httpserver.HttpRequest{
Method: "GET",
Path: "/hello",
Version: "HTTP/1.1",
Headers: []httpserver.Header{
{"Content-Length": "0"},
},
ContentLength: 0,
ContentType: "",
Body: io.NopCloser(bytes.NewReader(nil)),
},
},
}
for _, tc := range testData {
t.Run(tc.name, func(t *testing.T) {
mockConn := mocks.MockConn{
Reader: bytes.NewBuffer([]byte(tc.requestData)),
Writer: &bytes.Buffer{},
}
req, err := httpserver.Parse(&mockConn)
if tc.expectError {
if err == nil {
t.Fatalf("expected error but got nil")
}
if err.Error() != tc.err.Error() {
t.Fatalf("expected error '%v' but got '%v'", tc.err, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Method != tc.expectedHttpRequest.Method {
t.Errorf("expected method '%s', got '%s'", tc.expectedHttpRequest.Method, req.Method)
}
if req.Path != tc.expectedHttpRequest.Path {
t.Errorf("expected path '%s', got '%s'", tc.expectedHttpRequest.Path, req.Path)
}
if req.Version != tc.expectedHttpRequest.Version {
t.Errorf("expected version '%s', got '%s'", tc.expectedHttpRequest.Version, req.Version)
}
if req.ContentLength != tc.expectedHttpRequest.ContentLength {
t.Errorf("expected content length '%d', got '%d'", tc.expectedHttpRequest.ContentLength, req.ContentLength)
}
if req.ContentType != tc.expectedHttpRequest.ContentType {
t.Errorf("expected content type '%s', got '%s'", tc.expectedHttpRequest.ContentType, req.ContentType)
}
for i, expectedHeader := range tc.expectedHttpRequest.Headers {
if i >= len(req.Headers) {
t.Errorf("missing header at index %d: %v", i, expectedHeader)
continue
}
for k, v := range expectedHeader {
if req.Headers[i][k] != v {
t.Errorf("expected header '%s: %s', got '%s'", k, v, req.Headers[i][k])
}
}
}
if tc.expectedHttpRequest.Body != nil {
expectedBody, _ := ioutil.ReadAll(tc.expectedHttpRequest.Body)
actualBody, _ := ioutil.ReadAll(req.Body)
if !bytes.Equal(expectedBody, actualBody) {
t.Errorf("expected body '%s', got '%s'", string(expectedBody), string(actualBody))
}
}
})
}
}