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

Added Request::beforeSend callback. #164

Merged
merged 2 commits into from
Mar 8, 2015
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
22 changes: 21 additions & 1 deletion src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Request
$payload,
$parse_callback,
$error_callback,
$send_callback,
$follow_redirects = false,
$max_redirects = self::MAX_REDIRECTS_DEFAULT,
$payload_serializers = array();
Expand Down Expand Up @@ -648,6 +649,18 @@ public function whenError(\Closure $callback)
return $this;
}

/**
* Callback invoked after payload has been serialized but before
* the request has been built.
* @return Request this
* @param \Closure $callback (Request $request)
*/
public function beforeSend(\Closure $callback)
{
$this->send_callback = $callback;
return $this;
}

/**
* Register a callback that will be used to serialize the payload
* for a particular mime type. When using "*" for the mime
Expand Down Expand Up @@ -825,6 +838,14 @@ public function _curlPrep()
if (!isset($this->uri))
throw new \Exception('Attempting to send a request before defining a URI endpoint.');

if (isset($this->payload)) {
$this->serialized_payload = $this->_serializePayload($this->payload);
}

if (isset($this->send_callback)) {
call_user_func($this->send_callback, $this);
}

$ch = curl_init($this->uri);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
Expand Down Expand Up @@ -876,7 +897,6 @@ public function _curlPrep()
// https://github.com/nategood/httpful/issues/84
// set Content-Length to the size of the payload if present
if (isset($this->payload)) {
$this->serialized_payload = $this->_serializePayload($this->payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload);
if (!$this->isUpload()) {
$this->headers['Content-Length'] =
Expand Down
25 changes: 25 additions & 0 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,31 @@ function testWhenError() {
$this->assertTrue($caught);
}

function testBeforeSend() {
$invoked = false;
$changed = false;
$self = $this;

try {
Request::get('malformed://url')
->beforeSend(function($request) use(&$invoked,$self) {
$self->assertEquals('malformed://url', $request->uri);
$self->assertEquals('A payload', $request->serialized_payload);
$request->uri('malformed2://url');
$invoked = true;
})
->whenError(function($error) { /* Be silent */ })
->body('A payload')
->send();
} catch (\Httpful\Exception\ConnectionErrorException $e) {
$this->assertTrue(strpos($e->getMessage(), 'malformed2') !== false);
$changed = true;
}

$this->assertTrue($invoked);
$this->assertTrue($changed);
}

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