MiddlewareTest.php 16 KB

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