Skip to content

Commit

Permalink
user JSON encoding for browser logger
Browse files Browse the repository at this point in the history
Fixes a strange issue in IE
  • Loading branch information
subtleGradient committed Nov 13, 2013
1 parent 37bb9b7 commit b845134
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions grunt/config/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
module.exports = function(grunt){

function testResultLoggerMiddleware(req, res, next) {
if (!(req.body && req.body.data)) return next();
grunt.log.writeln('[%s][%s]', req.headers['user-agent'], Date.now(), req.body.data);
if (!(req.method == 'POST' && req._parsedUrl.pathname.indexOf('/reportTestResults') === 0)) return next();
var logType = 'writeln';
var message = req.body;

if (req.body.type && req.body.message){
if (req.body.type == 'error') logType = 'error';
else if (req.body.message.indexOf('ok') === 0) logType = 'ok';
else if (req.body.message.indexOf('not ok') === 0) logType = 'error';
message = req.body.message;
}
if (typeof message != 'string') message = JSON.stringify(message, null, 2);
grunt.log[logType]('[%s][%s]', req.headers['user-agent'], Date.now(), message);
res.write('<!doctype html><meta charset=utf-8>');
res.end('Got it, thanks!');
}
Expand All @@ -19,10 +29,10 @@ module.exports = function(grunt){
connect.logger.token('timestamp', function(req, res){ return Date.now(); });

return [
connect.logger({format:'[:user-agent][:timestamp] :method :url', stream:grunt.verbose}),
connect.bodyParser(),
connect.json(),
testResultLoggerMiddleware,

connect.logger({format:'[:user-agent][:timestamp] :method :url', stream:grunt.verbose}),
connect.static(options.base),
connect.directory(options.base)
];
Expand Down
4 changes: 2 additions & 2 deletions test/lib/reportTestResults.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ function postDataToURL(data, url, callback) {
callback(request.status == 200 ? null : request.status, request.responseText);
};
request.open('POST', url);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send('data=' + encodeURIComponent(JSON.stringify(data)));
request.setRequestHeader('Content-Type', 'application/json');
request.send(JSON.stringify(data));
}
postDataToURL.defaultCallback = function(error){
// console.log('postDataToURL.defaultCallback', arguments)
Expand Down

0 comments on commit b845134

Please sign in to comment.