forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors-proxy-test.js
More file actions
135 lines (119 loc) · 3.9 KB
/
cors-proxy-test.js
File metadata and controls
135 lines (119 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var assert = require('chai').assert
var supertest = require('supertest')
var path = require('path')
var nock = require('nock')
var { checkDnsSettings } = require('../utils')
var ldnode = require('../../index')
describe('CORS Proxy', () => {
var ldp = ldnode({
root: path.join(__dirname, '../resources'),
corsProxy: '/proxy',
webid: false
})
var server = supertest(ldp)
before(checkDnsSettings)
it('should return the website in /proxy?uri', (done) => {
nock('https://example.org').get('/').reply(200)
server.get('/proxy?uri=https://example.org/')
.expect(200, done)
})
it('should pass the Host header to the proxied server', (done) => {
let headers
nock('https://example.org').get('/').reply(function (uri, body) {
headers = this.req.headers
return 200
})
server.get('/proxy?uri=https://example.org/')
.expect(200)
.end(error => {
assert.propertyVal(headers, 'host', 'example.org')
done(error)
})
})
it('should return 400 when the uri parameter is missing', (done) => {
nock('https://192.168.0.0').get('/').reply(200)
server.get('/proxy')
.expect('Invalid URL passed: (none)')
.expect(400)
.end(done)
})
const LOCAL_IPS = ['127.0.0.0', '10.0.0.0', '172.16.0.0', '192.168.0.0']
LOCAL_IPS.forEach(ip => {
it(`should return 400 for a ${ip} address`, (done) => {
nock(`https://${ip}`).get('/').reply(200)
server.get(`/proxy?uri=https://${ip}/`)
.expect(`Cannot proxy https://${ip}/`)
.expect(400)
.end(done)
})
})
it('should return 400 with a local hostname', (done) => {
nock('https://nic.localhost').get('/').reply(200)
server.get('/proxy?uri=https://nic.localhost/')
.expect('Cannot proxy https://nic.localhost/')
.expect(400)
.end(done)
})
it('should return 400 on invalid uri', (done) => {
server.get('/proxy?uri=HELLOWORLD')
.expect('Invalid URL passed: HELLOWORLD')
.expect(400)
.end(done)
})
it('should return 400 on relative paths', (done) => {
server.get('/proxy?uri=../')
.expect('Invalid URL passed: ../')
.expect(400)
.end(done)
})
it('should return the same headers of proxied request', (done) => {
nock('https://example.org')
.get('/')
.reply(function (uri, req) {
if (this.req.headers['accept'] !== 'text/turtle') {
throw Error('Accept is received on the header')
}
if (this.req.headers['test'] && this.req.headers['test'] === 'test1') {
return [200, 'YES']
} else {
return [500, 'empty']
}
})
server.get('/proxy?uri=https://example.org/')
.set('test', 'test1')
.set('accept', 'text/turtle')
.expect(200)
.end((err, data) => {
if (err) return done(err)
done(err)
})
})
it('should also work on /proxy/ ?uri', (done) => {
nock('https://example.org').get('/').reply(200)
server.get('/proxy/?uri=https://example.org/')
.expect((a) => {
assert.equal(a.header['link'], null)
})
.expect(200, done)
})
it('should return the same HTTP status code as the uri', () => {
nock('https://example.org')
.get('/404').reply(404)
.get('/401').reply(401)
.get('/500').reply(500)
.get('/200').reply(200)
return Promise.all([
server.get('/proxy/?uri=https://example.org/404').expect(404),
server.get('/proxy/?uri=https://example.org/401').expect(401),
server.get('/proxy/?uri=https://example.org/500').expect(500),
server.get('/proxy/?uri=https://example.org/200').expect(200)
])
})
it('should work with cors', (done) => {
nock('https://example.org').get('/').reply(200)
server.get('/proxy/?uri=https://example.org/')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done)
})
})