Permalink
Browse files

Handle null content gracefully in the xml parser.

  • Loading branch information...
1 parent 73edbb4 commit d4f903562d5d304561bf4fe84c6c6259b490dd93 @typfel typfel committed Jun 11, 2011
Showing with 71 additions and 9 deletions.
  1. +13 −9 lib/wwwdude/parsers.js
  2. +30 −0 test/test_helper.js
  3. +28 −0 test/wwwdude-contentdelivery.js
View
@@ -33,15 +33,19 @@ module.exports = {
},
xml: function xml(content, callback) {
- var Xml2Js = require('xml2js-expat');
- var xmlParser = new Xml2Js.Parser(function (result, error) {
- if (error) {
- callback(error);
- } else {
- callback(null, result);
- }
- });
- xmlParser.parseString(content);
+ if (content) {
+ var Xml2Js = require('xml2js-expat');
+ var xmlParser = new Xml2Js.Parser(function (result, error) {
+ if (error) {
+ callback(error);
+ } else {
+ callback(null, result);
+ }
+ });
+ xmlParser.parseString(content);
+ } else {
+ callback(null, '');
+ }
}
};
View
@@ -117,3 +117,33 @@ exports.redirectServer = function (code) {
serv: server
};
};
+
+exports.nullServer = function () {
+ var port = exports.port;
+ exports.port += 1;
+
+ function handler(request, response, next) {
+ var headers = {
+ 'Content-Type': 'application/json',
+ };
+
+ response.writeHead(200, headers);
+ response.end();
+ server.close();
+ }
+
+ var server = Connect.createServer(
+ Connect.bodyParser(),
+ Gzip(),
+ Connect.router(function (app) {
+ _routeall(app, /null(.*)/, handler);
+ })
+ );
+
+ server.listen(port, 'localhost');
+
+ return {
+ url: 'http://localhost:' + port,
+ serv: server
+ };
+};
@@ -11,6 +11,7 @@ var Helper = require('./test_helper');
var HttpClient = require('../index');
var client = HttpClient.createClient({ contentParser: HttpClient.parsers.json});
+var client2 = HttpClient.createClient({ contentParser: HttpClient.parsers.xml});
function _assertWithPayload(beforeExit, verb, payload) {
var callbacks = 0;
@@ -34,8 +35,27 @@ function _assertWithPayload(beforeExit, verb, payload) {
beforeExit(function () {
assert.strictEqual(callbacks, 2, 'Ensure all callbacks are called');
+ });
+
+}
+
+function _assertWithNullPayload(beforeExit, nullValue, client) {
+ var callbacks = 0;
+ var nullServer = Helper.nullServer();
+
+ client.put(nullServer.url + '/null', { payload: 'payload' })
+ .on('success', function (data, resp) {
+ callbacks += 1;
+ assert.deepEqual(data, nullValue, 'Data must be empty when body is missing');
+ assert.ok(resp, 'Response must be provided');
+ })
+ .on('complete', function (data, resp) {
+ callbacks += 1;
});
+ beforeExit(function () {
+ assert.strictEqual(callbacks, 2, 'Ensure all callbacks are called');
+ });
}
exports.assertPayloadPut = function (beforeExit) {
@@ -45,3 +65,11 @@ exports.assertPayloadPut = function (beforeExit) {
exports.assertPayloadPost = function (beforeExit) {
_assertWithPayload(beforeExit, 'post', '2342HurrDurrDerp!äääää');
};
+
+exports.assertNullPayloadJson = function (beforeExit) {
+ _assertWithNullPayload(beforeExit, {}, client);
+};
+
+exports.assertNullPayloadXml = function (beforeExit) {
+ _assertWithNullPayload(beforeExit, '', client2);
+};

0 comments on commit d4f9035

Please sign in to comment.