Skip to content

Commit

Permalink
json, postJson methods: added test cases, updated readme (danwrong#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
disfated committed Jan 8, 2012
1 parent 3f62973 commit 60983d5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
23 changes: 18 additions & 5 deletions README.md
Expand Up @@ -54,6 +54,18 @@ Create a PUT request.

Create a DELETE request.

### head(url, options)

Create a HEAD request.

### json(url, data, options)

Send json `data` via GET method.

### postJson(url, data, options)

Send json `data` via POST method.

### response parsers

You can give any of these to the parsers option to specify how the response data is deserialized.
Expand All @@ -74,6 +86,7 @@ All of these attempt to turn the response into a JavaScript object. In order to
* `data` The data to be added to the body of the request. Can be a string or any object.
Note that if you want your request body to be JSON with the `Content-Type: application/json`, you need to
`JSON.stringify` your object first. Otherwise, it will be sent as `application/x-www-form-urlencoded` and encoded accordingly.
Also you can use `json()` and `postJson()` methods.
* `parser` A function that will be called on the returned data. Use any of predefined `restler.parsers`. See parsers section below. Defaults to `restler.parsers.auto`.
* `encoding` The encoding of the request body. Defaults to `"utf8"`.
* `decoding` The encoding of the response body. For a list of supported values see [Buffers](http://nodejs.org/docs/latest/api/buffers.html#buffers). Additionally accepts `"buffer"` - returns response as `Buffer`. Defaults to `"utf8"`.
Expand Down Expand Up @@ -142,12 +155,12 @@ client.update('Tweeting using a Restler service thingy').on('complete', function
sys.p(data);
});

// the JSON post
rest.post('http://example.com/action', {
data: JSON.stringify({ id: 334 }),
}).on('complete', function(data, response) {
// you can get at the raw response like this...
// post JSON
var jsonData = { id: 334 };
rest.postJson('http://example.com/action', jsonData).on('complete', function(data, response) {
// handle response
});

```

Running the tests
Expand Down
16 changes: 16 additions & 0 deletions lib/restler.js
Expand Up @@ -228,6 +228,20 @@ function head(url, options) {
return request(url, shortcutOptions(options, 'HEAD'));
}

function json(url, data, options, method) {
options = options || {};
options.parser = (typeof options.parser !== "undefined") ? options.parser : parsers.auto;
options.headers = options.headers || {};
options.headers['content-type'] = 'application/json';
options.data = JSON.stringify(data);
options.method = method || 'GET';
return request(url, options);
}

function postJson(url, data, options) {
return json(url, data, options, 'POST');
}

var parsers = {
auto: function(data, callback) {
var contentType = this.headers['content-type'];
Expand Down Expand Up @@ -361,6 +375,8 @@ mixin(exports, {
put: put,
del: del,
head: head,
json: json,
postJson: postJson,
parsers: parsers,
file: multipart.file,
data: multipart.data
Expand Down
33 changes: 33 additions & 0 deletions test/restler.js
Expand Up @@ -254,6 +254,18 @@ function dataResponse(request, response) {
response.writeHead(200);
response.end(Buffer([9, 30, 64, 135, 200]));
break;
case '/push-json':
var echo = '';
request.addListener('data', function(chunk) {
echo += chunk.toString('binary');
});
request.addListener('end', function() {
response.writeHead(200, {
'content-type': 'application/json',
});
response.end(JSON.stringify(JSON.parse(echo)));
});
break;
default:
response.writeHead(404);
response.end();
Expand Down Expand Up @@ -336,6 +348,27 @@ module.exports['Deserialization'] = {
test.equal(data, 'CR5Ah8g=', 'returned: ' + sys.inspect(data));
test.done();
})
},

'Should post and parse JSON': function(test) {
var obj = { secret : 'very secret string' };
rest.post(host + '/push-json', {
headers: {
'content-type': 'application/json'
},
data: JSON.stringify(obj)
}).on('complete', function(data) {
test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
test.done();
})
},

'Should post and parse JSON via shortcut method': function(test) {
var obj = { secret : 'very secret string' };
rest.postJson(host + '/push-json', obj).on('complete', function(data) {
test.equal(obj.secret, data.secret, 'returned: ' + sys.inspect(data));
test.done();
});
}

};
Expand Down

0 comments on commit 60983d5

Please sign in to comment.