@samitouri / QOS-React-2 / commits / b845134151

user JSON encoding for browser logger

Fixes a strange issue in IE

Thomas Aylott committed Nov 11, 2013 at 15:09 UTC b845134151638b1ef05b838eaf4a7d11cf6df898
2 files changed +16 -6
grunt/config/server.js
+14 -4
@@ -1,8 +1,18 @@
1 module.exports = function(grunt){
2
3 function testResultLoggerMiddleware(req, res, next) {
4 - if (!(req.body && req.body.data)) return next();
5 - grunt.log.writeln('[%s][%s]', req.headers['user-agent'], Date.now(), req.body.data);
4 + if (!(req.method == 'POST' && req._parsedUrl.pathname.indexOf('/reportTestResults') === 0)) return next();
5 + var logType = 'writeln';
6 + var message = req.body;
7 +
8 + if (req.body.type && req.body.message){
9 + if (req.body.type == 'error') logType = 'error';
10 + else if (req.body.message.indexOf('ok') === 0) logType = 'ok';
11 + else if (req.body.message.indexOf('not ok') === 0) logType = 'error';
12 + message = req.body.message;
13 + }
14 + if (typeof message != 'string') message = JSON.stringify(message, null, 2);
15 + grunt.log[logType]('[%s][%s]', req.headers['user-agent'], Date.now(), message);
16 res.write('<!doctype html><meta charset=utf-8>');
17 res.end('Got it, thanks!');
18 }
@@ -19,10 +29,10 @@ module.exports = function(grunt){
29 connect.logger.token('timestamp', function(req, res){ return Date.now(); });
30
31 return [
22 - connect.logger({format:'[:user-agent][:timestamp] :method :url', stream:grunt.verbose}),
23 - connect.bodyParser(),
32 + connect.json(),
33 testResultLoggerMiddleware,
34
35 + connect.logger({format:'[:user-agent][:timestamp] :method :url', stream:grunt.verbose}),
36 connect.static(options.base),
37 connect.directory(options.base)
38 ];
test/lib/reportTestResults.browser.js
+2 -2
@@ -42,8 +42,8 @@ function postDataToURL(data, url, callback) {
42 callback(request.status == 200 ? null : request.status, request.responseText);
43 };
44 request.open('POST', url);
45 - request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
46 - request.send('data=' + encodeURIComponent(JSON.stringify(data)));
45 + request.setRequestHeader('Content-Type', 'application/json');
46 + request.send(JSON.stringify(data));
47 }
48 postDataToURL.defaultCallback = function(error){
49 // console.log('postDataToURL.defaultCallback', arguments)