forked from Codeception/Codeception
/
RestTest.php
314 lines (276 loc) · 13.3 KB
/
RestTest.php
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
use Codeception\Util\Stub as Stub;
/**
* Class RestTest
* @group appveyor
*/
class RestTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Codeception\Module\REST
*/
protected $module;
public function setUp()
{
$connector = new \Codeception\Lib\Connector\Universal();
$connector->setIndex(\Codeception\Configuration::dataDir() . '/rest/index.php');
$connectionModule = new \Codeception\Module\UniversalFramework(make_container());
$connectionModule->client = $connector;
$connectionModule->_initialize();
$this->module = Stub::make('\Codeception\Module\REST');
$this->module->_inject($connectionModule);
$this->module->_initialize();
$this->module->_before(Stub::makeEmpty('\Codeception\TestCase\Cest'));
$this->module->client->setServerParameters([
'SCRIPT_FILENAME' => 'index.php',
'SCRIPT_NAME' => 'index',
'SERVER_NAME' => 'localhost',
'SERVER_PROTOCOL' => 'http'
]);
}
private function setStubResponse($response)
{
$connectionModule = Stub::make('\Codeception\Module\UniversalFramework', ['_getResponseContent' => $response]);
$this->module->_inject($connectionModule);
$this->module->_initialize();
$this->module->_before(Stub::makeEmpty('\Codeception\TestCase\Cest'));
}
public function testBeforeHookResetsVariables()
{
$this->module->haveHttpHeader('Origin','http://www.example.com');
$this->module->sendGET('/rest/user/');
$this->assertEquals(
'http://www.example.com',
$this->module->client->getServerParameter('HTTP_ORIGIN')
);
$this->module->_before(Stub::makeEmpty('\Codeception\TestCase\Cest'));
$this->assertNull($this->module->client->getServerParameter('HTTP_ORIGIN', null));
}
public function testGet()
{
$this->module->sendGET('/rest/user/');
$this->module->seeResponseIsJson();
$this->module->seeResponseContains('davert');
$this->module->seeResponseContainsJson(['name' => 'davert']);
$this->module->seeResponseCodeIs(200);
$this->module->dontSeeResponseCodeIs(404);
}
public function testPost()
{
$this->module->sendPOST('/rest/user/', ['name' => 'john']);
$this->module->seeResponseContains('john');
$this->module->seeResponseContainsJson(['name' => 'john']);
}
public function testPut()
{
$this->module->sendPUT('/rest/user/', ['name' => 'laura']);
$this->module->seeResponseContains('davert@mail.ua');
$this->module->seeResponseContainsJson(['name' => 'laura']);
$this->module->dontSeeResponseContainsJson(['name' => 'john']);
}
public function testGrabDataFromResponseByJsonPath()
{
$this->module->sendGET('/rest/user/');
// simple assoc array
$this->assertEquals(['davert@mail.ua'], $this->module->grabDataFromResponseByJsonPath('$.email'));
// nested assoc array
$this->assertEquals(['Kyiv'], $this->module->grabDataFromResponseByJsonPath('$.address.city'));
// nested index array
$this->assertEquals(['DavertMik'], $this->module->grabDataFromResponseByJsonPath('$.aliases[0]'));
// empty if data not found
$this->assertEquals([], $this->module->grabDataFromResponseByJsonPath('$.address.street'));
}
public function testValidJson()
{
$this->setStubResponse('{"xxx": "yyy"}');
$this->module->seeResponseIsJson();
$this->setStubResponse('{"xxx": "yyy", "zzz": ["a","b"]}');
$this->module->seeResponseIsJson();
$this->module->seeResponseEquals('{"xxx": "yyy", "zzz": ["a","b"]}');
}
public function testInvalidJson()
{
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->setStubResponse('{xxx = yyy}');
$this->module->seeResponseIsJson();
}
public function testValidXml()
{
$this->setStubResponse('<xml></xml>');
$this->module->seeResponseIsXml();
$this->setStubResponse('<xml><name>John</name></xml>');
$this->module->seeResponseIsXml();
$this->module->seeResponseEquals('<xml><name>John</name></xml>');
}
public function testInvalidXml()
{
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->setStubResponse('<xml><name>John</surname></xml>');
$this->module->seeResponseIsXml();
}
public function testSeeInJsonResponse()
{
$this->setStubResponse('{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}, "labels": null}}');
$this->module->seeResponseIsJson();
$this->module->seeResponseContainsJson(['name' => 'Davert']);
$this->module->seeResponseContainsJson(['user' => ['name' => 'Davert']]);
$this->module->seeResponseContainsJson(['ticket' => ['title' => 'Bug should be fixed']]);
$this->module->seeResponseContainsJson(['ticket' => ['user' => ['name' => 'Davert']]]);
$this->module->seeResponseContainsJson(['ticket' => ['labels' => null]]);
}
public function testSeeInJsonCollection()
{
$this->setStubResponse('[{"user":"Blacknoir","age":"42","tags":["wed-dev","php"]},{"user":"John Doe","age":27,"tags":["web-dev","java"]}]');
$this->module->seeResponseIsJson();
$this->module->seeResponseContainsJson(['tags' => ['web-dev', 'java']]);
$this->module->seeResponseContainsJson(['user' => 'John Doe', 'age' => 27]);
$this->module->seeResponseContainsJson([['user' => 'John Doe', 'age' => 27]]);
$this->module->seeResponseContainsJson([['user' => 'Blacknoir', 'age' => 42], ['user' => 'John Doe', 'age' => "27"]]);
}
public function testArrayJson()
{
$this->setStubResponse('[{"id":1,"title": "Bug should be fixed"},{"title": "Feature should be implemented","id":2}]');
$this->module->seeResponseContainsJson(['id' => 1]);
}
public function testDontSeeInJson()
{
$this->setStubResponse('{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}}}');
$this->module->seeResponseIsJson();
$this->module->dontSeeResponseContainsJson(['name' => 'Davet']);
$this->module->dontSeeResponseContainsJson(['user' => ['name' => 'Davet']]);
$this->module->dontSeeResponseContainsJson(['user' => ['title' => 'Bug should be fixed']]);
}
public function testApplicationJsonIncludesJsonAsContent()
{
$this->module->haveHttpHeader('Content-Type', 'application/json');
$this->module->sendPOST('/', ['name' => 'john']);
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertContains('application/json', $request->getServer());
$server = $request->getServer();
$this->assertEquals('application/json', $server['HTTP_CONTENT_TYPE']);
$this->assertJson($request->getContent());
$this->assertEmpty($request->getParameters());
}
public function testApplicationJsonIncludesObjectSerialized()
{
$this->module->haveHttpHeader('Content-Type', 'application/json');
$this->module->sendPOST('/', new JsonSerializedItem());
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertContains('application/json', $request->getServer());
$this->assertJson($request->getContent());
}
public function testGetApplicationJsonNotIncludesJsonAsContent()
{
$this->module->haveHttpHeader('Content-Type', 'application/json');
$this->module->sendGET('/', ['name' => 'john']);
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertNull($request->getContent());
$this->assertContains('john', $request->getParameters());
}
public function testUrlIsFull()
{
$this->module->sendGET('/api/v1/users');
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertEquals('http://localhost/api/v1/users',$request->getUri());
}
public function testSeeHeaders()
{
$response = new \Symfony\Component\BrowserKit\Response("", 200, [
'Cache-Control' => ['no-cache', 'no-store'],
'Content_Language' => 'en-US'
]);
$this->module->client->mockResponse($response);
$this->module->sendGET('/');
$this->module->seeHttpHeader('Cache-Control');
$this->module->seeHttpHeader('content_language','en-US');
$this->module->seeHttpHeader('Content-Language','en-US');
$this->module->dontSeeHttpHeader('Content-Language','en-RU');
$this->module->dontSeeHttpHeader('Content-Language1');
$this->module->seeHttpHeaderOnce('Content-Language');
$this->assertEquals('en-US', $this->module->grabHttpHeader('Content-Language'));
$this->assertEquals('no-cache', $this->module->grabHttpHeader('Cache-Control'));
$this->assertEquals(['no-cache', 'no-store'], $this->module->grabHttpHeader('Cache-Control', false));
}
public function testSeeHeadersOnce()
{
$this->shouldFail();
$response = new \Symfony\Component\BrowserKit\Response("", 200, [
'Cache-Control' => ['no-cache', 'no-store'],
]);
$this->module->client->mockResponse($response);
$this->module->sendGET('/');
$this->module->seeHttpHeaderOnce('Cache-Control');
}
public function testArrayJsonPathAndXPath()
{
$this->setStubResponse('[{"user":"Blacknoir","age":27,"tags":["wed-dev","php"]},{"user":"John Doe","age":27,"tags":["web-dev","java"]}]');
$this->module->seeResponseIsJson();
$this->module->seeResponseJsonMatchesXpath('//user');
$this->module->seeResponseJsonMatchesJsonPath('$[*].user');
$this->module->seeResponseJsonMatchesJsonPath('$[1].tags');
$this->module->dontSeeResponseJsonMatchesJsonPath('$[*].profile');
}
/**
* @Issue https://github.com/Codeception/Codeception/issues/2775
*/
public function testSeeResponseJsonMatchesXPathWorksWithAmpersand()
{
$this->setStubResponse('{ "product":[ { "category":[ { "comment":"something & something" } ] } ] }');
$this->module->seeResponseIsJson();
$this->module->seeResponseJsonMatchesXpath('//comment');
}
public function testArrayJsonPathFails()
{
$this->shouldFail();
$this->setStubResponse('[{"user":"Blacknoir","age":27,"tags":["wed-dev","php"]},{"user":"John Doe","age":27,"tags":["web-dev","java"]}]');
$this->module->seeResponseIsJson();
$this->module->seeResponseJsonMatchesJsonPath('$[*].profile');
}
public function testStructuredJsonPathAndXPath()
{
$this->setStubResponse('{ "store": {"book": [{ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": {"color": "red", "price": 19.95 } } }');
$this->module->seeResponseIsJson();
$this->module->seeResponseJsonMatchesXpath('//book/category');
$this->module->seeResponseJsonMatchesJsonPath('$..book');
$this->module->seeResponseJsonMatchesJsonPath('$.store.book[2].author');
$this->module->dontSeeResponseJsonMatchesJsonPath('$.invalid');
$this->module->dontSeeResponseJsonMatchesJsonPath('$.store.book.*.invalidField');
}
public function testApplicationJsonSubtypeIncludesObjectSerialized()
{
$this->module->haveHttpHeader('Content-Type', 'application/resource+json');
$this->module->sendPOST('/', new JsonSerializedItem());
/** @var $request \Symfony\Component\BrowserKit\Request **/
$request = $this->module->client->getRequest();
$this->assertContains('application/resource+json', $request->getServer());
$this->assertJson($request->getContent());
}
public function testJsonTypeMatches()
{
$this->setStubResponse('{"xxx": "yyy", "user_id": 1}');
$this->module->seeResponseMatchesJsonType(['xxx' => 'string', 'user_id' => 'integer:<10']);
$this->module->dontSeeResponseMatchesJsonType(['xxx' => 'integer', 'user_id' => 'integer:<10']);
}
public function testJsonTypeMatchesWithJsonPath()
{
$this->setStubResponse('{"users": [{ "name": "davert"}, {"id": 1}]}');
$this->module->seeResponseMatchesJsonType(['name' => 'string'], '$.users[0]');
$this->module->seeResponseMatchesJsonType(['id' => 'integer'], '$.users[1]');
$this->module->dontSeeResponseMatchesJsonType(['id' => 'integer'], '$.users[0]');
}
protected function shouldFail()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
}
}
class JsonSerializedItem implements JsonSerializable
{
public function jsonSerialize()
{
return array("hello" => "world");
}
}