MiddlewareTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Noa
  5. * Date: 09/10/2017
  6. * Time: 15:50
  7. */
  8. namespace Retailmenot\Lab\Test;
  9. use GuzzleHttp\Psr7\Request;
  10. use PHPUnit\Framework\TestCase;
  11. use Faker\Factory as Faker;
  12. class Test extends TestCase
  13. {
  14. /**
  15. * -- Data provider --
  16. * Define request heading to a success
  17. */
  18. public function correctRequest() {
  19. $requests = array();
  20. $faker = Faker::create();
  21. $request = new Request('GET', '/something');
  22. $request = $request->withHeader('X-RMN-Lab', 'rmn_request_'.$faker->randomNumber(5));
  23. $requests[] = [$request];
  24. return $requests;
  25. }
  26. /**
  27. * -- Data provider --
  28. * Define request heading to a failure
  29. */
  30. public function wrongRequestMissingHeader() {
  31. $requests = array();
  32. $request = new Request('GET', '/something');
  33. $requests[] = [$request];
  34. return $requests;
  35. }
  36. /**
  37. * -- Data provider --
  38. * Define request heading to a success
  39. */
  40. public function wrongRequestWrongHeaderName() {
  41. $requests = array();
  42. $faker = Faker::create();
  43. $request = new Request('GET', '/something');
  44. $request = $request->withHeader('X-RMN-Lab2', 'rmn_request_'.$faker->randomNumber(5));
  45. $requests[] = [$request];
  46. return $requests;
  47. }
  48. /**
  49. * -- Data provider --
  50. * Define request heading to a success
  51. */
  52. public function wrongRequestWrongHeaderValue() {
  53. $requests = array();
  54. $faker = Faker::create();
  55. $request = new Request('GET', '/something');
  56. $request = $request->withHeader('X-RMN-Lab', 'rmn_request2_'.$faker->randomNumber(5));
  57. $requests[] = [$request];
  58. return $requests;
  59. }
  60. /**
  61. * What : Input a request in the middleware
  62. * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
  63. * @param Request $request
  64. * @dataProvider correctRequest
  65. */
  66. public function testMiddlewareByFunctionSuccess($request) {
  67. $response = addHeaderMiddleware($request);
  68. $rmnHeader = $request->getHeader('X-RMN-Lab');
  69. $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
  70. if (!count($responseRmnHeader)) {
  71. $this->fail("Missing X-RMN-Lab-Response in Response object");
  72. }
  73. preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
  74. preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
  75. if (!count($matchesResponse)) {
  76. $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
  77. }
  78. $this->assertEquals(200, $response->getStatusCode());
  79. $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
  80. }
  81. /**
  82. * What : Input a request in the middleware without header
  83. * Expected : wait for a 400 response code and a JSON error body
  84. * @param Request $request
  85. * @dataProvider wrongRequestMissingHeader
  86. */
  87. public function testMiddlewareByFunctionFailureOnMissingHeader($request) {
  88. $response = addHeaderMiddleware($request);
  89. ob_start();
  90. echo $response->getBody();
  91. $body = ob_get_clean();
  92. $data = json_decode($body, true);
  93. if(is_null($data)) {
  94. $this->fail("Body isn't a correct JSON serialized string");
  95. }
  96. $contentType = $response->getHeader('Content-Type');
  97. if (!count($contentType)) {
  98. $this->fail("Missing Content-Type header");
  99. }
  100. $this->assertEquals("application/json", $contentType[0]);
  101. $this->assertEquals(400, $response->getStatusCode());
  102. $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
  103. $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
  104. $this->assertEquals('Missing "X-RMN-Lab" header', $data['message']);
  105. $this->assertEquals(1, $data['code']);
  106. }
  107. /**
  108. * What : Input a request in the middleware with a wrong header name
  109. * Expected : wait for a 400 response code and a JSON error body
  110. * @param Request $request
  111. * @dataProvider wrongRequestWrongHeaderName
  112. */
  113. public function testMiddlewareByFunctionFailureOnWrongHeaderName($request) {
  114. $response = addHeaderMiddleware($request);
  115. ob_start();
  116. echo $response->getBody();
  117. $body = ob_get_clean();
  118. $data = json_decode($body, true);
  119. if(is_null($data)) {
  120. $this->fail("Body isn't a correct JSON serialized string");
  121. }
  122. $contentType = $response->getHeader('Content-Type');
  123. if (!count($contentType)) {
  124. $this->fail("Missing Content-Type header");
  125. }
  126. $this->assertEquals("application/json", $contentType[0]);
  127. $this->assertEquals(400, $response->getStatusCode());
  128. $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
  129. $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
  130. $this->assertEquals('Missing "X-RMN-Lab" header', $data['message']);
  131. $this->assertEquals(1, $data['code']);
  132. }
  133. /**
  134. * What : Input a request in the middleware with a wrong header value
  135. * Expected : wait for a 400 response code and a JSON error body
  136. * @param Request $request
  137. * @dataProvider wrongRequestWrongHeaderValue
  138. */
  139. public function testMiddlewareByFunctionFailureOnWrongHeaderValue($request) {
  140. $response = addHeaderMiddleware($request);
  141. ob_start();
  142. echo $response->getBody();
  143. $body = ob_get_clean();
  144. $data = json_decode($body, true);
  145. if(is_null($data)) {
  146. $this->fail("Body isn't a correct JSON serialized string");
  147. }
  148. $rmnHeader = $request->getHeader('X-RMN-Lab')[0];
  149. $contentType = $response->getHeader('Content-Type');
  150. if (!count($contentType)) {
  151. $this->fail("Missing Content-Type header");
  152. }
  153. $this->assertEquals("application/json", $contentType[0]);
  154. $this->assertEquals(400, $response->getStatusCode());
  155. $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
  156. $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
  157. $this->assertEquals('Invalid "X-RMN-Lab" header: '.$rmnHeader, $data['message']);
  158. $this->assertEquals(2, $data['code']);
  159. }
  160. }