user JSON encoding for browser logger

Fixes a strange issue in IE
This commit is contained in:
Thomas Aylott 2013-11-11 15:09:33 -05:00
parent 37bb9b76ab
commit b845134151
2 changed files with 16 additions and 6 deletions

View File

@ -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!');
}
@ -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)
];

View File

@ -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)