MiddlewareTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 GuzzleHttp\Psr7\Response;
  11. use PHPUnit\Framework\TestCase;
  12. use Faker\Factory as Faker;
  13. use Psr\Http\Message\RequestInterface;
  14. use Retailmenot\Lab\Middleware\Dispatcher;
  15. class Test extends TestCase
  16. {
  17. /**
  18. * -- Data provider --
  19. * Define request heading to a success
  20. */
  21. public function correctRequest() {
  22. $requests = array();
  23. $faker = Faker::create();
  24. $request = new Request('GET', '/something');
  25. $request = $request->withHeader('X-RMN-Lab', 'rmn_request_'.$faker->randomNumber(5));
  26. $requests[] = [$request];
  27. return $requests;
  28. }
  29. /**
  30. * -- Data provider --
  31. * Define request heading to a failure
  32. */
  33. public function wrongRequestMissingHeader() {
  34. $requests = array();
  35. $request = new Request('GET', '/something');
  36. $requests[] = [$request];
  37. return $requests;
  38. }
  39. /**
  40. * -- Data provider --
  41. * Define request heading to a success
  42. */
  43. public function wrongRequestWrongHeaderName() {
  44. $requests = array();
  45. $faker = Faker::create();
  46. $request = new Request('GET', '/something');
  47. $request = $request->withHeader('X-RMN-Lab2', 'rmn_request_'.$faker->randomNumber(5));
  48. $requests[] = [$request];
  49. return $requests;
  50. }
  51. /**
  52. * -- Data provider --
  53. * Define request heading to a success
  54. */
  55. public function wrongRequestWrongHeaderValue() {
  56. $requests = array();
  57. $faker = Faker::create();
  58. $request = new Request('GET', '/something');
  59. $request = $request->withHeader('X-RMN-Lab', 'rmn_request2_'.$faker->randomNumber(5));
  60. $requests[] = [$request];
  61. return $requests;
  62. }
  63. /**
  64. * What : Input a request in the middleware
  65. * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
  66. * @param Request $request
  67. * @dataProvider correctRequest
  68. */
  69. public function testMiddlewareByFunctionSuccess($request) {
  70. $response = new Response();
  71. $response = addHeaderMiddleware($request, $response);
  72. $rmnHeader = $request->getHeader('X-RMN-Lab');
  73. $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
  74. if (!count($responseRmnHeader)) {
  75. $this->fail("Missing X-RMN-Lab-Response in Response object");
  76. }
  77. preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
  78. preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
  79. if (!count($matchesResponse)) {
  80. $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
  81. }
  82. $this->assertEquals(200, $response->getStatusCode());
  83. $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
  84. }
  85. /**
  86. * What : Input a request in the middleware without header
  87. * Expected : wait for a 400 response code and a JSON error body
  88. * @param Request $request
  89. * @dataProvider wrongRequestMissingHeader
  90. */
  91. public function testMiddlewareByFunctionFailureOnMissingHeader($request) {
  92. $response = new Response();
  93. $response = addHeaderMiddleware($request, $response);
  94. ob_start();
  95. echo $response->getBody();
  96. $body = ob_get_clean();
  97. $data = json_decode($body, true);
  98. if(is_null($data)) {
  99. $this->fail("Body isn't a correct JSON serialized string");
  100. }
  101. $contentType = $response->getHeader('Content-Type');
  102. if (!count($contentType)) {
  103. $this->fail("Missing Content-Type header");
  104. }
  105. $this->assertEquals("application/json", $contentType[0]);
  106. $this->assertEquals(400, $response->getStatusCode());
  107. $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
  108. $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
  109. $this->assertEquals('Missing "X-RMN-Lab" header', $data['message']);
  110. $this->assertEquals(1, $data['code']);
  111. }
  112. /**
  113. * What : Input a request in the middleware with a wrong header name
  114. * Expected : wait for a 400 response code and a JSON error body
  115. * @param Request $request
  116. * @dataProvider wrongRequestWrongHeaderName
  117. */
  118. public function testMiddlewareByFunctionFailureOnWrongHeaderName($request) {
  119. $response = new Response();
  120. $response = addHeaderMiddleware($request, $response);
  121. ob_start();
  122. echo $response->getBody();
  123. $body = ob_get_clean();
  124. $data = json_decode($body, true);
  125. if(is_null($data)) {
  126. $this->fail("Body isn't a correct JSON serialized string");
  127. }
  128. $contentType = $response->getHeader('Content-Type');
  129. if (!count($contentType)) {
  130. $this->fail("Missing Content-Type header");
  131. }
  132. $this->assertEquals("application/json", $contentType[0]);
  133. $this->assertEquals(400, $response->getStatusCode());
  134. $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
  135. $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
  136. $this->assertEquals('Missing "X-RMN-Lab" header', $data['message']);
  137. $this->assertEquals(1, $data['code']);
  138. }
  139. /**
  140. * What : Input a request in the middleware with a wrong header value
  141. * Expected : wait for a 400 response code and a JSON error body
  142. * @param Request $request
  143. * @dataProvider wrongRequestWrongHeaderValue
  144. */
  145. public function testMiddlewareByFunctionFailureOnWrongHeaderValue($request) {
  146. $response = new Response();
  147. $response = addHeaderMiddleware($request, $response);
  148. ob_start();
  149. echo $response->getBody();
  150. $body = ob_get_clean();
  151. $data = json_decode($body, true);
  152. if(is_null($data)) {
  153. $this->fail("Body isn't a correct JSON serialized string");
  154. }
  155. $rmnHeader = $request->getHeader('X-RMN-Lab')[0];
  156. $contentType = $response->getHeader('Content-Type');
  157. if (!count($contentType)) {
  158. $this->fail("Missing Content-Type header");
  159. }
  160. $this->assertEquals("application/json", $contentType[0]);
  161. $this->assertEquals(400, $response->getStatusCode());
  162. $this->assertArrayHasKey('message', $data, "Message body must contain the 'message' key");
  163. $this->assertArrayHasKey('code', $data, "Message body must contain the 'code' key");
  164. $this->assertEquals('Invalid "X-RMN-Lab" header: '.$rmnHeader, $data['message']);
  165. $this->assertEquals(2, $data['code']);
  166. }
  167. /**
  168. * What : Input a request in the middleware
  169. * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
  170. * @param Request $request
  171. * @dataProvider correctRequest
  172. */
  173. public function testMiddlewareByFunctionChainedSuccess($request) {
  174. $middleware1 = function($request, $response, $next=null) {
  175. return addHeaderMiddleware($request, $response, $next);
  176. };
  177. $middleware2 = function($request, $response, $next=null) {
  178. return dummyMiddleware($request, $response, $next);
  179. };
  180. $middleware3 = function($request, $response, $next=null) {
  181. return dummyMiddleware2($request, $response, $next);
  182. };
  183. $response = new Response();
  184. $response = $middleware1($request, $response, function($request, $response) use($middleware2, $middleware3) {
  185. return $middleware2($request, $response, function($request, $response) use($middleware3) {
  186. return $middleware3($request, $response, function ($request, $response) {
  187. return $response;
  188. });
  189. });
  190. });
  191. $middlewareHeader1 = $response->getHeader('X-RMN-DummyMiddleware');
  192. $middlewareHeader2 = $response->getHeader('X-RMN-DummyMiddleware2');
  193. $rmnHeader = $request->getHeader('X-RMN-Lab');
  194. $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
  195. if (!count($responseRmnHeader)) {
  196. $this->fail("Missing X-RMN-Lab-Response in Response object");
  197. }
  198. if (!count($middlewareHeader1)) {
  199. $this->fail("Missing X-RMN-DummyMiddleware in Response object");
  200. }
  201. if (!count($middlewareHeader2)) {
  202. $this->fail("Missing X-RMN-DummyMiddleware2 in Response object");
  203. }
  204. preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
  205. preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
  206. if (!count($matchesResponse)) {
  207. $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
  208. }
  209. $this->assertEquals(200, $response->getStatusCode());
  210. $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
  211. $this->assertEquals('I was here : dummy middleware', $middlewareHeader1[0]);
  212. $this->assertEquals('I was here : dummy middleware 2', $middlewareHeader2[0]);
  213. }
  214. /**
  215. * What : Input a request in the middleware
  216. * Expected : wait for a 200 response code and an header X-RMN-Lab with the right value
  217. * @param Request $request
  218. * @dataProvider correctRequest
  219. */
  220. public function testMiddlewareByFunctionDispatcheddSuccess($request)
  221. {
  222. $middleware1 = function ($request, $response, $next = null) {
  223. return addHeaderMiddleware($request, $response, $next);
  224. };
  225. $middleware2 = function ($request, $response, $next = null) {
  226. return dummyMiddleware($request, $response, $next);
  227. };
  228. $middleware3 = function ($request, $response, $next = null) {
  229. return dummyMiddleware2($request, $response, $next);
  230. };
  231. $response = new Response();
  232. $dispatcher = new Dispatcher();
  233. $dispatcher->pipe($middleware1);
  234. $dispatcher->pipe($middleware2);
  235. $dispatcher->pipe($middleware3);
  236. $response = $dispatcher->process($request, $response);
  237. $middlewareHeader1 = $response->getHeader('X-RMN-DummyMiddleware');
  238. $middlewareHeader2 = $response->getHeader('X-RMN-DummyMiddleware2');
  239. $rmnHeader = $request->getHeader('X-RMN-Lab');
  240. $responseRmnHeader = $response->getHeader('X-RMN-Lab-Response');
  241. if (!count($responseRmnHeader)) {
  242. $this->fail("Missing X-RMN-Lab-Response in Response object");
  243. }
  244. if (!count($middlewareHeader1)) {
  245. $this->fail("Missing X-RMN-DummyMiddleware in Response object");
  246. }
  247. if (!count($middlewareHeader2)) {
  248. $this->fail("Missing X-RMN-DummyMiddleware2 in Response object");
  249. }
  250. preg_match('#rmn_request_(\d+)#', $rmnHeader[0], $matchesRequest);
  251. preg_match('#rmn_response_(\d+)#', $responseRmnHeader[0], $matchesResponse);
  252. if (!count($matchesResponse)) {
  253. $this->fail("Wrong pattern in X-RMN-Lab-Response value: rmn_response_(\d+)");
  254. }
  255. $this->assertEquals(200, $response->getStatusCode());
  256. $this->assertEquals($matchesRequest[1], $matchesResponse[1], "The response header ID doesn't match to request header ID");
  257. $this->assertEquals('I was here : dummy middleware', $middlewareHeader1[0]);
  258. $this->assertEquals('I was here : dummy middleware 2', $middlewareHeader2[0]);
  259. }
  260. }