-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-examples.js
133 lines (103 loc) · 4.27 KB
/
test-examples.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
/**
* CORS Anywhere is designed for use as a standalone server. Sometimes you want
* to have extra functionality on top of the default CORS server. If it may be
* useful to others, please open a feature request on the issue tracker at
* https://github.com/Rob--W/cors-anywhere/issues.
*
* If it is only useful to your application, look below for some examples.
* These examples are provided as-is without guarantees. Use at your own risk.
*/
/* eslint-env mocha */
require('./setup');
var createServer = require('../').createServer;
var assert = require('assert');
var request = require('supertest');
var http = require('http');
describe('Examples', function() {
// Note: In the examples below we don't listen on any port after calling
// createServer() because it is not needed to start listening on a port if the
// CORS Anywhere is only used internally.
// And normally you have to listen on some port, like this:
//
// http_server.listen(port_number);
//
// But in these test, the call to request() automatically handles that part so
// the examples don't have an explicit .listen() call.
it('Rewrite proxy URL', function(done) {
var cors_anywhere = createServer();
var http_server = http.createServer(function(req, res) {
// For testing, check whether req.url is the same as what we input below.
assert.strictEqual(req.url, '/dummy-for-testing');
// Basic example: Always proxy example.com.
req.url = '/http://example.com';
cors_anywhere.emit('request', req, res);
});
request(http_server)
.get('/dummy-for-testing')
.expect('Access-Control-Allow-Origin', '*')
.expect('x-request-url', 'http://example.com/')
.expect(200, 'Response from example.com', done);
});
it('Transform response to uppercase (streaming)', function(done) {
var cors_anywhere = createServer();
var http_server = http.createServer(function(req, res) {
var originalWrite = res.write;
res.write = function(data, encoding, callback) {
if (Buffer.isBuffer(data)) {
data = data.toString();
}
assert.strictEqual(typeof data, 'string');
// This example shows how to transform the response to upper case.
data = data.toUpperCase();
originalWrite.call(this, data, encoding, callback);
};
cors_anywhere.emit('request', req, res);
});
request(http_server)
.get('/example.com')
.expect('Access-Control-Allow-Origin', '*')
.expect('x-request-url', 'http://example.com/')
.expect(200, 'RESPONSE FROM EXAMPLE.COM', done);
});
it('Transform response to uppercase (buffered)', function(done) {
var cors_anywhere = createServer();
var http_server = http.createServer(function(req, res) {
var originalWrite = res.write;
var originalEnd = res.end;
var buffers = [];
res.write = function(data, encoding, callback) {
assert.ok(Buffer.isBuffer(data) || typeof data === 'string');
buffers.push(data);
if (callback) {
process.nextTick(callback, null);
}
};
res.end = function(data, encoding, callback) {
if (data) {
this.write(data, encoding);
}
// After calling .end(), .write shouldn't be called any more. So let's
// restore it so that the default error handling for writing to closed
// streams would occur.
this.write = originalWrite;
// Combine all chunks. Note that we're assuming that all chunks are
// utf8 strings or buffers whose content is utf8-encoded. If this
// assumption is not true, then you have to update the .write method
// above.
data = buffers.join('');
// This example shows how to transform the response to upper case.
data = data.toUpperCase();
// .end should be called once, so let's restore it so that any default
// error handling occurs if it occurs again.
this.end = originalEnd;
this.end(data, 'utf8', callback);
};
cors_anywhere.emit('request', req, res);
});
request(http_server)
.get('/example.com')
.expect('Access-Control-Allow-Origin', '*')
.expect('x-request-url', 'http://example.com/')
.expect(200, 'RESPONSE FROM EXAMPLE.COM', done);
});
});