forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KarateHttpMockHandlerTest.java
223 lines (200 loc) · 6.74 KB
/
KarateHttpMockHandlerTest.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.intuit.karate.core;
import static com.intuit.karate.TestUtils.*;
import static com.intuit.karate.TestUtils.runScenario;
import com.intuit.karate.http.HttpServer;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
class KarateHttpMockHandlerTest {
static final Logger logger = LoggerFactory.getLogger(KarateHttpMockHandlerTest.class);
MockHandler handler;
HttpServer server;
FeatureBuilder mock;
ScenarioRuntime runtime;
String urlStep() {
return "url 'http://localhost:" + server.getPort() + "'";
}
void startMockServer() {
handler = new MockHandler(mock.build());
server = HttpServer.handler(handler).build();
}
FeatureBuilder background(String... lines) {
mock = FeatureBuilder.background(lines);
return mock;
}
Object get(String name) {
return runtime.engine.vars.get(name).getValue();
}
ScenarioRuntime run(String... lines) {
runtime = runScenario(null, lines);
return runtime;
}
private void matchVar(String name, Object expected) {
match(get(name), expected);
}
private void matchVarContains(String name, Object expected) {
matchContains(get(name), expected);
}
@AfterEach
void afterEach() {
server.stop();
}
@Test
void testSimpleGet() {
background().scenario(
"pathMatches('/hello')",
"def response = 'hello world'");
startMockServer();
run(
urlStep(),
"path 'hello'",
"method get"
);
matchVar("response", "hello world");
}
@Test
void testThatCookieIsPartOfRequest() {
background().scenario(
"pathMatches('/hello')",
"def response = requestHeaders");
startMockServer();
run(
urlStep(),
"path 'hello'",
"cookie foo = 'bar'",
"method get"
);
matchVarContains("response", "{ cookie: ['foo=bar'] }");
}
@Test
void testSameSiteSecureCookieRequest() {
background().scenario(
"pathMatches('/hello')",
"def response = requestHeaders");
startMockServer();
run(
urlStep(),
"path 'hello'",
"cookie foo = { value: 'bar', samesite: 'Strict', secure: true }",
"method get"
);
matchVarContains("response", "{ cookie: ['foo=bar'] }");
}
@Test
void testSameSiteSecureCookieResponse() {
background().scenario(
"pathMatches('/hello')",
"def responseHeaders = { 'Set-Cookie': 'foo=bar; expires=Wed, 30-Dec-20 09:25:45 GMT; path=/; domain=.example.com; HttpOnly; SameSite=Lax; Secure' }");
startMockServer();
run(
urlStep(),
"path 'hello'",
"method get"
);
matchVarContains("responseHeaders", "{ set-cookie: ['foo=bar; expires=Wed, 30-Dec-20 09:25:45 GMT; path=/; domain=.example.com; HttpOnly; SameSite=Lax; Secure'] }");
}
@Test
void testThatExoticContentTypeIsPreserved() {
background().scenario(
"pathMatches('/hello')",
"def response = requestHeaders");
startMockServer();
run(
urlStep(),
"path 'hello'",
"header Content-Type = 'application/xxx.pingixxxxxx.checkUsernamePassword+json'",
"method post"
);
matchVarContains("response", "{ 'content-type': ['application/xxx.pingixxxxxx.checkUsernamePassword+json'] }");
}
@Test
void testInspectRequestInHeadersFunction() {
background().scenario(
"pathMatches('/hello')",
"def response = requestHeaders");
startMockServer();
run(
urlStep(),
"configure headers = function(request){ return { 'api-key': request.bodyAsString } }",
"path 'hello'",
"request 'some text'",
"method post"
);
matchVarContains("response", "{ 'api-key': ['some text'] }");
}
@Test
void testKarateRemove() {
background().scenario(
"pathMatches('/hello/{id}')",
"def temp = { '1': 'foo', '2': 'bar' }",
"karate.remove('temp', pathParams.id)",
"def response = temp");
startMockServer();
run(
urlStep(),
"path 'hello', '1'",
"method get"
);
matchVarContains("response", "{ '2': 'bar' }");
}
@Test
void testTransferEncoding() {
background().scenario(
"pathMatches('/hello')",
"def response = request");
startMockServer();
run(
urlStep(),
"path 'hello'",
"header Transfer-Encoding = 'chunked'",
"request { foo: 'bar' }",
"method post"
);
matchVarContains("response", "{ foo: 'bar' }");
}
@Test
void testMalformedMockResponse() {
background().scenario(
"pathMatches('/hello')",
"def response = '{ \"id\" \"123\" }'");
startMockServer();
run(
urlStep(),
"path 'hello'",
"method get",
"match response == '{ \"id\" \"123\" }'",
"match responseType == 'string'"
);
Object response = get("response");
assertEquals(response, "{ \"id\" \"123\" }");
}
@Test
void testRedirectAfterPostWithCookie() {
background()
.scenario("pathMatches('/first')",
"def responseHeaders = { 'Set-Cookie': 'foo1=bar1', Location: '/second' }",
"def responseStatus = 302")
.scenario("pathMatches('/second')",
"def response = requestHeaders",
"def responseHeaders = { 'Set-Cookie': 'foo2=bar2' }");
startMockServer();
run(
urlStep(),
"path 'first'",
"form fields { username: 'blah', password: 'blah' }",
"method post"
);
matchVarContains("response", "{ cookie: ['foo1=bar1'] }");
Map<String, Object> map = (Map) get("responseHeaders");
List<String> list = (List) map.get("Set-Cookie");
matchContains(list, "['foo1=bar1; Domain=localhost', 'foo2=bar2; Domain=localhost']");
}
}