From 9d890f42277b79a6597456589d260fddcd9a91b8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 3 May 2018 19:04:40 +0200 Subject: [PATCH] Created CreateShortCodeContentNegotiationMiddleware --- ...rtCodeContentNegotiationMiddlewareTest.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 module/Rest/test/Middleware/ShortCode/CreateShortCodeContentNegotiationMiddlewareTest.php diff --git a/module/Rest/test/Middleware/ShortCode/CreateShortCodeContentNegotiationMiddlewareTest.php b/module/Rest/test/Middleware/ShortCode/CreateShortCodeContentNegotiationMiddlewareTest.php new file mode 100644 index 00000000..b61f06bf --- /dev/null +++ b/module/Rest/test/Middleware/ShortCode/CreateShortCodeContentNegotiationMiddlewareTest.php @@ -0,0 +1,68 @@ +middleware = new CreateShortCodeContentNegotiationMiddleware(); + $this->requestHandler = new class implements RequestHandlerInterface { + /** + * Handle the request and return a response. + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new JsonResponse(['shortUrl' => 'http://doma.in/foo']); + } + }; + } + + /** + * @test + * @dataProvider provideData + * @param array $query + */ + public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType) + { + $request = ServerRequestFactory::fromGlobals()->withQueryParams($query); + if ($accept !== null) { + $request = $request->withHeader('Accept', $accept); + } + + $response = $this->middleware->process($request, $this->requestHandler); + + $this->assertEquals($expectedContentType, $response->getHeaderLine('Content-type')); + } + + public function provideData(): array + { + return [ + [null, [], 'application/json'], + [null, ['format' => 'json'], 'application/json'], + [null, ['format' => 'invalid'], 'application/json'], + [null, ['format' => 'txt'], 'text/plain'], + ['application/json', [], 'application/json'], + ['application/xml', [], 'application/json'], + ['text/plain', [], 'text/plain'], + ]; + } +}