forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
173 lines (142 loc) · 3.97 KB
/
Copy pathproxy.js
File metadata and controls
173 lines (142 loc) · 3.97 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
165
166
167
168
169
170
171
172
173
// @flow strict-local
import assert from 'assert';
import path from 'path';
import {bundler, getNextBuild, inputFS} from '@parcel/test-utils';
import http from 'http';
import getPort from 'get-port';
const config = path.join(
__dirname,
'./integration/custom-configs/.parcelrc-dev-server',
);
function apiServer() {
const server = http
.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Request URL: ' + req.url);
res.end();
})
.listen(9753);
return server;
}
function get(file, port, client = http) {
return new Promise((resolve, reject) => {
client.get(
{
hostname: 'localhost',
port: port,
path: file,
rejectUnauthorized: false,
},
res => {
res.setEncoding('utf8');
let data = '';
res.on('data', c => (data += c));
res.on('end', () => {
if (res.statusCode !== 200) {
return reject({statusCode: res.statusCode, data});
}
resolve(data);
});
},
);
});
}
describe('proxy', function () {
let subscription;
let cwd;
let server;
beforeEach(function () {
cwd = inputFS.cwd();
});
afterEach(async () => {
inputFS.chdir(cwd);
if (subscription) {
await subscription.unsubscribe();
}
subscription = null;
if (server) {
await server.close();
}
server = null;
});
it('should handle proxy table written in .proxyrc', async function () {
let dir = path.join(__dirname, 'integration/proxyrc');
inputFS.chdir(dir);
let port = await getPort();
let b = bundler(path.join(dir, 'index.js'), {
config,
serveOptions: {
https: false,
port: port,
host: 'localhost',
},
});
subscription = await b.watch();
await getNextBuild(b);
server = apiServer();
let data = await get('/index.js', port);
assert.notEqual(data, 'Request URL: /index.js');
data = await get('/api/get', port);
assert.equal(data, 'Request URL: /api/get');
});
it('should handle proxy table written in .proxyrc.json', async function () {
let dir = path.join(__dirname, 'integration/proxyrc-json');
inputFS.chdir(dir);
let port = await getPort();
let b = bundler(path.join(dir, 'index.js'), {
config,
serveOptions: {
https: false,
port: port,
host: 'localhost',
},
});
subscription = await b.watch();
await getNextBuild(b);
server = apiServer();
let data = await get('/index.js', port);
assert.notEqual(data, 'Request URL: /index.js');
data = await get('/api/get', port);
assert.equal(data, 'Request URL: /api/get');
});
it('should handle proxy table written in .proxyrc.js', async function () {
let dir = path.join(__dirname, 'integration/proxyrc-js');
inputFS.chdir(dir);
let port = await getPort();
let b = bundler(path.join(dir, 'index.js'), {
config,
serveOptions: {
https: false,
port: port,
host: 'localhost',
},
});
subscription = await b.watch();
await getNextBuild(b);
server = apiServer();
let data = await get('/index.js', port);
assert.notEqual(data, 'Request URL: /index.js');
data = await get('/api/get', port);
assert.equal(data, 'Request URL: /get');
});
it('should handle proxy table written in .proxyrc.cjs', async function () {
let dir = path.join(__dirname, 'integration/proxyrc-cjs');
inputFS.chdir(dir);
let port = await getPort();
let b = bundler(path.join(dir, 'index.js'), {
config,
serveOptions: {
https: false,
port: port,
host: 'localhost',
},
});
subscription = await b.watch();
await getNextBuild(b);
server = apiServer();
let data = await get('/index.js', port);
assert.notEqual(data, 'Request URL: /index.js');
data = await get('/api/get', port);
assert.equal(data, 'Request URL: /get');
});
});