Skip to content

Commit

Permalink
Add more library to be tested
Browse files Browse the repository at this point in the history
  • Loading branch information
orzFly committed Jan 8, 2018
1 parent 2a9d9e9 commit 03996e4
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 45 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"main": "index.js",
"license": "MIT",
"devDependencies": {
"axios": "^0.17.1",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"isomorphic-fetch": "^2.2.1",
"mocha": "^4.1.0",
"nyc": "^11.4.1"
"node-fetch": "^1.7.3",
"nyc": "^11.4.1",
"request": "^2.83.0"
},
"scripts": {
"test": "node_modules/.bin/nyc node_modules/.bin/mocha",
Expand Down
128 changes: 98 additions & 30 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const expect = require('chai').expect;
const lib = require('..');
const http = require('http');
const https = require('https');
require('isomorphic-fetch');

const assertHijackedContent = function(i) {
expect(i).to.include("<h1>Example Domain</h1>");
Expand All @@ -14,25 +13,48 @@ const assertOriginalContent = function(i) {
expect(i).to.include("<h1>httpbin(1)");
}

const tryHttpGetWithAgent = function(agent, callback) {
http.get({
hostname: 'httpbin.org',
port: 80,
path: '/',
agent: agent
}, (res) => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
callback(rawData);
});
});
}

const tryHttpsGetWithAgent = function(agent, callback) {
https.get({
hostname: 'httpbin.org',
port: 443,
path: '/',
agent: agent
}, (res) => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
callback(rawData);
});
});
}

describe('hijacked-http-agent', () => {

describe('HijackedHttpAgent', () => {

const tryHttpGetWithAgent = function(agent, callback) {
http.get({
hostname: 'httpbin.org',
port: 80,
path: '/',
agent: agent
}, (res) => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
callback(rawData);
});
it('(request works without our agent)', (done) => {
tryHttpGetWithAgent(undefined, (data) => {
assertOriginalContent(data);
done();
});
}
});

it('can be used without mapping', (done) => {
const agent = new lib.HijackedHttpAgent();
Expand Down Expand Up @@ -81,21 +103,12 @@ describe('hijacked-http-agent', () => {

describe('HijackedHttpsAgent', () => {

const tryHttpsGetWithAgent = function(agent, callback) {
https.get({
hostname: 'httpbin.org',
port: 443,
path: '/',
agent: agent
}, (res) => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
callback(rawData);
});
it('(request works without our agent)', (done) => {
tryHttpsGetWithAgent(undefined, (data) => {
assertOriginalContent(data);
done();
});
}
});

it('can be used without mapping', (done) => {
const agent = new lib.HijackedHttpsAgent();
Expand Down Expand Up @@ -156,7 +169,62 @@ describe('hijacked-http-agent', () => {
expect(https.globalAgent).to.be.an.instanceof(lib.HijackedHttpsAgent);
});

describe('should hijack isomorphic-fetch', () => {
describe('should hijack node http & https', () => {
it('http', (done) => {
tryHttpGetWithAgent(undefined, (data) => {
assertHijackedContent(data);
done();
});
});

it('https', (done) => {
tryHttpsGetWithAgent(undefined, (data) => {
assertHijackedContent(data);
done();
});
});

});

describe('should hijack node http & https', () => {

const request = require('request');

it('http', (done) => {
request('http://httpbin.org/', (err, res, body) => {
assertHijackedContent(body);
done();
});
});

it('https', (done) => {
request('https://httpbin.org/', (err, res, body) => {
assertHijackedContent(body);
done();
});
});

});

describe('should hijack axios', () => {

const axios = require('axios');

it('http', () => {
return axios.get("http://httpbin.org/")
.then((response) => assertHijackedContent(response.data))
});

it('https', () => {
return axios.get("https://httpbin.org/")
.then((response) => assertHijackedContent(response.data))
});

});

describe('should hijack node-fetch', () => {

const fetch = require('node-fetch');

it('http', () => {
return fetch("http://httpbin.org/")
Expand Down
28 changes: 15 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ aws4@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e"

axios@^0.17.1:
version "0.17.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.17.1.tgz#2d8e3e5d0bdbd7327f91bc814f5c57660f81824d"
dependencies:
follow-redirects "^1.2.5"
is-buffer "^1.1.5"

babel-code-frame@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
Expand Down Expand Up @@ -517,6 +524,12 @@ find-up@^2.1.0:
dependencies:
locate-path "^2.0.0"

follow-redirects@^1.2.5:
version "1.3.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.3.0.tgz#f684871fc116d2e329fda55ef67687f4fabc905c"
dependencies:
debug "^3.1.0"

for-in@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down Expand Up @@ -797,13 +810,6 @@ isobject@^2.0.0:
dependencies:
isarray "1.0.0"

isomorphic-fetch@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9"
dependencies:
node-fetch "^1.0.1"
whatwg-fetch ">=0.10.0"

isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
Expand Down Expand Up @@ -1060,7 +1066,7 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

node-fetch@^1.0.1:
node-fetch@^1.7.3:
version "1.7.3"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
dependencies:
Expand Down Expand Up @@ -1323,7 +1329,7 @@ repeating@^2.0.0:
dependencies:
is-finite "^1.0.0"

request@^2.79.0:
request@^2.79.0, request@^2.83.0:
version "2.83.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.83.0.tgz#ca0b65da02ed62935887808e6f510381034e3356"
dependencies:
Expand Down Expand Up @@ -1594,10 +1600,6 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

whatwg-fetch@>=0.10.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84"

which-module@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
Expand Down

0 comments on commit 03996e4

Please sign in to comment.