forked from MrSwitch/proxy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
168 lines (131 loc) · 3.24 KB
/
tests.js
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
165
166
167
168
//
// OAuth Shim Tests
// Run from root with using command 'npm test'
//
// @author Andrew Dodson
// @since July 2013
//
//
////////////////////////////////
// Dependencies
////////////////////////////////
var proxy = require('../index'),
querystring = require('querystring'),
fs = require('fs'),
connect = require("connect"),
http = require("http"),
path = require('path');
// Setup a test server
var request = require('supertest');
var app = http.createServer( proxy ).listen( 3000 );
////////////////////////////////
// Remote Server
////////////////////////////////
var connect = require('connect');
var remoteServer = connect(), srv,
remote_url = "http://localhost:3001/path";
beforeEach(function(){
srv = remoteServer.listen(3001);
});
// tests here
afterEach(function(){
srv.close();
});
remoteServer.use('/', function(req,res){
var headers = req.headers;
headers.method = req.method;
headers.url = req.url;
res.writeHead(200, headers);
var buf='';
req.on('data', function(data){
buf+=data;
});
req.on('end', function(){
////////////////////
// TAILOR THE RESPONSE TO MATCH THE REQUEST
////////////////////
res.write(buf);
res.end();
});
});
// include this adds 'should' to all javascript objects...
// Indeed i too thought extending native objects was bad
// ... where there's a way there's a will!
require('should');
describe('Should handle GET requests with', function(){
it("200", function(done){
request(app)
.get('/'+remote_url)
.expect(200)
.expect("url", "/path")
.expect("method", "GET")
.expect("Access-Control-Allow-Origin", "*")
.expect("")
.end(function(err, res){
if (err) throw err;
done();
});
});
it("should use the address from a querystring e.g. /?url=address", function(done){
request(app)
.get('/url='+encodeURIComponent(remote_url))
.expect(200)
.expect("url", "/path")
.expect("method", "GET")
.expect("Access-Control-Allow-Origin", "*")
.expect("")
.end(function(err, res){
if (err) throw err;
done();
});
});
it("should override the header from a querystring e.g. /?headers=key%3Dvalue%26key2%3Dvalue2", function(done){
var path = '/?' + querystring.stringify({
url : remote_url,
headers : "key=value&key2=value2"
});
request(app)
.get( path )
.expect(200)
.expect("url", "/path")
.expect("method", "GET")
.expect("key", "value")
.expect("key2", "value2")
.expect("Access-Control-Allow-Origin", "*")
.expect("")
.end(function(err, res){
if (err) throw err;
done();
});
});
});
describe('Should handle POST requests', function(){
it("with regular post body", function(done){
var body = "POST_DATA";
request(app)
.post('/'+remote_url)
.send( body )
.expect(200)
.expect("url", "/path")
.expect("method", "POST")
.expect("Access-Control-Allow-Origin", "*")
.expect( body )
.end(function(err, res){
if (err) throw err;
done();
});
});
xit("with a multipart POST body", function(done){
request(app)
.post('/'+remote_url)
.attach("file", './test/tests.js')
.expect(200)
.expect("method", "POST")
.expect("Access-Control-Allow-Origin", "*")
.expect(/^\-\-.*?/)
.end(function(err, res){
if (err) throw err;
done();
});
});
});