Browse Source

Update IntegrationTestCase tests to use httpServer.

By default always use the http/server implementation as it is not
deprecated. Add a few tests to cover the legacy dispatcher process. I've
also reverted the changes made in #10957 as they are no longer
necessary. The `getRequestTarget()` method does not return an encoded
query string so we don't need to encode it when generating security
tokens either.
mark_story 8 years ago
parent
commit
b1adcab2ca

+ 6 - 7
src/TestSuite/IntegrationTestCase.php

@@ -532,7 +532,7 @@ abstract class IntegrationTestCase extends TestCase
                 $this->_viewName = $viewFile;
             }
             if ($this->_retainFlashMessages) {
-                $this->_flashMessages = $controller->request->session()->read('Flash');
+                $this->_flashMessages = $controller->request->getSession()->read('Flash');
             }
         });
         $events->on('View.beforeLayout', function ($event, $viewFile) {
@@ -579,12 +579,11 @@ abstract class IntegrationTestCase extends TestCase
         list ($url, $query) = $this->_url($url);
         $tokenUrl = $url;
 
-        parse_str($query, $queryData);
-
         if ($query) {
-            $tokenUrl .= '?' . http_build_query($queryData);
+            $tokenUrl .= '?' . $query;
         }
 
+        parse_str($query, $queryData);
         $props = [
             'url' => $url,
             'session' => $session,
@@ -916,7 +915,7 @@ abstract class IntegrationTestCase extends TestCase
         if ($alias !== false) {
             $type = $alias;
         }
-        $result = $this->_response->type();
+        $result = $this->_response->getType();
         $this->assertEquals($type, $result, $message);
     }
 
@@ -1087,7 +1086,7 @@ abstract class IntegrationTestCase extends TestCase
         if (!$this->_response) {
             $this->fail('Not response set, cannot assert cookies.');
         }
-        $result = $this->_response->cookie($name);
+        $result = $this->_response->getCookie($name);
         $this->assertEquals(
             $expected,
             $result['value'],
@@ -1146,7 +1145,7 @@ abstract class IntegrationTestCase extends TestCase
         if (!$this->_response) {
             $this->fail('No response set, cannot assert cookies.');
         }
-        $result = $this->_response->cookie($name);
+        $result = $this->_response->getCookie($name);
         $this->_cookieEncryptionKey = $key;
         $result['value'] = $this->_decrypt($result['value'], $encrypt);
         $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);

+ 89 - 41
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -19,6 +19,7 @@ use Cake\Event\EventManager;
 use Cake\Http\Response;
 use Cake\Routing\DispatcherFactory;
 use Cake\Routing\Router;
+use Cake\Routing\Route\InflectedRoute;
 use Cake\TestSuite\IntegrationTestCase;
 use Cake\Test\Fixture\AssertIntegrationTestCase;
 use Cake\Utility\Security;
@@ -39,13 +40,30 @@ class IntegrationTestCaseTest extends IntegrationTestCase
         parent::setUp();
         static::setAppNamespace();
 
-        Router::connect('/get/:controller/:action', ['_method' => 'GET'], ['routeClass' => 'InflectedRoute']);
-        Router::connect('/head/:controller/:action', ['_method' => 'HEAD'], ['routeClass' => 'InflectedRoute']);
-        Router::connect('/options/:controller/:action', ['_method' => 'OPTIONS'], ['routeClass' => 'InflectedRoute']);
-        Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
+        Router::reload();
+        Router::scope('/', function ($routes) {
+            $routes->setRouteClass(InflectedRoute::class);
+            $routes->get('/get/:controller/:action', []);
+            $routes->head('/head/:controller/:action', []);
+            $routes->options('/options/:controller/:action', []);
+            $routes->connect('/:controller/:action/*', []);
+        });
+        Router::$initialized = true;
+
+        $this->useHttpServer(true);
         DispatcherFactory::clear();
+    }
+
+    /**
+     * Helper for enabling the legacy stack for backwards compatibility testing.
+     *
+     * @return void
+     */
+    protected function useLegacyDispatcher()
+    {
         DispatcherFactory::add('Routing');
         DispatcherFactory::add('ControllerFactory');
+
         $this->useHttpServer(false);
     }
 
@@ -174,20 +192,24 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     /**
      * Test sending get requests.
      *
+     * @group deprecated
      * @return void
      */
-    public function testGet()
+    public function testGetLegacy()
     {
-        $this->assertNull($this->_response);
+        $this->useLegacyDispatcher();
+        $this->deprecated(function () {
+            $this->assertNull($this->_response);
 
-        $this->get('/request_action/test_request_action');
-        $this->assertNotEmpty($this->_response);
-        $this->assertInstanceOf('Cake\Http\Response', $this->_response);
-        $this->assertEquals('This is a test', $this->_response->getBody());
+            $this->get('/request_action/test_request_action');
+            $this->assertNotEmpty($this->_response);
+            $this->assertInstanceOf('Cake\Http\Response', $this->_response);
+            $this->assertEquals('This is a test', $this->_response->getBody());
 
-        $this->_response = null;
-        $this->get('/get/request_action/test_request_action');
-        $this->assertEquals('This is a test', $this->_response->getBody());
+            $this->_response = null;
+            $this->get('/get/request_action/test_request_action');
+            $this->assertEquals('This is a test', $this->_response->getBody());
+        });
     }
 
     /**
@@ -200,7 +222,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
         // first clean routes to have Router::$initailized === false
         Router::reload();
 
-        $this->useHttpServer(true);
         $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', null);
 
         $this->get('/some_alias');
@@ -251,9 +272,23 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      *
      * @return void
      */
+    public function testGetSpecificRouteLegacy()
+    {
+        $this->useLegacyDispatcher();
+        $this->deprecated(function () {
+            $this->get('/get/request_action/test_request_action');
+            $this->assertResponseOk();
+            $this->assertEquals('This is a test', $this->_response->getBody());
+        });
+    }
+
+    /**
+     * Test sending get requests sets the request method
+     *
+     * @return void
+     */
     public function testGetSpecificRouteHttpServer()
     {
-        $this->useHttpServer(true);
         $this->get('/get/request_action/test_request_action');
         $this->assertResponseOk();
         $this->assertEquals('This is a test', $this->_response->getBody());
@@ -268,8 +303,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     {
         $this->expectException(\LogicException::class);
         $this->expectExceptionMessage('Cannot load "TestApp\MissingApp" for use in integration');
-        DispatcherFactory::clear();
-        $this->useHttpServer(true);
         $this->configApplication('TestApp\MissingApp', []);
         $this->get('/request_action/test_request_action');
     }
@@ -281,8 +314,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testGetHttpServer()
     {
-        DispatcherFactory::clear();
-        $this->useHttpServer(true);
         $this->assertNull($this->_response);
 
         $this->get('/request_action/test_request_action');
@@ -299,8 +330,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testGetQueryStringHttpServer()
     {
-        $this->useHttpServer(true);
-
         $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
         $this->get('/request_action/params_pass?q=query');
         $this->assertResponseOk();
@@ -309,19 +338,38 @@ class IntegrationTestCaseTest extends IntegrationTestCase
         $this->assertHeader('X-Middleware', 'true');
 
         $request = $this->_controller->request;
-        $this->assertContains('/request_action/params_pass?q=query', $request->here());
         $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
     }
 
     /**
+     * Test that the PSR7 requests get query string data
+     *
+     * @group deprecated
+     * @return void
+     */
+    public function testGetQueryStringSetsHere()
+    {
+        $this->deprecated(function () {
+            $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
+            $this->get('/request_action/params_pass?q=query');
+            $this->assertResponseOk();
+            $this->assertResponseContains('"q":"query"');
+            $this->assertResponseContains('"contentType":"text\/plain"');
+            $this->assertHeader('X-Middleware', 'true');
+
+            $request = $this->_controller->request;
+            $this->assertContains('/request_action/params_pass?q=query', $request->here());
+            $this->assertContains('/request_action/params_pass?q=query', $request->getRequestTarget());
+        });
+    }
+
+    /**
      * Test that the PSR7 requests get cookies
      *
      * @return void
      */
     public function testGetCookiesHttpServer()
     {
-        $this->useHttpServer(true);
-
         $this->configRequest(['cookies' => ['split_test' => 'abc']]);
         $this->get('/request_action/cookie_pass');
         $this->assertResponseOk();
@@ -334,10 +382,24 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      *
      * @return void
      */
-    public function testPostDataHttpServer()
+    public function testPostDataLegacyDispatcher()
     {
-        $this->useHttpServer(true);
+        $this->useLegacyDispatcher();
+
+        $this->deprecated(function () {
+            $this->post('/request_action/post_pass', ['title' => 'value']);
+            $data = json_decode($this->_response->getBody());
+            $this->assertEquals('value', $data->title);
+        });
+    }
 
+    /**
+     * Test that the PSR7 requests receive post data
+     *
+     * @return void
+     */
+    public function testPostDataHttpServer()
+    {
         $this->post('/request_action/post_pass', ['title' => 'value']);
         $data = json_decode($this->_response->getBody());
         $this->assertEquals('value', $data->title);
@@ -351,8 +413,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testInputDataHttpServer()
     {
-        $this->useHttpServer(true);
-
         $this->post('/request_action/input_test', '{"hello":"world"}');
         if ($this->_response->getBody()->isSeekable()) {
             $this->_response->getBody()->rewind();
@@ -368,7 +428,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testInputDataSecurityToken()
     {
-        $this->useHttpServer(true);
         $this->enableSecurityToken();
 
         $this->post('/request_action/input_test', '{"hello":"world"}');
@@ -383,8 +442,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testSessionHttpServer()
     {
-        $this->useHttpServer(true);
-
         $this->session(['foo' => 'session data']);
         $this->get('/request_action/session_test');
         $this->assertResponseOk();
@@ -418,9 +475,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testRequestSetsPropertiesHttpServer()
     {
-        $this->useHttpServer(true);
-        DispatcherFactory::clear();
-
         $this->post('/posts/index');
         $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
         $this->assertNotEmpty($this->_viewName, 'View name not set');
@@ -478,7 +532,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testFlashSessionAndCookieAssertsHttpServer()
     {
-        $this->useHttpServer(true);
         $this->post('/posts/index');
 
         $this->assertSession('An error message', 'Flash.flash.0.message');
@@ -660,7 +713,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     public function testWithExpectedExceptionHttpServer()
     {
         DispatcherFactory::clear();
-        $this->useHttpServer(true);
 
         $this->get('/tests_apps/throw_exception');
         $this->assertResponseCode(500);
@@ -698,7 +750,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     public function testRedirectHttpServer()
     {
         DispatcherFactory::clear();
-        $this->useHttpServer(true);
 
         $this->post('/tests_apps/redirect_to');
         $this->assertResponseCode(302);
@@ -847,7 +898,7 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     public function testAssertContentType()
     {
         $this->_response = new Response();
-        $this->_response->type('json');
+        $this->_response = $this->_response->withType('json');
 
         $this->assertContentType('json');
         $this->assertContentType('application/json');
@@ -1002,9 +1053,6 @@ class IntegrationTestCaseTest extends IntegrationTestCase
      */
     public function testSendFileHttpServer()
     {
-        DispatcherFactory::clear();
-        $this->useHttpServer(true);
-
         $this->get('/posts/file');
         $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
     }

+ 1 - 0
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -141,6 +141,7 @@ class RequestActionController extends AppController
         if ($this->request->getParam('0')) {
             $content = 'return found';
         }
+
         return $this->response->withStringBody($content);
     }