Skip to content

Commit

Permalink
Cleaning up and adding tests for Pull Request
Browse files Browse the repository at this point in the history
Unit tests added
Slight code refactor to make smaller atomic unit
  • Loading branch information
Nathan committed Sep 13, 2013
1 parent 7c1bac3 commit 3d0eb21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
36 changes: 27 additions & 9 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,13 +809,10 @@ public function _curlPrep()
if (isset($this->payload)) {
$this->serialized_payload = $this->_serializePayload($this->payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload);
if(!$this->isUpload())
if (function_exists('mb_strlen')) {
$length = mb_strlen($this->serialized_payload, '8bit');
} else {
$length = strlen($this->serialized_payload);
}
$this->headers['Content-Length'] = $length ;
if(!$this->isUpload()) {
$this->headers['Content-Length'] =
$this->_determineLength($this->serialized_payload);
}
}

$headers = array();
Expand Down Expand Up @@ -877,11 +874,32 @@ public function _curlPrep()
return $this;
}

public function isUpload() {
/**
* @return int length of payload in bytes
* @param string $str payload
*/
public function _determineLength($str)
{
if (function_exists('mb_strlen')) {
return mb_strlen($str, '8bit');
} else {
return strlen($str);
}
}

/**
* @return bool
*/
public function isUpload()
{
return Mime::UPLOAD == $this->content_type;
}

public function buildUserAgent() {
/**
* @return string
*/
public function buildUserAgent()
{
$user_agent = 'User-Agent: Httpful/' . Httpful::VERSION . ' (cURL/';
$curl = \curl_version();

Expand Down
29 changes: 20 additions & 9 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,24 @@ class HttpfulTest extends \PHPUnit_Framework_TestCase
Transfer-Encoding: chunked
X-My-Header:Value1
X-My-Header:Value2\r\n";

function testInit()
{
$r = Request::init();
// Did we get a 'Request' object?
$this->assertEquals('Httpful\Request', get_class($r));
}

function testDetermineLength()
{
$r = Request::init();
$this->assertEquals(1, $r->_determineLength('A'));
$this->assertEquals(2, $r->_determineLength('À'));
$this->assertEquals(2, $r->_determineLength('Ab'));
$this->assertEquals(3, $r->_determineLength('Àb'));
$this->assertEquals(6, $r->_determineLength('世界'));
}

function testMethods()
{
$valid_methods = array('get', 'post', 'delete', 'put', 'options', 'head');
Expand Down Expand Up @@ -241,7 +252,7 @@ function testDigestAuthSetup()
$this->assertEquals($username, $r->username);
$this->assertEquals($password, $r->password);
$this->assertTrue($r->hasDigestAuth());
}
}

function testJsonResponseParse()
{
Expand Down Expand Up @@ -295,36 +306,36 @@ function testParsingContentTypeCharset()
$this->assertEquals($response->content_type, 'text/plain');
$this->assertEquals($response->charset, 'utf-8');
}

function testParsingContentTypeUpload()
{
$req = Request::init();

$req->sendsType(Mime::UPLOAD);
// $response = new Response(SAMPLE_JSON_RESPONSE, "", $req);
// // Check default content type of iso-8859-1
$this->assertEquals($req->content_type, 'multipart/form-data');
}

function testAttach() {
$req = Request::init();

$req->attach(array('index' => '/dir/filename'));
// $response = new Response(SAMPLE_JSON_RESPONSE, "", $req);
// // Check default content type of iso-8859-1
$this->assertEquals($req->payload['index'], '@/dir/filename');
$this->assertEquals($req->content_type, Mime::UPLOAD);
$this->assertEquals($req->serialize_payload_method, Request::SERIALIZE_PAYLOAD_NEVER);
}

function testIsUpload() {
$req = Request::init();

$req->sendsType(Mime::UPLOAD);

$this->assertTrue($req->isUpload());
}

function testEmptyResponseParse()
{
$req = Request::init()->sendsAndExpects(Mime::JSON);
Expand Down

0 comments on commit 3d0eb21

Please sign in to comment.