Skip to content

Commit 4f3453d

Browse files
committed
mocking proxy requests
1 parent 6241b69 commit 4f3453d

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"devDependencies": {
5454
"chai": "^3.0.0",
5555
"mocha": "^2.2.5",
56+
"nock": "^2.10.0",
5657
"supertest": "^1.0.1"
5758
},
5859
"main": "index.js",

test/params.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var assert = require('chai').assert;
22
var supertest = require('supertest');
3+
var nock = require('nock');
4+
var async = require('async');
35
// Helper functions for the FS
46
var rm = require('./test-utils').rm;
57
var write = require('./test-utils').write;
@@ -11,7 +13,6 @@ var ldnode = require('../index');
1113
describe('LDNODE params', function () {
1214

1315
describe('proxy', function() {
14-
this.timeout(10000);
1516

1617
var ldp = ldnode({
1718
root: __dirname + '/resources',
@@ -20,14 +21,46 @@ describe('LDNODE params', function () {
2021
var server = supertest(ldp);
2122

2223
it('should return the website in /proxy?uri', function(done) {
23-
server.get('/proxy?uri=https://google.com')
24-
.set('Origin', 'http://example.com')
25-
.expect('Access-Control-Allow-Origin', 'http://example.com')
24+
nock('https://amazingwebsite.tld').get('/').reply(200);
25+
server.get('/proxy?uri=https://amazingwebsite.tld/')
2626
.expect(200, done);
2727
});
2828

2929
it('should also work on /proxy/ ?uri', function(done) {
30-
server.get('/proxy/?uri=https://google.com')
30+
nock('https://amazingwebsite.tld').get('/').reply(200);
31+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
32+
.expect(200, done);
33+
});
34+
35+
it('should return the same HTTP status code as the uri', function(done) {
36+
async.parallel([
37+
// 500
38+
function(next) {
39+
nock('https://amazingwebsite.tld').get('/404').reply(404);
40+
server.get('/proxy/?uri=https://amazingwebsite.tld/404')
41+
.expect(404, next);
42+
},
43+
function(next) {
44+
nock('https://amazingwebsite.tld').get('/401').reply(401);
45+
server.get('/proxy/?uri=https://amazingwebsite.tld/401')
46+
.expect(401, next);
47+
},
48+
function(next) {
49+
nock('https://amazingwebsite.tld').get('/500').reply(500);
50+
server.get('/proxy/?uri=https://amazingwebsite.tld/500')
51+
.expect(500, next);
52+
},
53+
function(next) {
54+
nock('https://amazingwebsite.tld').get('/').reply(200);
55+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
56+
.expect(200, next);
57+
}
58+
], done);
59+
});
60+
61+
it('should work with cors', function(done) {
62+
nock('https://amazingwebsite.tld').get('/').reply(200);
63+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
3164
.set('Origin', 'http://example.com')
3265
.expect('Access-Control-Allow-Origin', 'http://example.com')
3366
.expect(200, done);

0 commit comments

Comments
 (0)