Skip to content

Commit 1a7fa9e

Browse files
committed
Close #132
1 parent 392db49 commit 1a7fa9e

File tree

3 files changed

+121
-21
lines changed

3 files changed

+121
-21
lines changed

src/Contracts/Http/ResponsesInterface.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,49 +46,59 @@ interface ResponsesInterface
4646
* @param int $statusCode
4747
* @param array|null $links
4848
* @param mixed $meta
49+
* @param array $headers
4950
*
5051
* @return mixed
5152
*/
52-
public function getContentResponse($data, $statusCode = self::HTTP_OK, $links = null, $meta = null);
53+
public function getContentResponse(
54+
$data,
55+
$statusCode = self::HTTP_OK,
56+
$links = null,
57+
$meta = null,
58+
array $headers = []
59+
);
5360

5461
/**
5562
* Get response for newly created resource with HTTP code 201 (adds 'location' header).
5663
*
5764
* @param object $resource
5865
* @param array|null $links
5966
* @param mixed $meta
67+
* @param array $headers
6068
*
6169
* @return mixed
6270
*/
63-
public function getCreatedResponse($resource, $links = null, $meta = null);
64-
71+
public function getCreatedResponse($resource, $links = null, $meta = null, array $headers = []);
6572

6673
/**
6774
* Get response with HTTP code only.
6875
*
69-
* @param $statusCode
76+
* @param int $statusCode
77+
* @param array $headers
7078
*
7179
* @return mixed
7280
*/
73-
public function getCodeResponse($statusCode);
81+
public function getCodeResponse($statusCode, array $headers = []);
7482

7583
/**
7684
* Get response with meta information only.
7785
*
7886
* @param array|object $meta Meta information.
7987
* @param int $statusCode
88+
* @param array $headers
8089
*
8190
* @return mixed
8291
*/
83-
public function getMetaResponse($meta, $statusCode = self::HTTP_OK);
92+
public function getMetaResponse($meta, $statusCode = self::HTTP_OK, array $headers = []);
8493

8594
/**
8695
* Get response with JSON API Error in body.
8796
*
8897
* @param Error|Error[]|ErrorCollection $errors
8998
* @param int $statusCode
99+
* @param array $headers
90100
*
91101
* @return mixed
92102
*/
93-
public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST);
103+
public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST, array $headers = []);
94104
}

src/Http/Responses.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,53 +81,58 @@ abstract protected function getMediaType();
8181
/**
8282
* @inheritdoc
8383
*/
84-
public function getContentResponse($data, $statusCode = self::HTTP_OK, $links = null, $meta = null)
85-
{
84+
public function getContentResponse(
85+
$data,
86+
$statusCode = self::HTTP_OK,
87+
$links = null,
88+
$meta = null,
89+
array $headers = []
90+
) {
8691
$encoder = $this->getEncoder();
8792
$links === null ?: $encoder->withLinks($links);
8893
$meta === null ?: $encoder->withMeta($meta);
8994
$content = $encoder->encodeData($data, $this->getEncodingParameters());
9095

91-
return $this->createJsonApiResponse($content, $statusCode);
96+
return $this->createJsonApiResponse($content, $statusCode, $headers);
9297
}
9398

9499
/**
95100
* @inheritdoc
96101
*/
97-
public function getCreatedResponse($resource, $links = null, $meta = null)
102+
public function getCreatedResponse($resource, $links = null, $meta = null, array $headers = [])
98103
{
99104
$encoder = $this->getEncoder();
100105
$links === null ?: $encoder->withLinks($links);
101106
$meta === null ?: $encoder->withMeta($meta);
102107
$content = $encoder->encodeData($resource, $this->getEncodingParameters());
103-
$headers = [self::HEADER_LOCATION => $this->getResourceLocationUrl($resource)];
108+
$headers[self::HEADER_LOCATION] = $this->getResourceLocationUrl($resource);
104109

105110
return $this->createJsonApiResponse($content, self::HTTP_CREATED, $headers);
106111
}
107112

108113
/**
109114
* @inheritdoc
110115
*/
111-
public function getCodeResponse($statusCode)
116+
public function getCodeResponse($statusCode, array $headers = [])
112117
{
113-
return $this->createJsonApiResponse(null, $statusCode);
118+
return $this->createJsonApiResponse(null, $statusCode, $headers);
114119
}
115120

116121
/**
117122
* @inheritdoc
118123
*/
119-
public function getMetaResponse($meta, $statusCode = self::HTTP_OK)
124+
public function getMetaResponse($meta, $statusCode = self::HTTP_OK, array $headers = [])
120125
{
121126
$encoder = $this->getEncoder();
122127
$content = $encoder->encodeMeta($meta);
123128

124-
return $this->createJsonApiResponse($content, $statusCode);
129+
return $this->createJsonApiResponse($content, $statusCode, $headers);
125130
}
126131

127132
/**
128133
* @inheritdoc
129134
*/
130-
public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST)
135+
public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST, array $headers = [])
131136
{
132137
if ($errors instanceof ErrorCollection || is_array($errors) === true) {
133138
/** @var Error[] $errors */
@@ -137,7 +142,7 @@ public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST)
137142
$content = $this->getEncoder()->encodeError($errors);
138143
}
139144

140-
return $this->createJsonApiResponse($content, $statusCode);
145+
return $this->createJsonApiResponse($content, $statusCode, $headers);
141146
}
142147

