This repository was archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathserver_test.js
More file actions
164 lines (141 loc) · 4.4 KB
/
server_test.js
File metadata and controls
164 lines (141 loc) · 4.4 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*jshint node:true, strict:false */
/*global describe, it */
var supertest = require('supertest'),
http = require('http'),
express = require('express'),
expect = require('expect.js'),
app = require('../lib/app.js');
describe('app', function(){
it('should give a status of 502 for a non-existent page', function(done){
supertest(app)
.get('/')
.query({url: 'http://localhost:8001'})
.expect(502, done);
});
it('should pass the JSON and set the CORS headers', function(done){
var json = JSON.stringify({ message: 'test' });
var destApp = express();
destApp.get('/', function(req, res){
res.send(json);
});
var server = http.createServer(destApp);
server.listen(8001, function(){
supertest(app)
.get('/')
.query({url: 'http://localhost:8001'})
.expect('access-control-allow-origin', '*')
.expect(json, function(err){
server.on('close', function(){
done(err);
});
server.close();
});
});
});
it('should exclude particular headers from the server', function(done){
var json = JSON.stringify({ message: 'test' });
var destApp = express();
destApp.get('/', function(req, res){
res.set({
'Content-Length': 123,
'Connection': 'close',
'Server': 'CERN/3.0 libwww/2.17',
'X-Frame-Options': 'SAMEORIGIN',
// an arbitrary header, just to ensure they're getting passed
'X-Foo': 'bar'
});
res.send(json);
});
var server = http.createServer(destApp);
server.listen(8001, function(){
supertest(app)
.get('/')
.query({url: 'http://localhost:8001'})
.expect('access-control-allow-origin', '*')
.expect('content-length', '18')
.expect('connection', 'keep-alive')
.expect('x-foo', 'bar')
.expect(json, function(err, res){
if (!err){
expect(res.headers['x-foo']).to.be('bar'); // double-check
expect(res.headers.server).to.be(undefined);
expect(res.headers['x-frame-options']).to.be(undefined);
}
server.on('close', function(){
done(err);
});
server.close();
});
});
});
it('should wrap with callback name, if provided', function(done){
var json = JSON.stringify({ message: 'test' });
var destApp = express();
destApp.get('/', function(req, res){
res.send(json);
});
var server = http.createServer(destApp);
server.listen(8001, function(){
supertest(app)
.get('/')
.query({callback: 'foo', url: 'http://localhost:8001'})
.expect('foo(' + json + ');', function(err){
server.on('close', function(){
done(err);
});
server.close();
});
});
});
it('should escape "raw" requests for JSONP', function(done){
var destApp = express();
destApp.get('/', function(req, res){
res.send('test " \' " </script> escaping');
});
var server = http.createServer(destApp);
server.listen(8001, function(){
supertest(app)
.get('/')
.query({callback: 'foo', url: 'http://localhost:8001', raw: true})
.expect('foo({"data":"test \\" \' \\" <\\/script> escaping"});', function(err){
server.on('close', function(){
done(err);
});
server.close();
});
});
});
it('should pass the unescaped body for "raw" CORS requests', function(done){
var body = 'test " \' " escaping';
var destApp = express();
destApp.get('/', function(req, res){
res.send(body);
});
var server = http.createServer(destApp);
server.listen(8001, function(){
supertest(app)
.get('/')
.query({url: 'http://localhost:8001', raw: true})
.expect(body, function(err){
server.on('close', function(){
done(err);
});
server.close();
});
});
});
it('should set the Accept header to "*/*" for "raw" requests', function(done){
var destApp = express();
destApp.get('/', function(req, res){
expect(req.headers.accept).to.eql('*/*');
res.send('');
});
var server = http.createServer(destApp);
server.listen(8001, function(){
supertest(app)
.get('/')
.query({url: 'http://localhost:8001', raw: true})
.expect(200, done);
});
});
});