Skip to content

Commit

Permalink
add Resty::putJson() and Resty::patchJson()
Browse files Browse the repository at this point in the history
  • Loading branch information
funkatron committed Feb 13, 2014
1 parent 33860a3 commit 0a6d863
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
54 changes: 53 additions & 1 deletion src/Resty.php
Expand Up @@ -324,12 +324,64 @@ public function delete($url, $querydata = null, $headers = null, $options = null
* @see Resty::sendRequest() the method that sends a request
*/
public function postJson($url, $structure = null, $headers = null, $options = null)
{
return $this->sendJsonRequest($url, 'POST', $structure, $headers, $options);
}

/**
* make a PUT request with a JSON body.
*
* The Content-Type header will be set to 'application/json'
*
* @param string $url the URL. This will be appended to the base_url, if any set
* @param array|stdClass $structure the array or object to send as JSON
* @param array $headers hash of key/val pairs
* @param array $options hash of key/val pairs ('timeout')
* @return array the response hash
* @see Resty::sendRequest() the method that sends a request
*/
public function putJson($url, $structure = null, $headers = null, $options = null)
{
return $this->sendJsonRequest($url, 'PUT', $structure, $headers, $options);
}

/**
* make a PATCH request with a JSON body.
*
* The Content-Type header will be set to 'application/json'
*
* @param string $url the URL. This will be appended to the base_url, if any set
* @param array|stdClass $structure the array or object to send as JSON
* @param array $headers hash of key/val pairs
* @param array $options hash of key/val pairs ('timeout')
* @return array the response hash
* @see Resty::sendRequest() the method that sends a request
*/
public function patchJson($url, $structure = null, $headers = null, $options = null)
{
return $this->sendJsonRequest($url, 'PATCH', $structure, $headers, $options);
}

/**
* A wrapper for Resty::sendRequest that encodes the $structure as JSON
* and sets the Content-Type header will be set to 'application/json'
*
* @param string $url the URL. This will be appended to the base_url, if any set
* @param string $method the HTTP method. This should really only be (POST|PUT|PATCH)
* @param array|stdClass $structure the array or object to send as JSON
* @param array $headers hash of key/val pairs
* @param array $options hash of key/val pairs ('timeout')
* @return array the response hash
* @see Resty::sendRequest() the method that sends a request
* @author Ed Finkler
*/
protected function sendJsonRequest($url, $method, $structure = null, $headers = null, $options = null)
{
if (!$headers) {
$headers = array();
}
$headers['Content-Type'] = 'application/json';
return $this->sendRequest($url, 'POST', json_encode($structure), $headers, $options);
return $this->sendRequest($url, $method, json_encode($structure), $headers, $options);
}


Expand Down
47 changes: 43 additions & 4 deletions tests/tests.php
Expand Up @@ -181,7 +181,7 @@ function exception_error_handler($errno, $errstr, $errfile, $errline)

});

fu::test('httpin GET status responses', function () {
fu::test('httpbin.org GET status responses', function () {

$r = fu::fixture('resty');
$r->setBaseURL(HTTPBIN_URL);
Expand All @@ -197,7 +197,7 @@ function exception_error_handler($errno, $errstr, $errfile, $errline)

});

fu::test('httpin GET JSON decoding', function () {
fu::test('httpbin.org GET JSON decoding', function () {

$r = fu::fixture('resty');
$r->setBaseURL(HTTPBIN_URL);
Expand All @@ -214,7 +214,7 @@ function exception_error_handler($errno, $errstr, $errfile, $errline)

});

fu::test('httpin POST form stuff', function () {
fu::test('httpbin.org POST form stuff', function () {

$r = fu::fixture('resty');
$r->setBaseURL(HTTPBIN_URL);
Expand All @@ -228,15 +228,54 @@ function exception_error_handler($errno, $errstr, $errfile, $errline)

});

fu::test('httpin POST JSON stuff', function () {
fu::test('httpbin.org POST JSON stuff', function () {

$r = fu::fixture('resty');
$r->setBaseURL(HTTPBIN_URL);

$resp = $r->postJson("post", array("foo"=>"bar", "foo2"=>"bar2"));
$req = $r->getLastRequest();
fu::strict_equal('application/json', $req['headers']['Content-Type'], "Request Content-Type is application/json");
fu::strict_equal('POST', $req['method'], "Request method is POST");

fu::has("json", $resp['body']);
fu::has("foo", $resp['body']->json, "foo is in json data");
fu::has("foo2", $resp['body']->json, "foo2 is in json data");
fu::strict_equal("bar", $resp['body']->json->foo, "foo value is correct");
fu::strict_equal("bar2", $resp['body']->json->foo2, "foo2 value is correct");

});

fu::test('httpbin.org PUT JSON stuff', function () {

$r = fu::fixture('resty');
$r->setBaseURL(HTTPBIN_URL);

$resp = $r->putJson("put", array("foo"=>"bar", "foo2"=>"bar2"));
$req = $r->getLastRequest();
fu::strict_equal('application/json', $req['headers']['Content-Type'], "Request Content-Type is application/json");
fu::strict_equal('PUT', $req['method'], "Request method is PUT");

fu::has("json", $resp['body']);
fu::has("foo", $resp['body']->json, "foo is in json data");
fu::has("foo2", $resp['body']->json, "foo2 is in json data");
fu::strict_equal("bar", $resp['body']->json->foo, "foo value is correct");
fu::strict_equal("bar2", $resp['body']->json->foo2, "foo2 value is correct");

});

fu::test('httpbin.org PATCH JSON stuff', function () {

$r = fu::fixture('resty');
$r->setBaseURL(HTTPBIN_URL);

$resp = $r->patchJson("patch", array("foo"=>"bar", "foo2"=>"bar2"));
$req = $r->getLastRequest();

print_r($req);
print_r($resp);
fu::strict_equal('application/json', $req['headers']['Content-Type'], "Request Content-Type is application/json");
fu::strict_equal('PATCH', $req['method'], "Request method is PATCH");

fu::has("json", $resp['body']);
fu::has("foo", $resp['body']->json, "foo is in json data");
Expand Down

0 comments on commit 0a6d863

Please sign in to comment.