Sitelet https://github.com/alibaba/anyproxy/commit/c040ae4578cb54d48752285190e5992d6f22c27e
Skip to content

Commit c040ae4

Browse files
committed
add test cases for compressed response
1 parent a8c9f59 commit c040ae4

4 files changed

Lines changed: 52 additions & 20 deletions

File tree

‎lib/recorder.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,16 @@ class Recorder extends events.EventEmitter {
242242
bodyContent = iconv.decode(bodyContent, currentCharset);
243243
}
244244

245-
result.mime = contentType;
246245
result.content = bodyContent.toString();
247246
result.type = contentType && /application\/json/i.test(contentType) ? 'json' : 'text';
248247
} else if (contentType && /image/i.test(contentType)) {
249248
result.type = 'image';
250-
result.mime = contentType;
251249
result.content = bodyContent;
252250
} else {
253251
result.type = contentType;
254-
result.mime = contentType;
255252
result.content = bodyContent.toString();
256253
}
254+
result.mime = contentType;
257255
result.fileName = path.basename(record.path);
258256
result.statusCode = record.statusCode;
259257
} catch (e) {

‎lib/webInterface.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,8 @@ class webInterface extends events.EventEmitter {
146146
if (err || !result) {
147147
res.json({});
148148
} else if (result.statusCode === 200 && result.mime) {
149-
if (result.type === 'json' ||
150-
result.mime.indexOf('text') === 0 ||
151-
// deal with 'application/x-javascript' and 'application/javascript'
152-
result.mime.indexOf('javascript') > -1) {
149+
// deal with 'application/x-javascript' and 'application/javascript'
150+
if (/json|text|javascript/.test(result.mime)) {
153151
_resContent();
154152
} else if (result.type === 'image') {
155153
_resDownload(false);

‎test/server/server.js‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const color = require('colorful');
1111
const WebSocketServer = require('ws').Server;
1212
const tls = require('tls');
1313
const crypto = require('crypto');
14+
const stream = require('stream');
15+
const brotli = require('brotli');
16+
const zlib = require('zlib');
1417

1518
const createSecureContext = tls.createSecureContext || crypto.createSecureContext;
1619

@@ -209,11 +212,6 @@ KoaServer.prototype.constructRouter = function () {
209212
this.response.set('Allow', 'GET, HEAD, POST, OPTIONS');
210213
});
211214

212-
// router.connect('/test/connect', function *(next) {
213-
// printLog('requesting connect /test/connect');
214-
// this.response.body = 'connect_established_body';
215-
// });
216-
217215
router.get('/test/should_not_replace_option', this.logRequest, function *(next) {
218216
this.response.body = 'the_option_that_not_be_replaced';
219217
});
@@ -249,6 +247,23 @@ KoaServer.prototype.constructRouter = function () {
249247
this.response.body = buf;
250248
});
251249

250+
router.get('/test/brotli', this.logRequest, function *(next) {
251+
this.status = 200;
252+
this.response.set('Content-Encoding', 'br');
253+
this.response.set('Content-Type', 'application/json');
254+
const buf = new Buffer('{"type":"brotli","message":"This is a brotli encoding response, but it need to be a long string or the brotli module\'s compress result will be null"}');
255+
this.response.body = Buffer.from(brotli.compress(buf));
256+
});
257+
258+
router.get('/test/gzip', this.logRequest, function *(next) {
259+
this.status = 200;
260+
this.response.set('Content-Encoding', 'gzip');
261+
this.response.set('Content-Type', 'application/json');
262+
const bufStream = new stream.PassThrough();
263+
bufStream.end(new Buffer('{"type":"gzip","message":"This is a gzip encoding response"}'));
264+
this.response.body = bufStream.pipe(zlib.createGzip());
265+
});
266+
252267
return router;
253268
};
254269

‎test/spec_lib/proxyServerModule.js‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
* test for rule replaceOption rule
33
*
44
*/
5-
const ip = require('ip');
65
const AnyProxy = require('../../proxy');
7-
const { proxyGet, directGet } = require('../util/HttpUtil.js');
6+
const {
7+
proxyGet,
8+
directGet,
9+
generateUrl,
10+
} = require('../util/HttpUtil.js');
811
const Server = require('../server/server.js');
912

10-
const OUT_BOUND_IP = ip.address();
11-
1213
describe('AnyProxy.proxyServer basic test', () => {
1314
it('should successfully start a proxy server', done => {
1415
const options = {
@@ -67,17 +68,17 @@ describe('AnyProxy.proxyServer high order test', () => {
6768
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
6869
done();
6970
})
70-
.catch(done)
71+
.catch(done);
7172
});
7273

7374
it('should work as expected for ip host', done => {
7475
// test if proxy server works
75-
proxyGet(`https://${OUT_BOUND_IP}:3001/test`, {}, {})
76+
proxyGet(generateUrl('https', '/test'), {}, {})
7677
.then(res => {
7778
expect(res && res.statusCode && res.statusCode === 200).toBe(true);
7879
done();
7980
})
80-
.catch(done)
81+
.catch(done);
8182
});
8283

8384
it('should start webinterface correctly', done => {
@@ -87,6 +88,26 @@ describe('AnyProxy.proxyServer high order test', () => {
8788
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
8889
done();
8990
})
90-
.catch(done)
91+
.catch(done);
92+
});
93+
94+
it('should deal well with the gzip encoding compressed response', done => {
95+
proxyGet(generateUrl('https', '/test/gzip'), {}, {})
96+
.then(res => {
97+
expect(res && res.statusCode === 200).toBe(true);
98+
expect(JSON.parse(res.body).type).toBe('gzip');
99+
done();
100+
})
101+
.catch(done);
102+
});
103+
104+
it('should deal well with the brotli encoding compressed response', done => {
105+
proxyGet(generateUrl('https', '/test/brotli'), {}, {})
106+
.then(res => {
107+
expect(res && res.statusCode === 200).toBe(true);
108+
expect(JSON.parse(res.body).type).toBe('brotli');
109+
done();
110+
})
111+
.catch(done);
91112
});
92113
});

0 commit comments

Comments
 (0)