MiddlewareTest.php 14 KB

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