Skip to content

Commit

Permalink
normalizing/splitting canonical tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khrome committed Feb 2, 2022
1 parent 73173e0 commit 033eaf2
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require('path');
const express = require('express');

module.exports = (Automaton)=>{
return {
render : {
table : (h, b)=>{
let headers = b && h;
let body = b || h;
let result = '';
if(headers) result += `<thead>${body.map((row)=>{
return `<tr>${row.map((item)=>{
return `<th>${item}</th>`
})}</tr>`
})}</thead>`;
result += `<tbody>${body.map((row)=>{
return `<tr>${row.map((item)=>{
return `<td>${item}</td>`
})}</tr>`
})}</tbody>`;
return `<table>${result}</table>`
}
},
endpointsAgainstDefinition : function(endpoints, xml, engine, values, instance, complete){
let done = complete;
let app = instance;
let data = values;
if(typeof instance === 'function' && !complete){
done = instance;
app = null;
}
if(typeof values === 'function' && !complete){
done = values;
data = {};
app = null;
}
if(!app) app = express();
Object.keys(endpoints).forEach((method)=>{
Object.keys(endpoints[method]).forEach((path)=>{
app[method.toLowerCase()](path, endpoints[method][path]);
});
});
let server = app.listen(8080, (err)=>{
if(err) throw err;
let scraper = new Automaton({
body: xml,
environment : data
}, engine);
scraper.run((err, resultData)=>{
done(err, resultData, (cleanupComplete)=>{
server.close(()=>{
if(cleanupComplete) cleanupComplete();
});
});
});
});
}
};
};
5 changes: 5 additions & 0 deletions test/canonical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');

const makeDelayTest = require('./canonical/delay');
const makeUntilExistsTest = require('./canonical/until-exists');

module.exports = (Automaton, should)=>{
return {
testDelayAttribute : makeDelayTest(Automaton, should),
testUntilExistsAttribute : makeUntilExistsTest(Automaton, should),
loadDefinition : function(engine, complete){
fs.readFile(
path.join(
Expand Down
48 changes: 48 additions & 0 deletions test/canonical/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module.exports = (Automaton, should)=>{
const base = require('../base.js')(Automaton);

return (engine, complete)=>{
let startTime = (new Date()).getTime();
base.endpointsAgainstDefinition({
"GET" : {
"/" : (req, res)=>{
res.setHeader('content-type', 'text/html')
res.send(`<html>
<head></head>
<body>${base.render.table([
['foo', 'bar', 'baz']
])}</body>
</html>`);
}
}
}, `
<go url="http://\${host}/" delay="3s">
<set xpath="//table/tbody/tr" push="true" variable="matches">
<set xpath="//td[1]/text()" variable="first"></set>
<set xpath="//td[2]/text()" variable="second"></set>
<set xpath="//td[3]/text()" variable="third"></set>
</set>
<emit variables="matches"></emit>
</go>
`, engine, {
host : 'localhost:8080'
}, (err, data, cleanup)=>{
let finishTime = (new Date()).getTime();
let difference = finishTime - startTime;
difference.should.be.above(3000);
should.not.exist(err);
should.exist(data);
should.exist(data.matches);
should.exist(data.matches[0]);
should.exist(data.matches[0].first);
should.exist(data.matches[0].second);
should.exist(data.matches[0].third);
data.matches[0].first.should.equal('foo');
data.matches[0].second.should.equal('bar');
data.matches[0].third.should.equal('baz');
cleanup(()=>{
complete();
})
});
}
}
55 changes: 55 additions & 0 deletions test/canonical/until-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = (Automaton, should)=>{
const base = require('../base.js')(Automaton);

return (engine, complete)=>{
let startTime = (new Date()).getTime();
base.endpointsAgainstDefinition({
"GET" : {
"/" : (req, res)=>{
res.setHeader('content-type', 'text/html')
res.send(`<html>
<head>
<script>
let toRender = '${base.render.table([
['foo', 'bar', 'baz']
])}';
setTimeout(()=>{
document.body.innerHTML = toRender;
}, 5 * 1000);
</script>
</head>
<body></body>
</html>`);
}
}
}, `
<go url="http://\${host}/" until-exists="//table">
<set xpath="//table/tbody/tr" push="true" variable="matches">
<set xpath="//td[1]/text()" variable="first"></set>
<set xpath="//td[2]/text()" variable="second"></set>
<set xpath="//td[3]/text()" variable="third"></set>
</set>
<emit variables="matches"></emit>
</go>
`, engine, {
host : 'localhost:8080'
}, (err, data, cleanup)=>{
let finishTime = (new Date()).getTime();
let difference = finishTime - startTime;
difference.should.be.above(5000);
should.not.exist(err);
should.exist(data);
should.exist(data.matches);
should.exist(data.matches[0]);
should.exist(data.matches[0].first);
should.exist(data.matches[0].second);
should.exist(data.matches[0].third);
data.matches[0].first.should.equal('foo');
data.matches[0].second.should.equal('bar');
data.matches[0].third.should.equal('baz');
cleanup(()=>{
complete();
})
});
}
}

0 comments on commit 033eaf2

Please sign in to comment.