Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from /issues/19
Browse files Browse the repository at this point in the history
Debug script for HTML pipeline
  • Loading branch information
trieloff authored Sep 21, 2018
2 parents d4842eb + 27b577d commit b2fa2e5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/defaults/html.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const responsive = require('../html/responsify-images.js');
const emit = require('../html/emit-html.js');
const type = require('../html/set-content-type.js');
const smartypants = require('../html/smartypants');
const debug = require('../html/output-debug.js');

/* eslint no-param-reassign: off */

Expand All @@ -38,6 +39,7 @@ const htmlpipe = (cont, payload, action) => {
.pre(emit)
.once(cont)
.post(type)
.post(debug)
.post(adaptOWResponse);

action.logger.log('debug', 'Running HTML pipeline');
Expand Down
33 changes: 33 additions & 0 deletions src/html/output-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const DEBUG_TEMPLATE = '<script>console.group(\'payload\');console.log(PAYLOAD_JSON);console.groupEnd();</script>';

function debug(payload, { logger }) {
const isDebug = payload.request && payload.request.params && (payload.request.params.debug === true || payload.request.params.debug === 'true');
const hasBody = payload.response && payload.response.body;
if (isDebug && hasBody) {
logger.debug('Adding debug script');
const p = payload;
// backup body
const { body } = p.response;
// remove body because that would be the response content
// and causes rendering issues of the script
delete p.response.body;
const debugScript = DEBUG_TEMPLATE.replace(/PAYLOAD_JSON/, JSON.stringify(p));
// inject debug script before the closing body tag
p.response.body = body.replace(/<\/body>/i, `${debugScript}</body>`);
return p;
}
return payload;
}
module.exports = debug;
module.exports.DEBUG_TEMPLATE = DEBUG_TEMPLATE;
2 changes: 1 addition & 1 deletion test/testDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Testing Default Pipeline', () => {
assert.ok(log, 'no logger found');
});

it('creates a runs the default pipeline', async () => {
it('creates and runs the default pipeline', async () => {
const out = await pipe((payload, action) => ({
body: `test. payload: ${payload.title} action: ${action.title}`,
}), {
Expand Down
68 changes: 68 additions & 0 deletions test/testOutputDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
const assert = require('assert');
const winston = require('winston');
const _ = require('lodash');
const debug = require('../src/html/output-debug');

const logger = winston.createLogger({
// tune this for debugging
level: 'debug',
// and turn this on if you want the output
silent: true,
format: winston.format.simple(),
transports: [new winston.transports.Console()],
});

describe('Test outputDebug', () => {
function getPayload() {
return {
request: {
params: {
debug: true,
},
},
response: {
body: '<html><body></body></html>',
},
};
}

function computeExpectedOutput(payload) {
const p = _.merge({}, payload);
const { body } = p.response;
delete p.response.body;
const debugScript = debug.DEBUG_TEMPLATE.replace(/PAYLOAD_JSON/, JSON.stringify(p));
p.response.body = body.replace(/<\/body>/i, `${debugScript}</body>`);
return p;
}

it('Testing no debug', () => {
const payload = getPayload();
payload.request.params.debug = false;
assert.deepEqual(debug(payload, { logger }), payload);
});

it('Testing simple payload', () => {
const payload = getPayload();
const expected = computeExpectedOutput(payload);
assert.deepEqual(debug(payload, { logger }), expected);
});

it('Testing upper case body tag', () => {
const payload = getPayload();
payload.response.body = payload.response.body.toUpperCase();
const expected = computeExpectedOutput(payload);
assert.deepEqual(debug(payload, { logger }), expected);
});
});

0 comments on commit b2fa2e5

Please sign in to comment.