Skip to content

Commit

Permalink
test: Partially working test setup in phantom
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Apr 15, 2016
1 parent b2f3553 commit 05f88e8
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 57 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "BSD-3-Clause",
"main": "lib/gofer.js",
"browser": {
"./lib/fetch.js": "./lib/fetch.browser.js"
"./lib/fetch.js": "./lib/fetch.browser.js",
"./test/mock-service.js": "./test/mock-service.browser.js"
},
"homepage": "https://github.com/groupon/gofer",
"repository": {
Expand Down Expand Up @@ -38,7 +39,10 @@
"eslint": "^1.0.0",
"eslint-config-groupon": "^2.0.0",
"mocha": "^2.0.0",
"nlm": "^2.0.0"
"mochify": "^2.17.0",
"nlm": "^2.0.0",
"promise": "^7.1.1",
"whatwg-fetch": "^0.11.0"
},
"author": {
"name": "Groupon",
Expand Down
1 change: 1 addition & 0 deletions test-browser/.eslintrc
1 change: 1 addition & 0 deletions test-browser/gofer.test.js
19 changes: 19 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
var execFile = require('child_process').execFile;

require('./mock-service');

describe('in a browser', function () {
xit('works (almost) just the same', function (done) {
var mochifyBin = require.resolve('.bin/mochify');
this.timeout(60 * 1000);
var child = execFile(mochifyBin, [
'--reporter', 'spec',
'./node_modules/promise/polyfill',
'./node_modules/whatwg-fetch',
'./test-browser/**/*.test.js',
], done);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
});
});
4 changes: 1 addition & 3 deletions test/fetch/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ var assert = require('assertive');

var fetch = require('../..').fetch;

var withMockService = require('../mock-service');
var options = require('../mock-service');

function unexpected() {
throw new Error('Should not happen');
}

describe('fetch: the basics', function () {
var options = withMockService();

it('can load using just a url string', function () {
return fetch(options.baseUrl)
.then(function (res) {
Expand Down
8 changes: 2 additions & 6 deletions test/gofer.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
'use strict';
var util = require('util');

var assert = require('assertive');

var Gofer = require('../');

var withMockService = require('./mock-service');
var options = require('./mock-service');

describe('gofer', function () {
var options = withMockService();

it('exports a `fetch` function', function () {
assert.hasType(Function, Gofer.fetch);
});
Expand Down Expand Up @@ -40,7 +36,7 @@ describe('gofer', function () {
function SubGofer(config) {
Gofer.call(this, config, 'sub', '1.2.3', 'my-sub-client');
}
util.inherits(SubGofer, Gofer);
SubGofer.prototype = Object.create(Gofer.prototype);

var sub;
before('create SubGofer instance', function () {
Expand Down
7 changes: 7 additions & 0 deletions test/mock-service.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
var MOCK_SERVICE_PORT = 3066;

var options = {
baseUrl: 'http://127.0.0.1:' + MOCK_SERVICE_PORT,
};
module.exports = options;
90 changes: 44 additions & 46 deletions test/mock-service.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
'use strict';
var http = require('http');
var parseUrl = require('url').parse;

function withMockService() {
var http = require('http');
var parseUrl = require('url').parse;
var options = require('./mock-service.browser');

var options = {};
var server;
var MOCK_SERVICE_PORT = +(options.baseUrl.match(/:(\d+)/)[1]);

function sendEcho(req, res) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
method: req.method,
url: req.url,
headers: req.headers,
}));
}
var server;

function send404(req, res) {
res.statusCode = 404;
sendEcho(req, res);
}
function sendEcho(req, res) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
method: req.method,
url: req.url,
headers: req.headers,
}));
}

function handleRequest(req, res) {
var pathname = parseUrl(req.url).pathname;
switch (pathname) {
case '/echo':
return sendEcho(req, res);
function send404(req, res) {
res.statusCode = 404;
sendEcho(req, res);
}

case '/json/404':
return send404(req, res);
function handleRequest(req, res) {
var pathname = parseUrl(req.url).pathname;
switch (pathname) {
case '/echo':
return sendEcho(req, res);

default:
return res.end('ok');
}
case '/json/404':
return send404(req, res);

default:
return res.end('ok');
}
}

before(function (done) {
server = http.createServer(handleRequest);
server.on('error', done);
server.listen(function () {
options.baseUrl = 'http://127.0.0.1:' + server.address().port;
done();
});
before(function (done) {
server = http.createServer(handleRequest);
server.on('error', done);
server.listen(MOCK_SERVICE_PORT, function () {
done();
});

after(function () {
if (server) {
try {
server.close();
} catch (e) {
// Ignore cleanup error
}
});

after(function () {
if (server) {
try {
server.close();
} catch (e) {
// Ignore cleanup error
}
});
}
});

return options;
}
module.exports = withMockService;
module.exports = options;

0 comments on commit 05f88e8

Please sign in to comment.