143148
/**

tests/Http/ResponsesTest.php

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,22 @@ public function testGetCodeResponse4()
110110
$this->assertEquals('some response', $this->responses->getCodeResponse(123));
111111
}
112112

113+
/**
114+
* Test get status code only response, with custom headers.
115+
*/
116+
public function testGetCodeResponse5()
117+
{
118+
$this->willBeCalledGetMediaType('some', 'type');
119+
$this->willBeCalledGetSupportedExtensions(null);
120+
$headers = [Responses::HEADER_CONTENT_TYPE => 'some/type', 'X-Custom' => 'Custom-Header'];
121+
$this->willBeCalledCreateResponse(null, 123, $headers, 'some response');
122+
$this->assertEquals('some response', $this->responses->getCodeResponse(123, ['X-Custom' => 'Custom-Header']));
123+
}
124+
113125
/**
114126
* Test response.
115127
*/
116-
public function testContentResponse()
128+
public function testContentResponse1()
117129
{
118130
$data = new stdClass();
119131
$links = ['some' => 'links'];
@@ -126,10 +138,28 @@ public function testContentResponse()
126138
$this->assertEquals('some response', $this->responses->getContentResponse($data, 321, $links, $meta));
127139
}
128140

141+
/**
142+
* Test content response, with custom headers.
143+
*/
144+
public function testContentResponse2()
145+
{
146+
$data = new stdClass();
147+
$links = ['some' => 'links'];
148+
$meta = ['some' => 'meta'];
149+
$this->willBeCalledGetMediaType('some', 'type');
150+
$this->willBeCalledGetSupportedExtensions(null);
151+
$this->willBeCalledEncoderForData($data, 'some json api', $links, $meta);
152+
$headers = [Responses::HEADER_CONTENT_TYPE => 'some/type', 'X-Custom' => 'Custom-Header'];
153+
$this->willBeCalledCreateResponse('some json api', 321, $headers, 'some response');
154+
$this->assertEquals('some response', $this->responses->getContentResponse($data, 321, $links, $meta, [
155+
'X-Custom' => 'Custom-Header',
156+
]));
157+
}
158+
129159
/**
130160
* Test response.
131161
*/
132-
public function testCreatedResponse()
162+
public function testCreatedResponse1()
133163
{
134164
$resource = new stdClass();
135165
$links = ['some' => 'links'];
@@ -146,10 +176,33 @@ public function testCreatedResponse()
146176
$this->assertEquals('some response', $this->responses->getCreatedResponse($resource, $links, $meta));
147177
}
148178

179+
/**
180+
* Test response, with custom headers
181+
*/
182+
public function testCreatedResponse2()
183+
{
184+
$resource = new stdClass();
185+
$links = ['some' => 'links'];
186+
$meta = ['some' => 'meta'];
187+
$this->willBeCalledGetMediaType('some', 'type');
188+
$this->willBeCalledGetSupportedExtensions(null);
189+
$this->willBeCalledEncoderForData($resource, 'some json api', $links, $meta);
190+
$this->willBeCreatedResourceLocationUrl($resource, 'http://server.tld', '/resource-type/123');
191+
$headers = [
192+
Responses::HEADER_CONTENT_TYPE => 'some/type',
193+
Responses::HEADER_LOCATION => 'http://server.tld/resource-type/123',
194+
'X-Custom' => 'Custom-Header',
195+
];
196+
$this->willBeCalledCreateResponse('some json api', Responses::HTTP_CREATED, $headers, 'some response');
197+
$this->assertEquals('some response', $this->responses->getCreatedResponse($resource, $links, $meta, [
198+
'X-Custom' => 'Custom-Header',
199+
]));
200+
}
201+
149202
/**
150203
* Test response.
151204
*/
152-
public function testMetaResponse()
205+
public function testMetaResponse1()
153206
{
154207
$meta = new stdClass();
155208
$this->willBeCalledGetMediaType('some', 'type');
@@ -160,6 +213,22 @@ public function testMetaResponse()
160213
$this->assertEquals('some response', $this->responses->getMetaResponse($meta, 321));
161214
}
162215

216+
/**
217+
* Test response, with custom headers
218+
*/
219+
public function testMetaResponse2()
220+
{
221+
$meta = new stdClass();
222+
$this->willBeCalledGetMediaType('some', 'type');
223+
$this->willBeCalledGetSupportedExtensions(null);
224+
$this->willBeCalledEncoderForMeta($meta, 'some json api');
225+
$headers = [Responses::HEADER_CONTENT_TYPE => 'some/type', 'X-Custom' => 'Custom-Header'];
226+
$this->willBeCalledCreateResponse('some json api', 321, $headers, 'some response');
227+
$this->assertEquals('some response', $this->responses->getMetaResponse($meta, 321, [
228+
'X-Custom' => 'Custom-Header',
229+
]));
230+
}
231+
163232
/**
164233
* Test response.
165234
*/
@@ -203,6 +272,22 @@ public function testErrorResponse3()
203272
$this->assertEquals('some response', $this->responses->getErrorResponse($errors, 321));
204273
}
205274

275+
/**
276+
* Test response, with custom headers.
277+
*/
278+
public function testErrorResponse4()
279+
{
280+
$error = new Error();
281+
$this->willBeCalledGetMediaType('some', 'type');
282+
$this->willBeCalledGetSupportedExtensions(null);
283+
$this->willBeCalledEncoderForError($error, 'some json api');
284+
$headers = [Responses::HEADER_CONTENT_TYPE => 'some/type', 'X-Custom' => 'Custom-Header'];
285+
$this->willBeCalledCreateResponse('some json api', 321, $headers, 'some response');
286+
$this->assertEquals('some response', $this->responses->getErrorResponse($error, 321, [
287+
'X-Custom' => 'Custom-Header',
288+
]));
289+
}
290+
206291
/**
207292
* @param string $type
208293
* @param string $subType

0 commit comments

Comments
 (0)