This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from /issues/19
Debug script for HTML pipeline
- Loading branch information
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |