Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload feature #89

Merged
merged 4 commits into from
Sep 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Httpful/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
*/
class Mime
{
const JSON = 'application/json';
const XML = 'application/xml';
const XHTML = 'application/html+xml';
const FORM = 'application/x-www-form-urlencoded';
const PLAIN = 'text/plain';
const JS = 'text/javascript';
const HTML = 'text/html';
const YAML = 'application/x-yaml';
const CSV = 'text/csv';
const JSON = 'application/json';
const XML = 'application/xml';
const XHTML = 'application/html+xml';
const FORM = 'application/x-www-form-urlencoded';
const UPLOAD = 'multipart/form-data';
const PLAIN = 'text/plain';
const JS = 'text/javascript';
const HTML = 'text/html';
const YAML = 'application/x-yaml';
const CSV = 'text/csv';

/**
* Map short name for a mime type
Expand All @@ -28,6 +29,7 @@ class Mime
'form' => self::FORM,
'plain' => self::PLAIN,
'text' => self::PLAIN,
'upload' => self::UPLOAD,
'html' => self::HTML,
'xhtml' => self::XHTML,
'js' => self::JS,
Expand Down
30 changes: 25 additions & 5 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function send()
$this->_curlPrep();

$result = curl_exec($this->_ch);

if ($result === false) {
$this->_error(curl_error($this->_ch));
throw new ConnectionErrorException('Unable to connect.');
Expand Down Expand Up @@ -333,6 +333,9 @@ public function mime($mime)
{
if (empty($mime)) return $this;
$this->content_type = $this->expected_type = Mime::getFullMime($mime);
if($this->isUpload()) {
$this->neverSerializePayload();
}
return $this;
}
// @alias of mime
Expand Down Expand Up @@ -374,6 +377,15 @@ public function expectsType($mime)
{
return $this->expects($mime);
}

public function attach($files) {
foreach ($files as $key => $file) {
$this->payload[$key] = "@{$file}";
}

$this->sendsType(Mime::UPLOAD);
return $this;
}

/**
* @return Request this
Expand All @@ -383,6 +395,9 @@ public function contentType($mime)
{
if (empty($mime)) return $this;
$this->content_type = Mime::getFullMime($mime);
if($this->isUpload()) {
$this->neverSerializePayload();
}
return $this;
}
// @alias of contentType
Expand Down Expand Up @@ -437,7 +452,7 @@ public function useProxy($proxy_host, $proxy_port = 80, $auth_type = null, $auth
* @return is this request setup for using proxy?
*/
public function hasProxy(){
return is_string($this->additional_curl_opts[CURLOPT_PROXY]);
return isset($this->additional_curl_opts[CURLOPT_PROXY]) && is_string($this->additional_curl_opts[CURLOPT_PROXY]);
}

/**
Expand Down Expand Up @@ -794,7 +809,8 @@ public function _curlPrep()
if (isset($this->payload)) {
$this->serialized_payload = $this->_serializePayload($this->payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload);
$this->headers['Content-Length'] = strlen($this->serialized_payload);
if(!$this->isUpload())
$this->headers['Content-Length'] = strlen($this->serialized_payload) ;
}

$headers = array();
Expand All @@ -821,7 +837,7 @@ public function _curlPrep()
}

// Solve a bug on squid proxy, NONE/411 when miss content length
if (!isset($this->headers['Content-Length'])) {
if (!isset($this->headers['Content-Length']) && !$this->isUpload()) {
$this->headers['Content-Length'] = 0;
}

Expand Down Expand Up @@ -850,11 +866,15 @@ public function _curlPrep()
foreach ($this->additional_curl_opts as $curlopt => $curlval) {
curl_setopt($ch, $curlopt, $curlval);
}

$this->_ch = $ch;

return $this;
}

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

public function buildUserAgent() {
$user_agent = 'User-Agent: Httpful/' . Httpful::VERSION . ' (cURL/';
Expand Down
33 changes: 32 additions & 1 deletion tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ function testShortMime()
$this->assertEquals(Mime::XML, Mime::getFullMime('xml'));
$this->assertEquals(Mime::HTML, Mime::getFullMime('html'));
$this->assertEquals(Mime::CSV, Mime::getFullMime('csv'));
$this->assertEquals(Mime::UPLOAD, Mime::getFullMime('upload'));

// Valid long ones
$this->assertEquals(Mime::JSON, Mime::getFullMime(Mime::JSON));
$this->assertEquals(Mime::XML, Mime::getFullMime(Mime::XML));
$this->assertEquals(Mime::HTML, Mime::getFullMime(Mime::HTML));
$this->assertEquals(Mime::CSV, Mime::getFullMime(Mime::CSV));
$this->assertEquals(Mime::UPLOAD, Mime::getFullMime(Mime::UPLOAD));

// No false positives
$this->assertNotEquals(Mime::XML, Mime::getFullMime(Mime::HTML));
Expand Down Expand Up @@ -293,7 +295,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