@@ -169,6 +169,66 @@ public function testResponseBadRequest(): void
169169 self ::assertSame (['Content-Type ' => ['application/json; charset=utf-8 ' ]], $ actual ->getHeaders ());
170170 }
171171
172+ public function testResponseUnauthorized (): void
173+ {
174+ /** @Given a body with error details */
175+ $ body = [
176+ 'error ' => 'Unauthorized ' ,
177+ 'message ' => 'Authentication is required to access this resource. '
178+ ];
179+
180+ /** @When we create the HTTP response with this body */
181+ $ actual = Response::unauthorized (body: $ body );
182+
183+ /** @Then the protocol version should be "1.1" */
184+ self ::assertSame ('1.1 ' , $ actual ->getProtocolVersion ());
185+
186+ /** @And the body of the response should match the JSON-encoded body */
187+ self ::assertSame (json_encode ($ body , JSON_PRESERVE_ZERO_FRACTION ), $ actual ->getBody ()->__toString ());
188+ self ::assertSame (json_encode ($ body , JSON_PRESERVE_ZERO_FRACTION ), $ actual ->getBody ()->getContents ());
189+
190+ /** @And the status code should be 401 */
191+ self ::assertSame (Code::UNAUTHORIZED ->value , $ actual ->getStatusCode ());
192+ self ::assertTrue (Code::isValidCode (code: $ actual ->getStatusCode ()));
193+ self ::assertTrue (Code::isErrorCode (code: $ actual ->getStatusCode ()));
194+
195+ /** @And the reason phrase should be "Unauthorized" */
196+ self ::assertSame (Code::UNAUTHORIZED ->message (), $ actual ->getReasonPhrase ());
197+
198+ /** @And the headers should contain Content-Type as application/json with charset=utf-8 */
199+ self ::assertSame (['Content-Type ' => ['application/json; charset=utf-8 ' ]], $ actual ->getHeaders ());
200+ }
201+
202+ public function testResponseForbidden (): void
203+ {
204+ /** @Given a body with error details */
205+ $ body = [
206+ 'error ' => 'Forbidden ' ,
207+ 'message ' => 'You do not have permission to access this resource. '
208+ ];
209+
210+ /** @When we create the HTTP response with this body */
211+ $ actual = Response::forbidden (body: $ body );
212+
213+ /** @Then the protocol version should be "1.1" */
214+ self ::assertSame ('1.1 ' , $ actual ->getProtocolVersion ());
215+
216+ /** @And the body of the response should match the JSON-encoded body */
217+ self ::assertSame (json_encode ($ body , JSON_PRESERVE_ZERO_FRACTION ), $ actual ->getBody ()->__toString ());
218+ self ::assertSame (json_encode ($ body , JSON_PRESERVE_ZERO_FRACTION ), $ actual ->getBody ()->getContents ());
219+
220+ /** @And the status code should be 403 */
221+ self ::assertSame (Code::FORBIDDEN ->value , $ actual ->getStatusCode ());
222+ self ::assertTrue (Code::isValidCode (code: $ actual ->getStatusCode ()));
223+ self ::assertTrue (Code::isErrorCode (code: $ actual ->getStatusCode ()));
224+
225+ /** @And the reason phrase should be "Forbidden" */
226+ self ::assertSame (Code::FORBIDDEN ->message (), $ actual ->getReasonPhrase ());
227+
228+ /** @And the headers should contain Content-Type as application/json with charset=utf-8 */
229+ self ::assertSame (['Content-Type ' => ['application/json; charset=utf-8 ' ]], $ actual ->getHeaders ());
230+ }
231+
172232 public function testResponseNotFound (): void
173233 {
174234 /** @Given a body with error details */
0 commit comments