Skip to content

Commit

Permalink
Fix warnings in Http/Client tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 16, 2017
1 parent 359a60b commit 792f1b9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/Http/Client.php
Expand Up @@ -16,8 +16,8 @@
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;
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Client/Adapter/Stream.php
Expand Up @@ -184,8 +184,8 @@ protected function _buildContent(Request $request, $options)
*/
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'])) {
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Client/Request.php
Expand Up @@ -208,8 +208,8 @@ protected function addHeaders(array $headers)
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.'
);
Expand Down Expand Up @@ -272,7 +272,7 @@ public function body($body = null)
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');
Expand Down
124 changes: 60 additions & 64 deletions tests/TestCase/Http/Client/Adapter/StreamTest.php
Expand Up @@ -122,10 +122,10 @@ public function tearDown()
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, []);
Expand All @@ -143,8 +143,7 @@ public function testSend()
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]);
Expand All @@ -159,24 +158,26 @@ public function testSendByUsingCakephpProtocol()
*/
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,
];
$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']);
Expand All @@ -191,22 +192,22 @@ public function testBuildingContextHeader()
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
];
$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']);
Expand All @@ -219,19 +220,21 @@ public function testSendContextContentString()
*/
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']);
Expand All @@ -245,21 +248,18 @@ public function testSendContextContentArray()
*/
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']);
}
Expand All @@ -271,8 +271,7 @@ public function testSendContextContentArrayFiles()
*/
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,
Expand Down Expand Up @@ -307,8 +306,7 @@ public function testSendContextSsl()
*/
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,
Expand Down Expand Up @@ -376,26 +374,26 @@ public function testCreateResponseWithRedirects()

$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'));
}

/**
Expand All @@ -405,8 +403,7 @@ public function testCreateResponseWithRedirects()
*/
public function testKeepDeadline()
{
$request = new Request();
$request->url('http://dummy/?sleep');
$request = new Request('http://dummy/?sleep');
$options = [
'timeout' => 5,
];
Expand All @@ -426,8 +423,7 @@ public function testMissDeadline()
{
$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,
];
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/Http/Client/CookieCollectionTest.php
Expand Up @@ -19,6 +19,8 @@

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

/**
Expand Down
67 changes: 35 additions & 32 deletions tests/TestCase/Http/ClientTest.php
Expand Up @@ -420,40 +420,43 @@ public function testGetWithAuthenticationAndProxy()
/**
* 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);
});
}

/**
Expand Down Expand Up @@ -822,9 +825,9 @@ public function testRedirects()

$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'));
}
}

0 comments on commit 792f1b9

Please sign in to comment.