-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
206 lines (194 loc) · 5.21 KB
/
Copy pathparser_test.go
File metadata and controls
206 lines (194 loc) · 5.21 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package httpparser
import (
"net/http"
"reflect"
"strings"
"testing"
)
func TestParseString(t *testing.T) {
tests := []struct {
name string
input string
wantVars map[string]string
want []Request
}{
{
name: "single GET, no headers, no body",
input: "GET https://example.com/health\n",
want: []Request{
{Method: "GET", URL: "https://example.com/health", Headers: http.Header{}, Line: 1},
},
},
{
name: "single POST with headers and JSON body",
input: "POST https://example.com/users\n" +
"Content-Type: application/json\n" +
"Accept: application/json\n" +
"\n" +
"{\n \"name\": \"Ada\"\n}\n",
want: []Request{
{
Method: "POST",
URL: "https://example.com/users",
Headers: http.Header{
"Content-Type": {"application/json"},
"Accept": {"application/json"},
},
Body: []byte("{\n \"name\": \"Ada\"\n}"),
Line: 1,
},
},
},
{
name: "multiple requests separated by ###",
input: "GET https://example.com/a\n" +
"###\n" +
"DELETE https://example.com/b\n",
want: []Request{
{Method: "GET", URL: "https://example.com/a", Headers: http.Header{}, Line: 1},
{Method: "DELETE", URL: "https://example.com/b", Headers: http.Header{}, Line: 3},
},
},
{
name: "file-level @var declarations",
input: "@base = https://example.com\n" +
"@token=xyz\n" +
"\n" +
"GET {{base}}/ping\n",
wantVars: map[string]string{
"base": "https://example.com",
"token": "xyz",
},
want: []Request{
{Method: "GET", URL: "{{base}}/ping", Headers: http.Header{}, Line: 4},
},
},
{
name: "@name annotation sets Request.Name",
input: "# @name fetchThing\n" +
"GET https://example.com/thing\n",
want: []Request{
{Name: "fetchThing", Method: "GET", URL: "https://example.com/thing", Headers: http.Header{}, Line: 2},
},
},
{
name: "placeholders left intact in URL, headers, and body",
input: "POST {{host}}/items/{{id}}\n" +
"Authorization: Bearer {{token}}\n" +
"\n" +
"{\"value\": \"{{val}}\"}\n",
want: []Request{
{
Method: "POST",
URL: "{{host}}/items/{{id}}",
Headers: http.Header{
"Authorization": {"Bearer {{token}}"},
},
Body: []byte("{\"value\": \"{{val}}\"}"),
Line: 1,
},
},
},
{
name: "comments ignored",
input: "# a leading comment\n" +
"// another comment\n" +
"GET https://example.com/x\n" +
"// header-region comment\n" +
"X-Test: 1\n",
want: []Request{
{
Method: "GET",
URL: "https://example.com/x",
Headers: http.Header{"X-Test": {"1"}},
Line: 3,
},
},
},
{
name: "HTTP version accepted and ignored",
input: "GET https://example.com/ver HTTP/1.1\n",
want: []Request{
{Method: "GET", URL: "https://example.com/ver", Headers: http.Header{}, Line: 1},
},
},
{
name: "blank file returns empty File",
input: "",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := ParseString(tt.input)
if err != nil {
t.Fatalf("ParseString returned error: %v", err)
}
wantVars := tt.wantVars
if wantVars == nil {
wantVars = map[string]string{}
}
if !reflect.DeepEqual(f.Vars, wantVars) {
t.Errorf("Vars = %#v, want %#v", f.Vars, wantVars)
}
if !reflect.DeepEqual(f.Requests, tt.want) {
t.Errorf("Requests = %#v, want %#v", f.Requests, tt.want)
}
})
}
}
func TestParseMalformedRequestLine(t *testing.T) {
// A method-less request line on line 3 must produce an error naming the line.
input := "# comment\n" +
"\n" +
"NOTAMETHOD\n"
_, err := ParseString(input)
if err == nil {
t.Fatal("expected error for malformed request line, got nil")
}
if !strings.Contains(err.Error(), "line 3") {
t.Errorf("error = %q, want it to mention %q", err.Error(), "line 3")
}
}
func TestParseUnsupportedMethod(t *testing.T) {
_, err := ParseString("TRACE https://example.com/x\n")
if err == nil {
t.Fatal("expected error for unsupported method, got nil")
}
if !strings.Contains(err.Error(), "line 1") {
t.Errorf("error = %q, want it to mention %q", err.Error(), "line 1")
}
}
func TestParseFile(t *testing.T) {
f, err := ParseFile("testdata/sample.http")
if err != nil {
t.Fatalf("ParseFile returned error: %v", err)
}
wantVars := map[string]string{
"baseUrl": "https://api.example.com",
"token": "abc123",
}
if !reflect.DeepEqual(f.Vars, wantVars) {
t.Errorf("Vars = %#v, want %#v", f.Vars, wantVars)
}
if len(f.Requests) != 2 {
t.Fatalf("got %d requests, want 2", len(f.Requests))
}
get := f.Requests[0]
if get.Name != "getUser" || get.Method != "GET" || get.URL != "{{baseUrl}}/users/1" {
t.Errorf("first request = %+v", get)
}
if got := get.Headers.Get("Authorization"); got != "Bearer {{token}}" {
t.Errorf("Authorization = %q, want %q", got, "Bearer {{token}}")
}
if get.Line != 5 {
t.Errorf("first request Line = %d, want 5", get.Line)
}
post := f.Requests[1]
if post.Name != "createUser" || post.Method != "POST" || post.URL != "{{baseUrl}}/users" {
t.Errorf("second request = %+v", post)
}
if !strings.Contains(string(post.Body), "\"token\": \"{{token}}\"") {
t.Errorf("second request body = %q", post.Body)
}
}