From a57cd25bece34f98f02fb573f0b35cb9133ce80a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Mon, 18 Dec 2017 12:35:44 +0100 Subject: [PATCH] test escape chars function --- test/unit/cartodb/error-middleware.test.js | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/unit/cartodb/error-middleware.test.js b/test/unit/cartodb/error-middleware.test.js index 4e235c4a..861bec54 100644 --- a/test/unit/cartodb/error-middleware.test.js +++ b/test/unit/cartodb/error-middleware.test.js @@ -122,4 +122,57 @@ describe('error-middleware', function() { done(); }); + + it('should escape chars that broke logs regex', function (done) { + const badString = 'error: ( ) = " \" \' * $ & |'; + const escapedString = 'error '; + + const error = new Error(badString); + error.label = badString; + error.type = badString; + error.subtype = badString; + + const errors = [error, error]; + + const req = {}; + const res = { + headers: {}, + set (key, value) { + this.headers[key] = value; + }, + statusCode: 0, + status (status) { + this.statusCode = status; + }, + json () {}, + send () {} + }; + + const errorHeader = { + mainError: { + statusCode: 400, + message: escapedString, + name: error.name, + label: escapedString, + type: escapedString, + subtype: escapedString, + }, + moreErrors: [{ + message: escapedString, + name: error.name, + label: escapedString, + type: escapedString, + subtype: escapedString + }] + }; + + const errorFn = errorMiddleware(); + errorFn(errors, req, res); + + assert.deepEqual(res.headers, { + 'X-Tiler-Errors': JSON.stringify(errorHeader) + }); + + done(); + }); });