Skip to content
Permalink
Browse files

Get client tests passing.

The PSR7 request can't easily be inspected with attribute helpers.
Switch to using callback() so we can do deeper object inspection.
  • Loading branch information...
markstory committed May 7, 2016
1 parent 2e52015 commit a495cb08183f998aa635b34021396ea87f583905
Showing with 61 additions and 56 deletions.
  1. +1 −0 src/Http/Client.php
  2. +60 −56 tests/TestCase/Network/Http/ClientTest.php
@@ -336,6 +336,7 @@ protected function _doRequest($method, $url, $data, $options)
$data,
$options
);
return $this->send($request, $options);
}
@@ -171,13 +171,15 @@ public function testGetSimpleWithHeadersAndCookies()
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/test.html'),
$this->attributeEqualTo('_headers', $headers),
$this->attributeEqualTo('_cookies', $cookies)
))
->with($this->callback(function ($request) use ($cookies, $headers) {
$this->assertInstanceOf('Cake\Network\Http\Request', $request);
$this->assertEquals(Request::METHOD_GET, $request->getMethod());
$this->assertEquals('http://cakephp.org/test.html', $request->getUri() . '');
$this->assertEquals($cookies, $request->cookies());
$this->assertEquals($headers['Content-Type'], $request->getHeaderLine('content-type'));
$this->assertEquals($headers['Connection'], $request->getHeaderLine('connection'));
return true;
}))
->will($this->returnValue([$response]));
$http = new Client(['adapter' => $mock]);
@@ -200,11 +202,14 @@ public function testGetQuerystring()
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
))
->with($this->callback(function ($request) {
$this->assertEquals(Request::METHOD_GET, $request->getMethod());
$this->assertEquals(
'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
$request->getUri() . ''
);
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([
@@ -230,10 +235,13 @@ public function testGetQuerystringString()
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
))
->with($this->callback(function ($request) {
$this->assertEquals(
'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
$request->getUri() . ''
);
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([
@@ -261,12 +269,12 @@ public function testGetWithContent()
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/search'),
$this->attributeEqualTo('_body', 'some data')
))
->with($this->callback(function ($request) {
$this->assertEquals(Request::METHOD_GET, $request->getMethod());
$this->assertEquals('http://cakephp.org/search', '' . $request->getUri());
$this->assertEquals('some data', '' . $request->getBody());
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([
@@ -311,19 +319,18 @@ public function testGetWithAuthenticationAndProxy()
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$headers = [
'Connection' => 'close',
'User-Agent' => 'CakePHP',
'Authorization' => 'Basic ' . base64_encode('mark:secret'),
'Proxy-Authorization' => 'Basic ' . base64_encode('mark:pass'),
];
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/'),
$this->attributeEqualTo('_headers', $headers)
))
->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([
@@ -367,11 +374,12 @@ public function testMethodsSimple($method)
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('method', $method),
$this->attributeEqualTo('url', new Uri('http://cakephp.org/projects/add'))
))
->with($this->callback(function ($request) use ($method) {
$this->assertInstanceOf('Cake\Network\Http\Request', $request);
$this->assertEquals($method, $request->getMethod());
$this->assertEquals('http://cakephp.org/projects/add', '' . $request->getUri());
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([
@@ -408,20 +416,19 @@ public function testPostWithTypeKey($type, $mime)
$response = new Response();
$data = 'some data';
$headers = [
'Connection' => 'close',
'User-Agent' => 'CakePHP',
'Content-Type' => $mime,
'Accept' => $mime,
];
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->attributeEqualTo('method', Request::METHOD_POST),
$this->attributeEqualTo('_body', $data),
$this->attributeEqualTo('_headers', $headers)
))
->with($this->callback(function ($request) use ($headers) {
$this->assertEquals(Request::METHOD_POST, $request->getMethod());
$this->assertEquals($headers['Content-Type'], $request->getHeaderLine('Content-Type'));
$this->assertEquals($headers['Accept'], $request->getHeaderLine('Accept'));
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([
@@ -440,19 +447,15 @@ public function testPostWithStringDataDefaultsToFormEncoding()
{
$response = new Response();
$data = 'some=value&more=data';
$headers = [
'Connection' => 'close',
'User-Agent' => 'CakePHP',
'Content-Type' => 'application/x-www-form-urlencoded',
];
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->any())
->method('send')
->with($this->logicalAnd(
$this->attributeEqualTo('_body', $data),
$this->attributeEqualTo('_headers', $headers)
))
->with($this->callback(function ($request) use ($data) {
$this->assertEquals($data, '' . $request->getBody());
$this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([
@@ -537,11 +540,12 @@ public function testHeadQuerystring()
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('method', Request::METHOD_HEAD),
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there')
))
->with($this->callback(function ($request) {
$this->assertInstanceOf('Cake\Network\Http\Request', $request);
$this->assertEquals(Request::METHOD_HEAD, $request->getMethod());
$this->assertEquals('http://cakephp.org/search?q=hi+there', '' . $request->getUri());
return true;
}))
->will($this->returnValue([$response]));
$http = new Client([

0 comments on commit a495cb0

Please sign in to comment.
You can’t perform that action at this time.