123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Noa
- * Date: 09/10/2017
- * Time: 15:50
- */
- namespace Retailmenot\Lab\Test;
- use GuzzleHttp\Psr7\Request;
- use PHPUnit\Framework\TestCase;
- use Faker\Factory as Faker;
- class Test extends TestCase
- {
- /**
- * -- Data provider --
- * Define request heading to a success
- */
- public function correctRequest() {
- $requests = array();
- $faker = Faker::create();
- $request = new Request('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 Request('GET', '/something');
- $requests[] = [$request];
- return $requests;
- }
- /**
- * -- Data provider --
- * Define request heading to a success
- */
- public function wrongRequestWrongHeaderName() {
- $requests = array();
- $faker = Faker::create();
- $request = new Request('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 Request('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 = addHeaderMiddleware($request);
- $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 = addHeaderMiddleware($request);
- 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 = addHeaderMiddleware($request);
- 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 = addHeaderMiddleware($request);
- 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']);
- }
- }
|