Skip to content

Commit

Permalink
Improve tests for Client\Request (#10762)
Browse files Browse the repository at this point in the history
* Fix array input in method
* Make mehtod for test 4 main method request
* Fix error lint
* Fix test version
* Make dataprovider RequestTest || Fix url
* Make comment method
* Fix request test
  • Loading branch information
mehdi-fathi authored and markstory committed Jun 14, 2017
1 parent 41e9cf1 commit 02b5839
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Http/Client/Request.php
Expand Up @@ -153,7 +153,7 @@ public function header($name = null, $value = null)
* @param array $headers The headers to add.
* @return void
*/
protected function addHeaders($headers)
protected function addHeaders(array $headers)
{
foreach ($headers as $key => $val) {
$normalized = strtolower($key);
Expand Down Expand Up @@ -253,5 +253,5 @@ public function body($body = null)
}
}

// @deprecated Add backwards compat alias.
// @deprecated Add backwards compact alias.
class_alias('Cake\Http\Client\Request', 'Cake\Network\Http\Request');
44 changes: 40 additions & 4 deletions tests/TestCase/Http/Client/RequestTest.php
Expand Up @@ -22,6 +22,7 @@
*/
class RequestTest extends TestCase
{

/**
* test string ata, header and constructor
*
Expand All @@ -37,10 +38,44 @@ public function testConstructorStringData()
$request = new Request('http://example.com', 'POST', $headers, json_encode($data));

$this->assertEquals('http://example.com', $request->url());
$this->assertEquals('POST', $request->getMethod());
$this->assertContains($request->getMethod(), 'POST');
$this->assertEquals('application/json', $request->getHeaderLine('Content-Type'));
$this->assertEquals(json_encode($data), $request->body());
}
/**
* @param array $headers The HTTP headers to set.
* @param array|string|null $data The request body to use.
* @param string $method The HTTP method to use.
*
* @dataProvider additionProvider
*/
public function testMethods(array $headers, $data, $method)
{
$request = new Request('http://example.com', $method, $headers, json_encode($data));

$this->assertEquals($request->getMethod(), $method);
$this->assertEquals('http://example.com', $request->url());
$this->assertEquals('application/json', $request->getHeaderLine('Content-Type'));
$this->assertEquals(json_encode($data), $request->body());
}
/**
* @dataProvider additionProvider
*/
public function additionProvider()
{
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer valid-token',
];
$data = ['a' => 'b', 'c' => 'd'];

return [
[$headers, $data, Request::METHOD_POST],
[$headers, $data, Request::METHOD_GET],
[$headers, $data, Request::METHOD_PUT],
[$headers, $data, Request::METHOD_DELETE],
];
}

/**
* test array data, header and constructor
Expand Down Expand Up @@ -128,14 +163,15 @@ public function testMethod()
}

/**
* test method interop.
* test method interoperability.
*
* @return void
*/
public function testMethodInteroperability()
{
$request = new Request();
$this->assertSame($request, $request->method(Request::METHOD_GET));

$this->assertEquals(Request::METHOD_GET, $request->method());
$this->assertEquals(Request::METHOD_GET, $request->getMethod());

Expand Down Expand Up @@ -293,13 +329,13 @@ public function testVersion()
{
$request = new Request();
$result = $request->version('1.0');
$this->assertSame($request, $request, 'Should return self');
$this->assertSame($request, $result, 'Should return self');

$this->assertSame('1.0', $request->version());
}

/**
* test version interop.
* test version Interoperable.
*
* @return void
*/
Expand Down

0 comments on commit 02b5839

Please sign in to comment.