-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_test.go
More file actions
167 lines (162 loc) · 4.38 KB
/
Copy pathscript_test.go
File metadata and controls
167 lines (162 loc) · 4.38 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
package httpparser
import (
"errors"
"reflect"
"testing"
)
func TestParseScripts(t *testing.T) {
tests := []struct {
name string
input string
wantPre *Script
wantResp *Script
wantBody string // "" means nil body
wantMethod string
}{
{
name: "inline pre-request script",
input: "< {% request.variables.set(\"id\", \"42\") %}\n" +
"GET https://example.com/x\n",
wantPre: &Script{Source: `request.variables.set("id", "42")`, Line: 1},
wantMethod: "GET",
},
{
name: "multi-line inline pre-request script",
input: "< {%\n" +
" const t = Date.now();\n" +
" request.variables.set(\"t\", t);\n" +
"%}\n" +
"GET https://example.com/x\n",
wantPre: &Script{
Source: "const t = Date.now();\n request.variables.set(\"t\", t);",
Line: 1,
},
wantMethod: "GET",
},
{
name: "external pre-request script",
input: "< ./scripts/pre.js\n" +
"GET https://example.com/x\n",
wantPre: &Script{Source: "./scripts/pre.js", External: true, Line: 1},
wantMethod: "GET",
},
{
name: "external pre-request absolute path",
input: "< /abs/path/prerequest.js\n" +
"POST https://example.com/x\n",
wantPre: &Script{Source: "/abs/path/prerequest.js", External: true, Line: 1},
wantMethod: "POST",
},
{
name: "inline response handler",
input: "GET https://example.com/x\n" +
"\n" +
"> {% client.test(\"ok\", () => {}) %}\n",
wantResp: &Script{Source: `client.test("ok", () => {})`, Line: 3},
wantMethod: "GET",
},
{
name: "multi-line inline response handler",
input: "GET https://example.com/x\n" +
"\n" +
"> {%\n" +
" client.test(\"status\", () => {\n" +
" client.assert(response.status === 200);\n" +
" });\n" +
"%}\n",
wantResp: &Script{
Source: "client.test(\"status\", () => {\n client.assert(response.status === 200);\n });",
Line: 3,
},
wantMethod: "GET",
},
{
name: "external response handler",
input: "GET https://example.com/x\n" +
"\n" +
"> ./handler.js\n",
wantResp: &Script{Source: "./handler.js", External: true, Line: 3},
wantMethod: "GET",
},
{
name: "response handler after body",
input: "POST https://example.com/x\n" +
"Content-Type: application/json\n" +
"\n" +
"{\"a\": 1}\n" +
"\n" +
"> {% client.log(response.body) %}\n",
wantResp: &Script{Source: "client.log(response.body)", Line: 6},
wantBody: `{"a": 1}`,
wantMethod: "POST",
},
{
name: "both pre-request and response scripts",
input: "< {% request.variables.set(\"k\", 1) %}\n" +
"# @name both\n" +
"GET https://example.com/x\n" +
"\n" +
"> {% client.test(\"t\", () => {}) %}\n",
wantPre: &Script{Source: `request.variables.set("k", 1)`, Line: 1},
wantResp: &Script{Source: `client.test("t", () => {})`, Line: 5},
wantMethod: "GET",
},
}
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)
}
if len(f.Requests) != 1 {
t.Fatalf("got %d requests, want 1", len(f.Requests))
}
req := f.Requests[0]
if req.Method != tt.wantMethod {
t.Errorf("Method = %q, want %q", req.Method, tt.wantMethod)
}
if !reflect.DeepEqual(req.PreScript, tt.wantPre) {
t.Errorf("PreScript = %#v, want %#v", req.PreScript, tt.wantPre)
}
if !reflect.DeepEqual(req.ResponseScript, tt.wantResp) {
t.Errorf("ResponseScript = %#v, want %#v", req.ResponseScript, tt.wantResp)
}
gotBody := string(req.Body)
if gotBody != tt.wantBody {
t.Errorf("Body = %q, want %q", gotBody, tt.wantBody)
}
})
}
}
func TestParseScriptErrors(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "unclosed inline pre-request block",
input: "< {% never closed\nGET https://example.com/x\n",
},
{
name: "empty external pre-request reference",
input: "<\nGET https://example.com/x\n",
},
{
name: "unclosed inline response block",
input: "GET https://example.com/x\n" +
"\n" +
"> {% never closed\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseString(tt.input)
if err == nil {
t.Fatal("expected an error, got nil")
}
if !errors.Is(err, ErrMalformedScript) {
t.Errorf("errors.Is(err, ErrMalformedScript) = false; err = %v", err)
}
})
}
}