Skip to content

Commit bfd1ce6

Browse files
committed
Add configurable default headers support
1 parent 6657805 commit bfd1ce6

12 files changed

Lines changed: 663 additions & 51 deletions

File tree

‎CHANGELOG-4.0.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
### Added
99
- PHP 8.4 support
1010
- PHP 8.5 support
11+
- Configurable default headers support via `headers` module configuration
12+
- Improved test coverage with unit and functional tests
1113

1214
### Changed
1315
- Upgraded PHPStan to v2

‎README.md‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ modules:
5151
```
5252
5353
The `application` property is a relative path to file which returns your `Slim\App` instance.
54+
55+
You can also configure default headers that will be sent with every request using the `headers` option:
56+
57+
```yaml
58+
modules:
59+
enabled:
60+
- REST:
61+
depends: DoclerLabs\CodeceptionSlimModule\Module\Slim
62+
63+
config:
64+
DoclerLabs\CodeceptionSlimModule\Module\Slim:
65+
application: path/to/application.php
66+
headers:
67+
Content-Type: application/json
68+
Accept: application/json
69+
```
70+
5471
Here is the minimum `application.php` content:
5572

5673
```php
@@ -68,7 +85,6 @@ return $app;
6885
## Testing your API endpoints
6986

7087
```php
71-
7288
class UserCest
7389
{
7490
public function getUserReturnsWithEmail(FunctionalTester $I): void
@@ -86,3 +102,32 @@ class UserCest
86102
}
87103
}
88104
```
105+
106+
### With default headers
107+
108+
When you configure default `headers` in your suite configuration, you no longer need to set them manually in each test:
109+
110+
```php
111+
class UserCest
112+
{
113+
// Content-Type and Accept headers are already set via module config
114+
public function getUserReturnsWithEmail(FunctionalTester $I): void
115+
{
116+
$I->sendGET('/users/John');
117+
118+
$I->seeResponseCodeIs(200);
119+
$I->seeResponseContainsJson(
120+
[
121+
'email' => 'john.doe@example.com',
122+
]
123+
);
124+
}
125+
126+
public function createUserReturnsCreated(FunctionalTester $I): void
127+
{
128+
$I->sendPOST('/users', ['name' => 'Jane', 'email' => 'jane.doe@example.com']);
129+
130+
$I->seeResponseCodeIs(201);
131+
}
132+
}
133+
```

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"autoload-dev": {
3030
"psr-4": {
3131
"DoclerLabs\\CodeceptionSlimModule\\Test\\": "test/support",
32-
"DoclerLabs\\CodeceptionSlimModule\\Test\\Functional\\": "test/suite/functional"
32+
"DoclerLabs\\CodeceptionSlimModule\\Test\\Functional\\": "test/suite/functional",
33+
"DoclerLabs\\CodeceptionSlimModule\\Test\\Unit\\": "test/suite/unit"
3334
}
3435
},
3536
"config": {

‎src/Module/Slim.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
* ### Slim 4.x
2222
*
2323
* * application - Relative path to file which bootstrap and returns your `Slim\App` instance.
24+
* * headers - Default headers to be sent with every request (optional).
2425
*
2526
* #### Example (`test/suite/functional.suite.yml`)
2627
* ```yaml
2728
* modules:
2829
* config:
2930
* DoclerLabs\CodeceptionSlimModule\Module\Slim:
3031
* application: 'app/bootstrap.php'
32+
* headers:
33+
* Content-Type: application/json
3134
* ```
3235
*
3336
* ## Public Properties
@@ -46,6 +49,8 @@
4649
* config:
4750
* DoclerLabs\CodeceptionSlimModule\Module\Slim:
4851
* application: 'app/bootstrap.php'
52+
* headers:
53+
* Content-Type: application/json
4954
* ```
5055
*/
5156
class Slim extends Framework
@@ -55,6 +60,8 @@ class Slim extends Framework
5560

5661
protected array $requiredFields = ['application'];
5762

63+
protected array $config = ['headers' => []];
64+
5865
private string $applicationPath;
5966

6067
public function _initialize(): void
@@ -95,6 +102,10 @@ public function _before(TestInterface $test): void
95102

96103
$this->client = $connector;
97104

105+
/** @var array<string, string> $headers */
106+
$headers = $this->config['headers'];
107+
$this->headers = $headers;
108+
98109
parent::_before($test);
99110
}
100111
}

‎test/suite/functional.suite.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ modules:
88
config:
99
DoclerLabs\CodeceptionSlimModule\Module\Slim:
1010
application: test/support/Fake/application.php
11+
headers:
12+
X-Default-Header: default-value

‎test/suite/functional/TestAppCest.php‎

Lines changed: 164 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function convertGetRequest(FunctionalTester $I): void
1414
$I->haveHttpHeader('Cookie', 'name=value; name2=value2; name3=value3');
1515
$I->haveServerParameter('custom', 'server');
1616

17-
$I->sendGET('/hello/John/Doe', ['foo' => 'bar']);
17+
$I->sendGet('/hello/John/Doe', ['foo' => 'bar']);
1818

1919
$I->seeResponseCodeIs(200);
2020
$response = json_decode($I->grabResponse(), true);
@@ -44,10 +44,11 @@ public function convertGetRequest(FunctionalTester $I): void
4444
// Check headers.
4545
$I->assertSame(
4646
[
47-
'User-Agent' => ['Symfony BrowserKit'],
48-
'Custom' => ['header'],
49-
'Cookie' => ['name=value; name2=value2; name3=value3'],
50-
'Host' => ['localhost'],
47+
'User-Agent' => ['Symfony BrowserKit'],
48+
'X-Default-Header' => ['default-value'],
49+
'Custom' => ['header'],
50+
'Cookie' => ['name=value; name2=value2; name3=value3'],
51+
'Host' => ['localhost'],
5152
],
5253
$response['headers'],
5354
'Header parameters are not identical.'
@@ -74,7 +75,7 @@ public function convertPostMultipartFormDataRequest(FunctionalTester $I): void
7475
$I->haveHttpHeader('Cookie', 'name=value; name2=value2; name3=value3');
7576
$I->haveServerParameter('custom', 'server');
7677

77-
$I->sendPOST('/hello/John/Doe?query=value', ['foo' => 'bar']);
78+
$I->sendPost('/hello/John/Doe?query=value', ['foo' => 'bar']);
7879

7980
$I->seeResponseCodeIs(200);
8081
$response = json_decode($I->grabResponse(), true);
@@ -104,10 +105,11 @@ public function convertPostMultipartFormDataRequest(FunctionalTester $I): void
104105
// Check headers.
105106
$I->assertSame(
106107
[
107-
'User-Agent' => ['Symfony BrowserKit'],
108-
'Content-Type' => ['multipart/form-data'],
109-
'Cookie' => ['name=value; name2=value2; name3=value3'],
110-
'Host' => ['localhost'],
108+
'User-Agent' => ['Symfony BrowserKit'],
109+
'X-Default-Header' => ['default-value'],
110+
'Content-Type' => ['multipart/form-data'],
111+
'Cookie' => ['name=value; name2=value2; name3=value3'],
112+
'Host' => ['localhost'],
111113
],
112114
$response['headers'],
113115
'Header parameters are not identical.'
@@ -134,7 +136,7 @@ public function convertPostJsonRequest(FunctionalTester $I): void
134136
$I->haveHttpHeader('Cookie', 'name=value; name2=value2; name3=value3');
135137
$I->haveServerParameter('custom', 'server');
136138

137-
$I->sendPOST('/hello/John/Doe?query=value', ['foo' => 'bar']);
139+
$I->sendPost('/hello/John/Doe?query=value', ['foo' => 'bar']);
138140

139141
$I->seeResponseCodeIs(200);
140142
$response = json_decode($I->grabResponse(), true);
@@ -164,10 +166,11 @@ public function convertPostJsonRequest(FunctionalTester $I): void
164166
// Check headers.
165167
$I->assertSame(
166168
[
167-
'User-Agent' => ['Symfony BrowserKit'],
168-
'Content-Type' => ['application/json'],
169-
'Cookie' => ['name=value; name2=value2; name3=value3'],
170-
'Host' => ['localhost'],
169+
'User-Agent' => ['Symfony BrowserKit'],
170+
'X-Default-Header' => ['default-value'],
171+
'Content-Type' => ['application/json'],
172+
'Cookie' => ['name=value; name2=value2; name3=value3'],
173+
'Host' => ['localhost'],
171174
],
172175
$response['headers'],
173176
'Header parameters are not identical.'
@@ -187,4 +190,150 @@ public function convertPostJsonRequest(FunctionalTester $I): void
187190
// Check uploaded files.
188191
$I->assertSame([], $response['uploaded_files'], 'Uploaded file parameters are not identical.');
189192
}
193+
194+
public function convertPutRequest(FunctionalTester $I): void
195+
{
196+
$I->haveHttpHeader('Content-type', 'application/json');
197+
198+
$I->sendPut('/echo', ['foo' => 'bar']);
199+
200+
$I->seeResponseCodeIs(200);
201+
$response = json_decode($I->grabResponse(), true);
202+
203+
$I->assertSame('PUT', $response['method'], 'Method is not identical.');
204+
$I->assertSame('{"foo":"bar"}', $response['body'], 'Request body is not identical.');
205+
$I->assertSame(['foo' => 'bar'], $response['parsed_body'], 'Parsed request body is not identical.');
206+
}
207+
208+
public function convertPatchRequest(FunctionalTester $I): void
209+
{
210+
$I->haveHttpHeader('Content-type', 'application/json');
211+
212+
$I->sendPatch('/echo', ['key' => 'value']);
213+
214+
$I->seeResponseCodeIs(200);
215+
$response = json_decode($I->grabResponse(), true);
216+
217+
$I->assertSame('PATCH', $response['method'], 'Method is not identical.');
218+
$I->assertSame('{"key":"value"}', $response['body'], 'Request body is not identical.');
219+
$I->assertSame(['key' => 'value'], $response['parsed_body'], 'Parsed request body is not identical.');
220+
}
221+
222+
public function convertDeleteRequest(FunctionalTester $I): void
223+
{
224+
$I->sendDelete('/echo');
225+
226+
$I->seeResponseCodeIs(200);
227+
$response = json_decode($I->grabResponse(), true);
228+
229+
$I->assertSame('DELETE', $response['method'], 'Method is not identical.');
230+
}
231+
232+
public function convertPostWithFileUpload(FunctionalTester $I): void
233+
{
234+
$tmpFile = tempnam(sys_get_temp_dir(), 'test_upload_');
235+
file_put_contents($tmpFile, 'file content here');
236+
237+
$I->haveHttpHeader('Content-type', 'multipart/form-data');
238+
$I->sendPost(
239+
'/upload',
240+
[],
241+
[
242+
'attachment' => [
243+
'tmp_name' => $tmpFile,
244+
'name' => 'test.txt',
245+
'type' => 'text/plain',
246+
'size' => filesize($tmpFile),
247+
'error' => UPLOAD_ERR_OK,
248+
],
249+
]
250+
);
251+
252+
$I->seeResponseCodeIs(200);
253+
$response = json_decode($I->grabResponse(), true);
254+
255+
$I->assertSame('POST', $response['method'], 'Method is not identical.');
256+
$I->assertCount(1, $response['uploaded_files'], 'Expected one uploaded file.');
257+
$I->assertSame('test.txt', $response['uploaded_files']['attachment']['filename']);
258+
$I->assertSame('text/plain', $response['uploaded_files']['attachment']['media_type']);
259+
$I->assertSame('file content here', $response['uploaded_files']['attachment']['content']);
260+
$I->assertSame(UPLOAD_ERR_OK, $response['uploaded_files']['attachment']['error']);
261+
262+
@unlink($tmpFile);
263+
}
264+
265+
public function handleNon200StatusCode(FunctionalTester $I): void
266+
{
267+
$I->sendGet('/status/404');
268+
269+
$I->seeResponseCodeIs(404);
270+
}
271+
272+
public function handleServerErrorStatusCode(FunctionalTester $I): void
273+
{
274+
$I->sendGet('/status/500');
275+
276+
$I->seeResponseCodeIs(500);
277+
}
278+
279+
public function handleRedirectResponse(FunctionalTester $I): void
280+
{
281+
$I->stopFollowingRedirects();
282+
283+
$I->sendGet('/redirect');
284+
285+
$I->seeResponseCodeIs(302);
286+
$I->seeHttpHeader('Location', '/hello/John/Doe');
287+
288+
$I->startFollowingRedirects();
289+
}
290+
291+
public function handleEmptyResponse(FunctionalTester $I): void
292+
{
293+
$I->sendGet('/empty');
294+
295+
$I->seeResponseCodeIs(204);
296+
$I->assertSame('', $I->grabResponse(), 'Response body should be empty.');
297+
}
298+
299+
public function convertMinimalGetRequest(FunctionalTester $I): void
300+
{
301+
$I->sendGet('/echo');
302+
303+
$I->seeResponseCodeIs(200);
304+
$response = json_decode($I->grabResponse(), true);
305+
306+
$I->assertSame('GET', $response['method'], 'Method is not identical.');
307+
$I->assertSame('http://localhost/echo', $response['uri'], 'Uri is not identical.');
308+
$I->assertSame([], $response['query_params'], 'Query parameters should be empty.');
309+
$I->assertSame([], $response['cookie_params'], 'Cookie parameters should be empty.');
310+
$I->assertNull($response['parsed_body'], 'Parsed body should be null.');
311+
}
312+
313+
public function convertPostWithEmptyBody(FunctionalTester $I): void
314+
{
315+
$I->haveHttpHeader('Content-type', 'application/json');
316+
317+
$I->sendPost('/echo', '');
318+
319+
$I->seeResponseCodeIs(200);
320+
$response = json_decode($I->grabResponse(), true);
321+
322+
$I->assertSame('POST', $response['method'], 'Method is not identical.');
323+
$I->assertSame('', $response['body'], 'Request body should be empty.');
324+
}
325+
326+
public function configHeadersAreSentWithEveryRequest(FunctionalTester $I): void
327+
{
328+
$I->sendGet('/echo');
329+
330+
$I->seeResponseCodeIs(200);
331+
$response = json_decode($I->grabResponse(), true);
332+
333+
$I->assertSame(
334+
'default-value',
335+
$response['headers']['X-Default-Header'][0] ?? null,
336+
'Default header from config should be present.'
337+
);
338+
}
190339
}

‎test/suite/unit.suite.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
actor: UnitTester
2+
modules:
3+
enabled:
4+
- Asserts:

0 commit comments

Comments
 (0)