Skip to content
Browse files

added JsonMiddleware

  • Loading branch information...
1 parent 683bb67 commit 7044ad3d006df486105ea332cc7e4a48175c8099 @francois2metz committed Oct 29, 2010
Showing with 58 additions and 3 deletions.
  1. +1 −0 Makefile
  2. +9 −1 README.md
  3. +3 −2 examples/test_simple.js
  4. +11 −0 lib/middlewares.js
  5. +34 −0 tests/test_middlewares.js
View
1 Makefile
@@ -1,3 +1,4 @@
test:
node tests/test_client.js
+ node tests/test_middlewares.js
node tests/test_server.js
View
10 README.md
@@ -80,7 +80,7 @@ You can many middlewares:
var auth = new HttpAuthMiddleware();
var xmlserializer = new XmlSerializerMiddleware();
- spore.createClient(auth, xmlserializer __dirname +'/twitter.json');
+ spore.createClient(auth, xmlserializer, __dirname +'/twitter.json');
If a middleware throw exception, then the callback is immediatly called, and err param contain exception.
@@ -147,6 +147,14 @@ Transform body:
response.data = JSON.parse(response.data);
}
+#### FormatJson Middleware
+
+
+Parse JSON response if content-type is application/json.
+
+ var JsonMiddleware = require('spore/middlewares').json`
+ spore.createClient(JsonMiddleware, __dirname +'/twitter.json');
+
## Server
Based on [connect](http://github.com/senchalabs/connect).
View
5 examples/test_simple.js
@@ -1,8 +1,9 @@
require.paths.unshift(__dirname +"/../lib");
var spore = require('spore'),
+ json = require('middlewares').json,
sys = require('sys');
-var twitterClient = spore.createClient({
+var twitterClient = spore.createClient(json, {
"base_url" : "http://api.twitter.com/1",
"version" : "0.1",
"methods" : {
@@ -23,7 +24,7 @@ var twitterClient = spore.createClient({
twitterClient.public_timeline({
format : 'json',
}, function(err, result) {
- var r = JSON.parse(result.body);
+ var r = result.body;
for (var e in r)
{
sys.log(r[e].user.screen_name +' - '+ r[e].text);
View
11 lib/middlewares.js
@@ -0,0 +1,11 @@
+/**
+ * Json middleware
+ */
+exports.json = {
+ response: function(method, response) {
+ if (response.headers['content-type'] &&
+ response.headers['content-type'].search(/application\/json/) != -1) {
+ response.body = JSON.parse(response.body);
+ }
+ }
+};
View
34 tests/test_middlewares.js
@@ -0,0 +1,34 @@
+// :(
+require.paths.unshift(__dirname +"/minitest");
+require.paths.unshift(__dirname +"/../lib");
+
+var JsonMiddleware = require('middlewares').json;
+
+var minitest = require("minitest");
+var assert = require("assert");
+
+minitest.setupListeners();
+
+minitest.context("json middleware", function () {
+ this.setup(function () {
+ this.middleware = JsonMiddleware;
+ this.response = {
+ headers: {},
+ body: '[{"place":null,"text": "node-spore is awesome"}]'
+ };
+ });
+
+ this.assertion("parse json if content type is application/json", function (test) {
+ this.response.headers['content-type'] = 'application/json; charset=utf-8';
+ this.middleware.response({}, this.response);
+ assert.equal(this.response.body.length, 1);
+ assert.equal(this.response.body[0].text, 'node-spore is awesome');
+ test.finished();
+ });
+
+ this.assertion("do nothing with other content type", function (test) {
+ this.middleware.response({}, this.response);
+ assert.equal(this.response.body.length, 48);
+ test.finished();
+ });
+});

0 comments on commit 7044ad3

Please sign in to comment.
Something went wrong with that request. Please try again.