Skip to content

Commit

Permalink
Merge pull request #131 from shimonchayim/master
Browse files Browse the repository at this point in the history
Fix for issue #98
  • Loading branch information
Subbu Allamaraju committed Dec 6, 2011
2 parents 39ce88a + fbc1905 commit 18d8476
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
5 changes: 4 additions & 1 deletion modules/engine/lib/engine/http.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ function sendOneRequest(args, resourceUri, params, holder, cb) {

// Parse
try {
if(mediaType.subtype === 'xml') {
if(!respData || /^\s*$/.test(respData)){
respJson = {};
}
else if(mediaType.subtype === 'xml') {
respJson = expat.toJson(respData, {object: true});
}
else if(mediaType.subtype === 'json') {
Expand Down
156 changes: 156 additions & 0 deletions modules/engine/test/empty-json-handle-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright 2011 eBay Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var _ = require('underscore'),
Engine = require('../lib/engine'),
sys = require('sys'),
http = require('http'),
fs = require('fs'),
util = require('util');

var engine = new Engine({
config: __dirname + '/config/dev.json',
connection: 'close'
});

module.exports = {
'emtpy response with 200 json': function(test) {
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type' : 'application/json',
'Content-Length' : 0
});
res.write(" \r\n\t \r\n\t \r\n\t")
res.end();
});
server.listen(3000, function() {
// Do the test here.
var engine = new Engine({
connection : 'close'
});
var script = fs.readFileSync(__dirname + '/mock/empty-json-resp.ql', 'UTF-8');

engine.exec(script, function(err, result) {

if (err) {
console.log(err.stack || sys.inspect(err, false, 10));
test.fail('got error');
test.done();
}
else {
test.equals(result.headers['content-type'], 'application/json', 'json expected');
test.equals(result.body, null, 'null expected');
test.done();
}
server.close();
});
});
},
'emtpy response with error code json': function(test) {
var server = http.createServer(function(req, res) {
res.writeHead(502, {
'Content-Type' : 'application/json',
'Content-Length' : 0
});
res.write(" \r\n\t \r\n\t \r\n\t")
res.end();
});
server.listen(3000, function() {
// Do the test here.
var engine = new Engine({
connection : 'close'
});
var script = fs.readFileSync(__dirname + '/mock/empty-json-resp.ql', 'UTF-8');

engine.exec(script, function(err, result) {

if (err) {
test.equals(err.headers['content-type'], 'application/json', 'json expected');
test.equals(JSON.stringify(err.body), '{}', 'empty expected');
test.done();
}
else {
test.fail('failure expected got success');
test.done();
}
server.close();
});
});
},
'emtpy response with 200 xml': function(test) {
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type' : 'application/xml',
'Content-Length' : 0
});
res.write(" \r\n\t \r\n\t \r\n\t")
res.end();
});
server.listen(3000, function() {
// Do the test here.
var engine = new Engine({
connection : 'close'
});
var script = fs.readFileSync(__dirname + '/mock/empty-json-resp.ql', 'UTF-8');

engine.exec(script, function(err, result) {

if (err) {
console.log(err.stack || sys.inspect(err, false, 10));
test.fail('got error');
test.done();
}
else {
test.equals(result.headers['content-type'], 'application/json', 'json expected');
test.equals(result.body, null, 'null expected');
test.done();
}
server.close();
});
});
},
'emtpy response with error code xml': function(test) {
var server = http.createServer(function(req, res) {
res.writeHead(502, {
'Content-Type' : 'application/xml',
'Content-Length' : 0
});
res.write(" \r\n\t \r\n\t \r\n\t")
res.end();
});
server.listen(3000, function() {
// Do the test here.
var engine = new Engine({
connection : 'close'
});
var script = fs.readFileSync(__dirname + '/mock/empty-json-resp.ql', 'UTF-8');

engine.exec(script, function(err, result) {

if (err) {
test.equals(err.headers['content-type'], 'application/json', 'json expected');
test.equals(JSON.stringify(err.body), '{}', 'empty expected');
test.done();
}
else {
test.fail('failure expected got success');
test.done();
}
server.close();
});
});
}
}
6 changes: 6 additions & 0 deletions modules/engine/test/mock/empty-json-resp.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

create table items
on select get from "http://localhost:3000/FindItemsByKeywordsResponse.xml"
resultset 'findItemsByKeywordsResponse';

return select * from items;

0 comments on commit 18d8476

Please sign in to comment.