Browse Source

Fix warnings in Http/Client tests.

Mark Story 8 years ago
parent
commit
792f1b97e1

+ 1 - 1
src/Http/Client.php

@@ -16,8 +16,8 @@ namespace Cake\Http;
 use Cake\Core\App;
 use Cake\Core\Exception\Exception;
 use Cake\Core\InstanceConfigTrait;
-use Cake\Http\Client\CookieCollection;
 use Cake\Http\Client\Request;
+use Cake\Http\Cookie\CookieCollection;
 use Cake\Http\Cookie\CookieInterface;
 use Cake\Utility\Hash;
 use InvalidArgumentException;

+ 2 - 2
src/Http/Client/Adapter/Stream.php

@@ -184,8 +184,8 @@ class Stream
      */
     protected function _buildOptions(Request $request, $options)
     {
-        $this->_contextOptions['method'] = $request->method();
-        $this->_contextOptions['protocol_version'] = $request->version();
+        $this->_contextOptions['method'] = $request->getMethod();
+        $this->_contextOptions['protocol_version'] = $request->getProtocolVersion();
         $this->_contextOptions['ignore_errors'] = true;
 
         if (isset($options['timeout'])) {

+ 3 - 3
src/Http/Client/Request.php

@@ -208,8 +208,8 @@ class Request extends Message implements RequestInterface
     public function cookie($name, $value = null)
     {
         deprecationWarning(
-            'Request::header() is deprecated. ' .
-            'No longer used. CookieCollections now add `Cookie` header to the ' .
+            'Request::cookie() is deprecated. ' .
+            'The Client internals now add the required `Cookie` header to the ' .
             'request before sending. Use Cake\Http\Cookie\CookieCollection::addToRequest() ' .
             'to make adding cookies to a request easier.'
         );
@@ -272,7 +272,7 @@ class Request extends Message implements RequestInterface
         if (is_array($body)) {
             $formData = new FormData();
             $formData->addMany($body);
-            $this->header('Content-Type', $formData->contentType());
+            $this->addHeaders(['Content-Type' => $formData->contentType()]);
             $body = (string)$formData;
         }
         $stream = new Stream('php://memory', 'rw');

+ 60 - 64
tests/TestCase/Http/Client/Adapter/StreamTest.php

@@ -122,10 +122,10 @@ class StreamTest extends TestCase
     public function testSend()
     {
         $stream = new Stream();
-        $request = new Request();
-        $request->url('http://localhost')
-            ->header('User-Agent', 'CakePHP TestSuite')
-            ->cookie('testing', 'value');
+        $request = new Request('http://localhost', 'GET', [
+            'User-Agent' => 'CakePHP TestSuite',
+            'Cookie' => 'testing=value'
+        ]);
 
         try {
             $responses = $stream->send($request, []);
@@ -143,8 +143,7 @@ class StreamTest extends TestCase
     public function testSendByUsingCakephpProtocol()
     {
         $stream = new Stream();
-        $request = new Request();
-        $request->url('http://dummy/');
+        $request = new Request('http://dummy/');
 
         $responses = $stream->send($request, []);
         $this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
@@ -159,13 +158,15 @@ class StreamTest extends TestCase
      */
     public function testBuildingContextHeader()
     {
-        $request = new Request();
-        $request->url('http://localhost')
-            ->header([
+        $request = new Request(
+            'http://localhost',
+            'GET',
+            [
                 'User-Agent' => 'CakePHP TestSuite',
                 'Content-Type' => 'application/json',
                 'Cookie' => 'a=b; c=do%20it'
-            ]);
+            ]
+        );
 
         $options = [
             'redirect' => 20,
@@ -173,10 +174,10 @@ class StreamTest extends TestCase
         $this->stream->send($request, $options);
         $result = $this->stream->contextOptions();
         $expected = [
-            'Connection: close',
             'User-Agent: CakePHP TestSuite',
             'Content-Type: application/json',
             'Cookie: a=b; c=do%20it',
+            'Connection: close',
         ];
         $this->assertEquals(implode("\r\n", $expected), $result['header']);
         $this->assertSame(0, $result['max_redirects']);
@@ -191,12 +192,12 @@ class StreamTest extends TestCase
     public function testSendContextContentString()
     {
         $content = json_encode(['a' => 'b']);
-        $request = new Request();
-        $request->url('http://localhost')
-            ->header([
-                'Content-Type' => 'application/json'
-            ])
-            ->body($content);
+        $request = new Request(
+            'http://localhost',
+            'GET',
+            ['Content-Type' => 'application/json'],
+            $content
+        );
 
         $options = [
             'redirect' => 20
@@ -204,9 +205,9 @@ class StreamTest extends TestCase
         $this->stream->send($request, $options);
         $result = $this->stream->contextOptions();
         $expected = [
+            'Content-Type: application/json',
             'Connection: close',
             'User-Agent: CakePHP',
-            'Content-Type: application/json',
         ];
         $this->assertEquals(implode("\r\n", $expected), $result['header']);
         $this->assertEquals($content, $result['content']);
@@ -219,19 +220,21 @@ class StreamTest extends TestCase
      */
     public function testSendContextContentArray()
     {
-        $request = new Request();
-        $request->url('http://localhost')
-            ->header([
+        $request = new Request(
+            'http://localhost',
+            'GET',
+            [
                 'Content-Type' => 'application/json'
-            ])
-            ->body(['a' => 'my value']);
+            ],
+            ['a' => 'my value']
+        );
 
         $this->stream->send($request, []);
         $result = $this->stream->contextOptions();
         $expected = [
+            'Content-Type: application/x-www-form-urlencoded',
             'Connection: close',
             'User-Agent: CakePHP',
-            'Content-Type: application/x-www-form-urlencoded',
         ];
         $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
         $this->assertContains('a=my+value', $result['content']);
@@ -245,21 +248,18 @@ class StreamTest extends TestCase
      */
     public function testSendContextContentArrayFiles()
     {
-        $request = new Request();
-        $request->url('http://localhost')
-            ->header([
-                'Content-Type' => 'application/json'
-            ])
-            ->body(['upload' => fopen(CORE_PATH . 'VERSION.txt', 'r')]);
+        $request = new Request(
+            'http://localhost',
+            'GET',
+            ['Content-Type' => 'application/json'],
+            ['upload' => fopen(CORE_PATH . 'VERSION.txt', 'r')]
+        );
 
         $this->stream->send($request, []);
         $result = $this->stream->contextOptions();
-        $expected = [
-            'Connection: close',
-            'User-Agent: CakePHP',
-            'Content-Type: multipart/form-data',
-        ];
-        $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
+        $this->assertContains("Content-Type: multipart/form-data", $result['header']);
+        $this->assertContains("Connection: close\r\n", $result['header']);
+        $this->assertContains("User-Agent: CakePHP", $result['header']);
         $this->assertContains('name="upload"', $result['content']);
         $this->assertContains('filename="VERSION.txt"', $result['content']);
     }
@@ -271,8 +271,7 @@ class StreamTest extends TestCase
      */
     public function testSendContextSsl()
     {
-        $request = new Request();
-        $request->url('https://localhost.com/test.html');
+        $request = new Request('https://localhost.com/test.html');
         $options = [
             'ssl_verify_host' => true,
             'ssl_verify_peer' => true,
@@ -307,8 +306,7 @@ class StreamTest extends TestCase
      */
     public function testSendContextSslNoVerifyPeerName()
     {
-        $request = new Request();
-        $request->url('https://localhost.com/test.html');
+        $request = new Request('https://localhost.com/test.html');
         $options = [
             'ssl_verify_host' => true,
             'ssl_verify_peer' => true,
@@ -376,26 +374,26 @@ class StreamTest extends TestCase
 
         $responses = $this->stream->createResponses($headers, $content);
         $this->assertCount(3, $responses);
-        $this->assertEquals('close', $responses[0]->header('Connection'));
-        $this->assertEquals('', $responses[0]->body());
-        $this->assertEquals('', $responses[1]->body());
-        $this->assertEquals($content, $responses[2]->body());
-
-        $this->assertEquals(302, $responses[0]->statusCode());
-        $this->assertEquals(302, $responses[1]->statusCode());
-        $this->assertEquals(200, $responses[2]->statusCode());
-
-        $this->assertEquals('value', $responses[0]->cookie('first'));
-        $this->assertEquals(null, $responses[0]->cookie('second'));
-        $this->assertEquals(null, $responses[0]->cookie('third'));
-
-        $this->assertEquals(null, $responses[1]->cookie('first'));
-        $this->assertEquals('val', $responses[1]->cookie('second'));
-        $this->assertEquals(null, $responses[1]->cookie('third'));
-
-        $this->assertEquals(null, $responses[2]->cookie('first'));
-        $this->assertEquals(null, $responses[2]->cookie('second'));
-        $this->assertEquals('works', $responses[2]->cookie('third'));
+        $this->assertEquals('close', $responses[0]->getHeaderLine('Connection'));
+        $this->assertEquals('', (string)$responses[0]->getBody());
+        $this->assertEquals('', (string)$responses[1]->getBody());
+        $this->assertEquals($content, (string)$responses[2]->getBody());
+
+        $this->assertEquals(302, $responses[0]->getStatusCode());
+        $this->assertEquals(302, $responses[1]->getStatusCode());
+        $this->assertEquals(200, $responses[2]->getStatusCode());
+
+        $this->assertEquals('value', $responses[0]->getCookie('first'));
+        $this->assertEquals(null, $responses[0]->getCookie('second'));
+        $this->assertEquals(null, $responses[0]->getCookie('third'));
+
+        $this->assertEquals(null, $responses[1]->getCookie('first'));
+        $this->assertEquals('val', $responses[1]->getCookie('second'));
+        $this->assertEquals(null, $responses[1]->getCookie('third'));
+
+        $this->assertEquals(null, $responses[2]->getCookie('first'));
+        $this->assertEquals(null, $responses[2]->getCookie('second'));
+        $this->assertEquals('works', $responses[2]->getCookie('third'));
     }
 
     /**
@@ -405,8 +403,7 @@ class StreamTest extends TestCase
      */
     public function testKeepDeadline()
     {
-        $request = new Request();
-        $request->url('http://dummy/?sleep');
+        $request = new Request('http://dummy/?sleep');
         $options = [
             'timeout' => 5,
         ];
@@ -426,8 +423,7 @@ class StreamTest extends TestCase
     {
         $this->expectException(\Cake\Network\Exception\HttpException::class);
         $this->expectExceptionMessage('Connection timed out http://dummy/?sleep');
-        $request = new Request();
-        $request->url('http://dummy/?sleep');
+        $request = new Request('http://dummy/?sleep');
         $options = [
             'timeout' => 2,
         ];

+ 5 - 1
tests/TestCase/Http/Client/CookieCollectionTest.php

@@ -19,6 +19,8 @@ use Cake\TestSuite\TestCase;
 
 /**
  * HTTP cookies test.
+ *
+ * @group deprecated
  */
 class CookieCollectionTest extends TestCase
 {
@@ -31,7 +33,9 @@ class CookieCollectionTest extends TestCase
     public function setUp()
     {
         parent::setUp();
-        $this->cookies = new CookieCollection();
+        $this->deprecated(function () {
+            $this->cookies = new CookieCollection();
+        });
     }
 
     /**

+ 35 - 32
tests/TestCase/Http/ClientTest.php

@@ -420,40 +420,43 @@ class ClientTest extends TestCase
     /**
      * Test authentication adapter that mutates request.
      *
+     * @group deprecated
      * @return void
      */
     public function testAuthenticationWithMutation()
     {
-        static::setAppNamespace();
-        $response = new Response();
-        $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
-            ->setMethods(['send'])
-            ->getMock();
-        $headers = [
-            'Authorization' => 'Bearer abc123',
-            'Proxy-Authorization' => 'Bearer abc123',
-        ];
-        $mock->expects($this->once())
-            ->method('send')
-            ->with($this->callback(function ($request) use ($headers) {
-                $this->assertEquals(Request::METHOD_GET, $request->getMethod());
-                $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
-                $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
-                $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
+        $this->deprecated(function () {
+            static::setAppNamespace();
+            $response = new Response();
+            $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
+                ->setMethods(['send'])
+                ->getMock();
+            $headers = [
+                'Authorization' => 'Bearer abc123',
+                'Proxy-Authorization' => 'Bearer abc123',
+            ];
+            $mock->expects($this->once())
+                ->method('send')
+                ->with($this->callback(function ($request) use ($headers) {
+                    $this->assertEquals(Request::METHOD_GET, $request->getMethod());
+                    $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
+                    $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
+                    $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
 
-                return true;
-            }))
-            ->will($this->returnValue([$response]));
-
-        $http = new Client([
-            'host' => 'cakephp.org',
-            'adapter' => $mock
-        ]);
-        $result = $http->get('/', [], [
-            'auth' => ['type' => 'TestApp\Http\CompatAuth'],
-            'proxy' => ['type' => 'TestApp\Http\CompatAuth'],
-        ]);
-        $this->assertSame($result, $response);
+                    return true;
+                }))
+                ->will($this->returnValue([$response]));
+
+            $http = new Client([
+                'host' => 'cakephp.org',
+                'adapter' => $mock
+            ]);
+            $result = $http->get('/', [], [
+                'auth' => ['type' => 'TestApp\Http\CompatAuth'],
+                'proxy' => ['type' => 'TestApp\Http\CompatAuth'],
+            ]);
+            $this->assertSame($result, $response);
+        });
     }
 
     /**
@@ -822,9 +825,9 @@ class ClientTest extends TestCase
 
         $this->assertInstanceOf(Response::class, $result);
         $this->assertTrue($result->isOk());
-        $cookies = $client->cookies()->get($url);
+        $cookies = $client->cookies();
 
-        $this->assertArrayHasKey('redirect1', $cookies);
-        $this->assertArrayHasKey('redirect2', $cookies);
+        $this->assertTrue($cookies->has('redirect1'));
+        $this->assertTrue($cookies->has('redirect2'));
     }
 }