Skip to content

Commit 5460f85

Browse files
author
Andrei
committed
Merge pull request #343 from solid/proxy-es6
Moving tests and converting proxy to es6
2 parents f64eebc + 3822855 commit 5460f85

3 files changed

Lines changed: 118 additions & 113 deletions

File tree

lib/handlers/proxy.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
module.exports = addProxy
22

3-
var request = require('request')
4-
var cors = require('cors')
5-
var http = require('http')
6-
var https = require('https')
7-
var debug = require('../debug')
8-
var url = require('url')
3+
const cors = require('cors')
4+
const http = require('http')
5+
const https = require('https')
6+
const debug = require('../debug')
7+
const url = require('url')
98
const isIp = require('is-ip')
109
const ipRange = require('ip-range-check')
1110
const validUrl = require('valid-url')
@@ -20,7 +19,7 @@ function addProxy (app, path) {
2019
maxAge: 1728000,
2120
origin: true
2221
}),
23-
function (req, res) {
22+
(req, res) => {
2423
if (!validUrl.isUri(req.query.uri)) {
2524
return res
2625
.status(406)
@@ -29,7 +28,7 @@ function addProxy (app, path) {
2928

3029
debug.settings('proxy received: ' + req.originalUrl)
3130

32-
var hostname = url.parse(req.query.uri).hostname
31+
const hostname = url.parse(req.query.uri).hostname
3332

3433
if (isIp(hostname) && (
3534
ipRange(hostname, '10.0.0.0/8') ||
@@ -40,7 +39,7 @@ function addProxy (app, path) {
4039
.status(406)
4140
.send('Cannot proxy this IP')
4241
}
43-
var uri = req.query.uri
42+
const uri = req.query.uri
4443
if (!uri) {
4544
return res
4645
.status(400)
@@ -49,7 +48,8 @@ function addProxy (app, path) {
4948

5049
debug.settings('Proxy destination URI: ' + uri)
5150

52-
var protocol = uri.split(':')[0]
51+
const protocol = uri.split(':')[0]
52+
let request
5353
if (protocol === 'http') {
5454
request = http.get
5555
} else if (protocol === 'https') {
@@ -59,24 +59,24 @@ function addProxy (app, path) {
5959
}
6060

6161
// Set the headers and uri of the proxied request
62-
var opts = url.parse(uri)
62+
const opts = url.parse(uri)
6363
opts.headers = req.headers
6464
// See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
6565
delete opts.headers.connection
6666
delete opts.headers.host
6767

68-
var _req = request(opts, function (_res) {
68+
const _req = request(opts, (_res) => {
6969
res.status(_res.statusCode)
7070
// Set the response with the same header of proxied response
71-
Object.keys(_res.headers).forEach(function (header) {
71+
Object.keys(_res.headers).forEach((header) => {
7272
if (!res.get(header)) {
7373
res.setHeader(header.trim(), _res.headers[header])
7474
}
7575
})
7676
_res.pipe(res)
7777
})
7878

79-
_req.on('error', function (e) {
79+
_req.on('error', (e) => {
8080
res.send(500, 'Cannot proxy')
8181
})
8282

test/params.js

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
var assert = require('chai').assert
22
var supertest = require('supertest')
3-
var nock = require('nock')
4-
var async = require('async')
53
var path = require('path')
64
// Helper functions for the FS
75
var rm = require('./test-utils').rm
@@ -12,103 +10,6 @@ var read = require('./test-utils').read
1210
var ldnode = require('../index')
1311

1412
describe('LDNODE params', function () {
15-
describe('proxy', function () {
16-
var ldp = ldnode({
17-
root: path.join(__dirname, '/resources'),
18-
proxy: '/proxy'
19-
})
20-
var server = supertest(ldp)
21-
22-
it('should return the website in /proxy?uri', function (done) {
23-
nock('https://amazingwebsite.tld').get('/').reply(200)
24-
server.get('/proxy?uri=https://amazingwebsite.tld/')
25-
.expect(200, done)
26-
})
27-
28-
it('should return error on local network requests', function (done) {
29-
nock('https://192.168.0.0').get('/').reply(200)
30-
server.get('/proxy?uri=https://192.168.0.0/')
31-
.expect(406, done)
32-
})
33-
34-
it('should return error on invalid uri', function (done) {
35-
server.get('/proxy?uri=HELLOWORLD')
36-
.expect(406, done)
37-
})
38-
39-
it('should return error on relative paths', function (done) {
40-
server.get('/proxy?uri=../')
41-
.expect(406, done)
42-
})
43-
44-
it('should return the same headers of proxied request', function (done) {
45-
nock('https://amazingwebsite.tld')
46-
.get('/')
47-
.reply(function (uri, req) {
48-
if (this.req.headers['accept'] !== 'text/turtle') {
49-
throw Error('Accept is received on the header')
50-
}
51-
if (this.req.headers['test'] && this.req.headers['test'] === 'test1') {
52-
return [200, 'YES']
53-
} else {
54-
return [500, 'empty']
55-
}
56-
})
57-
58-
server.get('/proxy?uri=https://amazingwebsite.tld/')
59-
.set('test', 'test1')
60-
.set('accept', 'text/turtle')
61-
.expect(200)
62-
.end(function (err, data) {
63-
if (err) return done(err)
64-
done(err)
65-
})
66-
})
67-
68-
it('should also work on /proxy/ ?uri', function (done) {
69-
nock('https://amazingwebsite.tld').get('/').reply(200)
70-
server.get('/proxy/?uri=https://amazingwebsite.tld/')
71-
.expect(function (a) {
72-
assert.equal(a.header['link'], null)
73-
})
74-
.expect(200, done)
75-
})
76-
77-
it('should return the same HTTP status code as the uri', function (done) {
78-
async.parallel([
79-
// 500
80-
function (next) {
81-
nock('https://amazingwebsite.tld').get('/404').reply(404)
82-
server.get('/proxy/?uri=https://amazingwebsite.tld/404')
83-
.expect(404, next)
84-
},
85-
function (next) {
86-
nock('https://amazingwebsite.tld').get('/401').reply(401)
87-
server.get('/proxy/?uri=https://amazingwebsite.tld/401')
88-
.expect(401, next)
89-
},
90-
function (next) {
91-
nock('https://amazingwebsite.tld').get('/500').reply(500)
92-
server.get('/proxy/?uri=https://amazingwebsite.tld/500')
93-
.expect(500, next)
94-
},
95-
function (next) {
96-
nock('https://amazingwebsite.tld').get('/').reply(200)
97-
server.get('/proxy/?uri=https://amazingwebsite.tld/')
98-
.expect(200, next)
99-
}
100-
], done)
101-
})
102-
103-
it('should work with cors', function (done) {
104-
nock('https://amazingwebsite.tld').get('/').reply(200)
105-
server.get('/proxy/?uri=https://amazingwebsite.tld/')
106-
.set('Origin', 'http://example.com')
107-
.expect('Access-Control-Allow-Origin', 'http://example.com')
108-
.expect(200, done)
109-
})
110-
})
111-
11213
describe('suffixMeta', function () {
11314
describe('not passed', function () {
11415
it('should fallback on .meta', function () {

test/proxy.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var assert = require('chai').assert
2+
var supertest = require('supertest')
3+
var path = require('path')
4+
var nock = require('nock')
5+
var async = require('async')
6+
7+
var ldnode = require('../index')
8+
9+
describe('proxy', () => {
10+
var ldp = ldnode({
11+
root: path.join(__dirname, '/resources'),
12+
proxy: '/proxy'
13+
})
14+
var server = supertest(ldp)
15+
16+
it('should return the website in /proxy?uri', (done) => {
17+
nock('https://amazingwebsite.tld').get('/').reply(200)
18+
server.get('/proxy?uri=https://amazingwebsite.tld/')
19+
.expect(200, done)
20+
})
21+
22+
it('should return error on local network requests', (done) => {
23+
nock('https://192.168.0.0').get('/').reply(200)
24+
server.get('/proxy?uri=https://192.168.0.0/')
25+
.expect(406, done)
26+
})
27+
28+
it('should return error on invalid uri', (done) => {
29+
server.get('/proxy?uri=HELLOWORLD')
30+
.expect(406, done)
31+
})
32+
33+
it('should return error on relative paths', (done) => {
34+
server.get('/proxy?uri=../')
35+
.expect(406, done)
36+
})
37+
38+
it('should return the same headers of proxied request', (done) => {
39+
nock('https://amazingwebsite.tld')
40+
.get('/')
41+
.reply(function (uri, req) {
42+
if (this.req.headers['accept'] !== 'text/turtle') {
43+
throw Error('Accept is received on the header')
44+
}
45+
if (this.req.headers['test'] && this.req.headers['test'] === 'test1') {
46+
return [200, 'YES']
47+
} else {
48+
return [500, 'empty']
49+
}
50+
})
51+
52+
server.get('/proxy?uri=https://amazingwebsite.tld/')
53+
.set('test', 'test1')
54+
.set('accept', 'text/turtle')
55+
.expect(200)
56+
.end((err, data) => {
57+
if (err) return done(err)
58+
done(err)
59+
})
60+
})
61+
62+
it('should also work on /proxy/ ?uri', (done) => {
63+
nock('https://amazingwebsite.tld').get('/').reply(200)
64+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
65+
.expect((a) => {
66+
assert.equal(a.header['link'], null)
67+
})
68+
.expect(200, done)
69+
})
70+
71+
it('should return the same HTTP status code as the uri', (done) => {
72+
async.parallel([
73+
// 500
74+
(next) => {
75+
nock('https://amazingwebsite.tld').get('/404').reply(404)
76+
server.get('/proxy/?uri=https://amazingwebsite.tld/404')
77+
.expect(404, next)
78+
},
79+
(next) => {
80+
nock('https://amazingwebsite.tld').get('/401').reply(401)
81+
server.get('/proxy/?uri=https://amazingwebsite.tld/401')
82+
.expect(401, next)
83+
},
84+
(next) => {
85+
nock('https://amazingwebsite.tld').get('/500').reply(500)
86+
server.get('/proxy/?uri=https://amazingwebsite.tld/500')
87+
.expect(500, next)
88+
},
89+
(next) => {
90+
nock('https://amazingwebsite.tld').get('/').reply(200)
91+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
92+
.expect(200, next)
93+
}
94+
], done)
95+
})
96+
97+
it('should work with cors', (done) => {
98+
nock('https://amazingwebsite.tld').get('/').reply(200)
99+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
100+
.set('Origin', 'http://example.com')
101+
.expect('Access-Control-Allow-Origin', 'http://example.com')
102+
.expect(200, done)
103+
})
104+
})

0 commit comments

Comments
 (0)