Skip to content

Commit

Permalink
Add test that inserts Chaperon iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbrisebois committed Dec 7, 2019
1 parent 069919b commit 88e40bd
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 29 deletions.
2 changes: 2 additions & 0 deletions html/chaperon/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<h1>Chaperon!</h1>
14 changes: 14 additions & 0 deletions html/happ/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

<h1>hApp!</h1>

<div id="main-content">Hello World</div>

<script type="text/javascript">
window.addEventListener( "message", function ( event ) {
console.log( event );
console.log( event.origin );
console.log( event.source );
console.log( event.data );
});
</script>

57 changes: 29 additions & 28 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,36 @@ const fs = require('fs');
const HAPP_PORT = 4531;
const CHAP_PORT = 4532;

const base_dir = ".";

function handle_requests (request, response) {
try {
const req_url = url.parse( request.url );
function create_server ( base_dir, port ) {
const server = http.createServer(function (request, response) {
try {
const req_url = url.parse( request.url );

const fs_path = base_dir + path.normalize( req_url.pathname );
const fs_path = base_dir + path.normalize( req_url.pathname );

log.normal("Looking for file @ %s", fs_path );
var file_stream = fs.createReadStream( fs_path );
file_stream.pipe( response );
file_stream.on('open', function () {
response.writeHead( 200 );
});
file_stream.on('error',function(e) {
// assume the file doesn't exist
response.writeHead( 404 );
log.normal("Looking for file @ %s", fs_path );
var file_stream = fs.createReadStream( fs_path );
file_stream.pipe( response );
file_stream.on('open', function () {
response.writeHead( 200 );
});
file_stream.on('error',function(e) {
// assume the file doesn't exist
response.writeHead( 404 );
response.end();
});
} catch( e ) {
log.fatal("Failed to process request: %s", e );
console.error( e );

response.writeHead( 500 );
response.end();
});
} catch( e ) {
log.fatal("Failed to process request: %s", e );
console.error( e );

response.writeHead( 500 );
response.end();
}
}
});

server.listen( port );

return server;
}

function async_wrapper ( fn ) {
Expand All @@ -51,11 +55,8 @@ function async_wrapper ( fn ) {
}

function main () {
const happ_server = http.createServer( handle_requests );
const chap_server = http.createServer( handle_requests );

happ_server.listen( HAPP_PORT );
chap_server.listen( CHAP_PORT );
const happ_server = create_server( "./html/happ", HAPP_PORT );
const chap_server = create_server( "./html/chaperon", CHAP_PORT );

return {
"servers": {
Expand Down
76 changes: 76 additions & 0 deletions tests/unit/test_comb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const path = require('path');
const log = require('@whi/stdlog')(path.basename( __filename ), {
level: process.env.LOG_LEVEL || 'fatal',
});

const fs = require('fs');
const assert = require('assert');
const axios = require('axios');
const expect = require('chai').expect;
const puppeteer = require('puppeteer');

const http_servers = require('../setup.js');


describe("Testing COMB", function() {

it('should insert Chaperon iframe into hApp window', async function () {
// const setup = http_servers();
// log.debug("Setup config: %s", setup );

// try {
// const happ_url = `http://localhost:${setup.ports.happ}/index.html`;
// log.info("Fetch: %s", happ_url );
// const happ_resp = await axios.get( happ_url );

// expect( happ_resp.status ).to.equal( 200 );

// const chap_url = `http://localhost:${setup.ports.chaperon}/index.html`;
// log.info("Fetch: %s", chap_url );
// const chap_resp = await axios.get( chap_url );

// expect( chap_resp.status ).to.equal( 200 );
// } finally {
// await setup.close();
// }

const setup = http_servers();
log.debug("Setup config: %s", setup.ports );
const browser = await puppeteer.launch();

try {
const happ_url = `http://localhost:${setup.ports.happ}/index.html`;
const chap_url = `http://localhost:${setup.ports.chaperon}/index.html`;

const page = await browser.newPage();

log.info("Go to: %s", happ_url );
await page.goto( happ_url );

log.info("Add Chaperon iFrame: %s", chap_url );
await page.evaluate(( url ) => {
const container = document.createElement("div");
container.innerHTML = `<iframe src="${url}"></iframe>`;

document.body.appendChild( container );
}, chap_url );

const parent = page.mainFrame();
const frames = parent.childFrames();
log.debug("Frames: %s", frames.length );

expect( frames.length ).to.equal( 1 );

const chap_frame = frames[0];

await chap_frame.waitForNavigation();

expect( frames[0].url() ).to.equal( chap_url );

} finally {
await browser.close();
await setup.close();
}
});

});
2 changes: 1 addition & 1 deletion tests/unit/test_puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const puppeteer = require('puppeteer');
const http_servers = require('../setup.js');


describe("Testing setup for tests", function() {
describe("Testing puppeteer with test servers", function() {

it('should start test servers, connect with puppeteer, then close', async function () {
const setup = http_servers();
Expand Down

0 comments on commit 88e40bd

Please sign in to comment.