123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Noa
- * Date: 09/10/2017
- * Time: 15:50
- */
- namespace Retailmenot\Lab\Test;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Psr7\Response;
- use GuzzleHttp\Psr7\ServerRequest;
- use PHPUnit\Framework\TestCase;
- use Faker\Factory as Faker;
- use Retailmenot\Lab\Middleware\Dispatcher;
- use Retailmenot\Lab\Middleware\DispatcherPsr15;
- use Retailmenot\Lab\Middleware\middlewares\DummyMiddleware1;
- use Retailmenot\Lab\Middleware\middlewares\psr15\DummyMiddleware1Psr15;
- use Retailmenot\Lab\Middleware\middlewares\DummyMiddleware2;
- use Retailmenot\Lab\Middleware\middlewares\psr15\DummyMiddleware2Psr15;
- use Retailmenot\Lab\Middleware\middlewares\psr15\RmnMiddlewarePsr15;
- use Retailmenot\Lab\Middleware\middlewares\RmnMiddleware;
- class Test extends TestCase
- {
- /**
- * -- Data provider --
- * Define request heading to a success
- */
- public function correctRequest()
- {
- $requests = array();
- $faker = Faker::create();
- $request = new ServerRequest('GET', '/something');
- $request = $request->withHeader('X-RMN-Lab', 'rmn_request_' . $faker->randomNumber(5));
- $requests[] = [$request];
- return $requests;
- }
- /**
- * -- Data provider --
- * Define request heading to a failure
- */
- public function wrongRequestMissingHeader()
- {
- $requests = array();
- $request = new ServerRequest('GET', '/something');
- $requests[] = [$request];
- return $requests;
- }
- /**
- * -- Data provider --
- * Define request heading to a success
- */
- public function wrongRequestWrongHeaderName()
- {
- $requests = array();
- $faker = Faker::create();
- $request = new ServerRequest('GET', '/something');
- $request = $request->withHeader('X-RMN-Lab2', 'rmn_request_' . $faker->randomNumber(5));
- $requests[] = [$request];
- return $requests;
- }
- /**
- * -- Data provider --
- * Define request heading to a success
- */
- public function wrongRequestWrongHeaderValue()
- {
- $requests = array();
- $faker = Faker::create();
- $request = new ServerRequest('GET', '/something');
- $request = $request->withHeader('X-RMN-Lab', 'rmn_request2_' . $faker->randomNumber(5));
- $requests[] = [$request];
- return $requests;
- }
- /**
- * What : Input a request in the middleware
- * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
- * @param Request $request
- * @dataProvider correctRequest
- */
- public function testMiddlewareByFunctionSuccess($request)
- {
- $response = new Response();
- $response = addHeaderMiddleware($request, $response);
- $rmnHeader = $request->getHeader('X-RMN-Lab');
- $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
- if (!count($responseRmnHeader)) {
- $this->fail("Missing X-RMN-Lab-Response in Response object");
- }
- preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
- preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
- if (!count($matchesResponse)) {
- $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
- }
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
- }
- /**
- * What : Input a request in the middleware without header
- * Expected : wait for a 400 response code and a JSON error body
- * @param Request $request
- * @dataProvider wrongRequestMissingHeader
- */
- public function testMiddlewareByFunctionFailureOnMissingHeader($request)
- {
- $response = new Response();
- $response = addHeaderMiddleware($request, $response);
- ob_start();
- echo $response->getBody();
- $body = ob_get_clean();
- $data = json_decode($body, true);
- if (is_null($data)) {
- $this->fail("Body isn't a correct JSON serialized string");
- }
- $contentType = $response->getHeader('Content-Type');
- if (!count($contentType)) {
- $this->fail("Missing Content-Type header");
- }
- $this->assertEquals("application/json", $contentType[0]);
- $this->assertEquals(400, $response->getStatusCode());
- $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
- $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
- $this->assertEquals('Missing "X-RMN-Lab" header', $data['message']);
- $this->assertEquals(1, $data['code']);
- }
- /**
- * What : Input a request in the middleware with a wrong header name
- * Expected : wait for a 400 response code and a JSON error body
- * @param Request $request
- * @dataProvider wrongRequestWrongHeaderName
- */
- public function testMiddlewareByFunctionFailureOnWrongHeaderName($request)
- {
- $response = new Response();
- $response = addHeaderMiddleware($request, $response);
- ob_start();
- echo $response->getBody();
- $body = ob_get_clean();
- $data = json_decode($body, true);
- if (is_null($data)) {
- $this->fail("Body isn't a correct JSON serialized string");
- }
- $contentType = $response->getHeader('Content-Type');
- if (!count($contentType)) {
- $this->fail("Missing Content-Type header");
- }
- $this->assertEquals("application/json", $contentType[0]);
- $this->assertEquals(400, $response->getStatusCode());
- $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
- $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
- $this->assertEquals('Missing "X-RMN-Lab" header', $data['message']);
- $this->assertEquals(1, $data['code']);
- }
- /**
- * What : Input a request in the middleware with a wrong header value
- * Expected : wait for a 400 response code and a JSON error body
- * @param Request $request
- * @dataProvider wrongRequestWrongHeaderValue
- */
- public function testMiddlewareByFunctionFailureOnWrongHeaderValue($request)
- {
- $response = new Response();
- $response = addHeaderMiddleware($request, $response);
- ob_start();
- echo $response->getBody();
- $body = ob_get_clean();
- $data = json_decode($body, true);
- if (is_null($data)) {
- $this->fail("Body isn't a correct JSON serialized string");
- }
- $rmnHeader = $request->getHeader('X-RMN-Lab')[0];
- $contentType = $response->getHeader('Content-Type');
- if (!count($contentType)) {
- $this->fail("Missing Content-Type header");
- }
- $this->assertEquals("application/json", $contentType[0]);
- $this->assertEquals(400, $response->getStatusCode());
- $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
- $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
- $this->assertEquals('Invalid "X-RMN-Lab" header: ' . $rmnHeader, $data['message']);
- $this->assertEquals(2, $data['code']);
- }
- /**
- * What : Input a request in the middleware
- * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
- * @param Request $request
- * @dataProvider correctRequest
- */
- public function testMiddlewareByFunctionChainedSuccess($request)
- {
- $middleware1 = function ($request, $response, $next = null) {
- return addHeaderMiddleware($request, $response, $next);
- };
- $middleware2 = function ($request, $response, $next = null) {
- return dummyMiddleware($request, $response, $next);
- };
- $middleware3 = function ($request, $response, $next = null) {
- return dummyMiddleware2($request, $response, $next);
- };
- $response = new Response();
- $response = $middleware1($request, $response, function ($request, $response) use ($middleware2, $middleware3) {
- return $middleware2($request, $response, function ($request, $response) use ($middleware3) {
- return $middleware3($request, $response, function ($request, $response) {
- return $response;
- });
- });
- });
- $middlewareHeader1 = $response->getHeader('X-RMN-DummyMiddleware');
- $middlewareHeader2 = $response->getHeader('X-RMN-DummyMiddleware2');
- $rmnHeader = $request->getHeader('X-RMN-Lab');
- $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
- if (!count($responseRmnHeader)) {
- $this->fail("Missing X-RMN-Lab-Response in Response object");
- }
- if (!count($middlewareHeader1)) {
- $this->fail("Missing X-RMN-DummyMiddleware in Response object");
- }
- if (!count($middlewareHeader2)) {
- $this->fail("Missing X-RMN-DummyMiddleware2 in Response object");
- }
- preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
- preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
- if (!count($matchesResponse)) {
- $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
- }
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
- $this->assertEquals('I was here : dummy middleware', $middlewareHeader1[0]);
- $this->assertEquals('I was here : dummy middleware 2', $middlewareHeader2[0]);
- }
- /**
- * What : Input a request in the middleware
- * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
- * @param Request $request
- * @dataProvider correctRequest
- */
- public function testMiddlewareByFunctionDispatcheddSuccess($request)
- {
- $middleware1 = function ($request, $response, $next = null) {
- return addHeaderMiddleware($request, $response, $next);
- };
- $middleware2 = function ($request, $response, $next = null) {
- return dummyMiddleware($request, $response, $next);
- };
- $middleware3 = function ($request, $response, $next = null) {
- return dummyMiddleware2($request, $response, $next);
- };
- $response = new Response();
- $dispatcher = new Dispatcher();
- $dispatcher->pipe($middleware1);
- $dispatcher->pipe($middleware2);
- $dispatcher->pipe($middleware3);
- $response = $dispatcher->process($request, $response);
- $middlewareHeader1 = $response->getHeader('X-RMN-DummyMiddleware');
- $middlewareHeader2 = $response->getHeader('X-RMN-DummyMiddleware2');
- $rmnHeader = $request->getHeader('X-RMN-Lab');
- $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
- if (!count($responseRmnHeader)) {
- $this->fail("Missing X-RMN-Lab-Response in Response object");
- }
- if (!count($middlewareHeader1)) {
- $this->fail("Missing X-RMN-DummyMiddleware in Response object");
- }
- if (!count($middlewareHeader2)) {
- $this->fail("Missing X-RMN-DummyMiddleware2 in Response object");
- }
- preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
- preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
- if (!count($matchesResponse)) {
- $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
- }
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
- $this->assertEquals('I was here : dummy middleware', $middlewareHeader1[0]);
- $this->assertEquals('I was here : dummy middleware 2', $middlewareHeader2[0]);
- }
- /**
- * What : Input a request in the middleware
- * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
- * @param Request $request
- * @dataProvider correctRequest
- */
- public function testMiddlewareByClassDispatcheddSuccess($request)
- {
- $response = new Response();
- $dispatcher = new Dispatcher();
- $dispatcher->pipe(new RmnMiddleware());
- $dispatcher->pipe(new DummyMiddleware1());
- $dispatcher->pipe(new DummyMiddleware2());
- $response = $dispatcher->process($request, $response);
- $middlewareHeader1 = $response->getHeader('X-RMN-DummyMiddleware');
- $middlewareHeader2 = $response->getHeader('X-RMN-DummyMiddleware2');
- $rmnHeader = $request->getHeader('X-RMN-Lab');
- $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
- if (!count($responseRmnHeader)) {
- $this->fail("Missing X-RMN-Lab-Response in Response object");
- }
- if (!count($middlewareHeader1)) {
- $this->fail("Missing X-RMN-DummyMiddleware in Response object");
- }
- if (!count($middlewareHeader2)) {
- $this->fail("Missing X-RMN-DummyMiddleware2 in Response object");
- }
- preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
- preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
- if (!count($matchesResponse)) {
- $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
- }
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
- $this->assertEquals('I was here : dummy middleware', $middlewareHeader1[0]);
- $this->assertEquals('I was here : dummy middleware 2', $middlewareHeader2[0]);
- }
- /**
- * What : Input a request in the middleware
- * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
- * @param ServerRequest $request
- * @dataProvider correctRequest
- */
- public function testMiddlewareByClassPsr15DispatcheddSuccess($request)
- {
- $dispatcher = new DispatcherPsr15();
- $dispatcher->pipe(new RmnMiddlewarePsr15());
- $dispatcher->pipe(new DummyMiddleware1Psr15());
- $dispatcher->pipe(new DummyMiddleware2Psr15());
- $response = $dispatcher->handle($request);
- $middlewareHeader1 = $response->getHeader('X-RMN-DummyMiddleware');
- $middlewareHeader2 = $response->getHeader('X-RMN-DummyMiddleware2');
- $rmnHeader = $request->getHeader('X-RMN-Lab');
- $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
- if (!count($responseRmnHeader)) {
- $this->fail("Missing X-RMN-Lab-Response in Response object");
- }
- if (!count($middlewareHeader1)) {
- $this->fail("Missing X-RMN-DummyMiddleware in Response object");
- }
- if (!count($middlewareHeader2)) {
- $this->fail("Missing X-RMN-DummyMiddleware2 in Response object");
- }
- preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
- preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
- if (!count($matchesResponse)) {
- $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
- }
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
- $this->assertEquals('I was here : dummy middleware', $middlewareHeader1[0]);
- $this->assertEquals('I was here : dummy middleware 2', $middlewareHeader2[0]);
- }
- }
|