Skip to content

Commit

Permalink
Merge pull request #3 from rjacobsen2012/add-content-type-header
Browse files Browse the repository at this point in the history
Add content type header
  • Loading branch information
Brian Webb committed Apr 7, 2014
2 parents fe2a1f4 + 886a5aa commit 76e9b28
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/Trucker/Transporters/JsonTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class JsonTransporter implements TransporterInterface
/**
* Function to set the appropriate headers on a request object
* to facilitate a JSON transport
*
*
* @param GuzzleHttpMessageRequest $request
*/
public function setHeaderOnRequest(\Guzzle\Http\Message\Request &$request)
{
$request->setHeader('Accept', 'application/json');
$request->setHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]);
}

/**
Expand Down
104 changes: 91 additions & 13 deletions tests/Requests/RestRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public function testGetOption()
public function testSetTransportLanguage()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
]);

$r = $request->createRequest('http://example.com', '/users', 'GET');
Expand All @@ -40,7 +46,13 @@ public function testSetTransportLanguage()
public function testCreateNewRequest()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
]);
$result = $request->createRequest('http://example.com', '/users', 'GET');
$this->assertTrue($result instanceof \Guzzle\Http\Message\Request);
Expand All @@ -51,7 +63,13 @@ public function testCreateNewRequest()
public function testSetPostParameters()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'setPostField', 'args' => ['biz', 'banng']],
]);

Expand All @@ -67,7 +85,13 @@ public function testSetGetParameters()
$mQuery->shouldReceive('add')->with('foo', 'bar');

$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'getQuery', 'return' => $mQuery],
]);

Expand All @@ -80,7 +104,13 @@ public function testSetGetParameters()
public function testSetFileParameters()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'addPostFile', 'args' => ['fileOne', '/path/to/fileOne']],
]);

Expand All @@ -93,7 +123,13 @@ public function testSetFileParameters()
public function testSettingModelProperties()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'setPostField', 'args' => ['foo', 'bar']],
['method' => 'setPostField', 'args' => ['biz', 'bang']],
['method' => 'addPostFile', 'args' => ['fOne', '/path/to/file/one']],
Expand Down Expand Up @@ -123,7 +159,13 @@ public function testSettingModelProperties()
public function testSettingHeaders()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'setHeader', 'args' => ['Cache-Control', 'no-cache, must-revalidate']],
]);

Expand All @@ -139,7 +181,13 @@ public function testAddingErrorHandler()
$dispatcher->shouldReceive('addListener');

$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'getEventDispatcher', 'return' => $dispatcher],
]);

Expand All @@ -156,7 +204,13 @@ public function testAddingErrorHandler()
public function testAddQueryCondition()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
]);
$r = $request->createRequest('http://example.com', '/users', 'GET');

Expand All @@ -171,7 +225,13 @@ public function testAddQueryCondition()
public function testAddQueryResultOrder()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
]);
$r = $request->createRequest('http://example.com', '/users', 'GET');

Expand All @@ -186,7 +246,13 @@ public function testAddQueryResultOrder()
public function testAddAuthentication()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
]);
$r = $request->createRequest('http://example.com', '/users', 'GET');

Expand All @@ -201,7 +267,13 @@ public function testAddAuthentication()
public function testSendRequest()
{
$request = $this->simpleMockRequest([
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'send', 'return' => m::mock('Guzzle\Http\Message\Response')],
]);

Expand All @@ -215,7 +287,13 @@ public function testHttpMethodParam()
{
$request = $this->simpleMockRequest(
[
['method' => 'setHeader', 'args' => ['Accept', 'application/json']],
[
'method' => 'setHeaders',
'args' => [[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]]
],
['method' => 'setPostField', 'args' => ['_method', 'PUT']],
],
'http://example.com',
Expand Down
7 changes: 5 additions & 2 deletions tests/Transporters/JsonTransporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ class JsonTransporterTest extends TruckerTests
public function testSetsHeaderOnRequest()
{
$request = m::mock('Guzzle\Http\Message\Request');
$request->shouldReceive('setHeader')
->with('Accept', 'application/json')
$request->shouldReceive('setHeaders')
->with([
'Accept' => 'application/json',
'Content-Type' => 'application/json'
])
->once();

$transporter = new JsonTransporter;
Expand Down

0 comments on commit 76e9b28

Please sign in to comment.