1
0
mirror of https://github.com/alibaba/anyproxy.git synced 2025-05-22 23:48:26 +00:00

add test cases for compressed response

This commit is contained in:
guox191 2019-03-21 18:18:20 +08:00
parent a8c9f590fc
commit c040ae4578
4 changed files with 52 additions and 20 deletions

@ -242,18 +242,16 @@ class Recorder extends events.EventEmitter {
bodyContent = iconv.decode(bodyContent, currentCharset);
}
result.mime = contentType;
result.content = bodyContent.toString();
result.type = contentType && /application\/json/i.test(contentType) ? 'json' : 'text';
} else if (contentType && /image/i.test(contentType)) {
result.type = 'image';
result.mime = contentType;
result.content = bodyContent;
} else {
result.type = contentType;
result.mime = contentType;
result.content = bodyContent.toString();
}
result.mime = contentType;
result.fileName = path.basename(record.path);
result.statusCode = record.statusCode;
} catch (e) {

@ -146,10 +146,8 @@ class webInterface extends events.EventEmitter {
if (err || !result) {
res.json({});
} else if (result.statusCode === 200 && result.mime) {
if (result.type === 'json' ||
result.mime.indexOf('text') === 0 ||
// deal with 'application/x-javascript' and 'application/javascript'
result.mime.indexOf('javascript') > -1) {
// deal with 'application/x-javascript' and 'application/javascript'
if (/json|text|javascript/.test(result.mime)) {
_resContent();
} else if (result.type === 'image') {
_resDownload(false);

@ -11,6 +11,9 @@ const color = require('colorful');
const WebSocketServer = require('ws').Server;
const tls = require('tls');
const crypto = require('crypto');
const stream = require('stream');
const brotli = require('brotli');
const zlib = require('zlib');
const createSecureContext = tls.createSecureContext || crypto.createSecureContext;
@ -209,11 +212,6 @@ KoaServer.prototype.constructRouter = function () {
this.response.set('Allow', 'GET, HEAD, POST, OPTIONS');
});
// router.connect('/test/connect', function *(next) {
// printLog('requesting connect /test/connect');
// this.response.body = 'connect_established_body';
// });
router.get('/test/should_not_replace_option', this.logRequest, function *(next) {
this.response.body = 'the_option_that_not_be_replaced';
});
@ -249,6 +247,23 @@ KoaServer.prototype.constructRouter = function () {
this.response.body = buf;
});
router.get('/test/brotli', this.logRequest, function *(next) {
this.status = 200;
this.response.set('Content-Encoding', 'br');
this.response.set('Content-Type', 'application/json');
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"}');
this.response.body = Buffer.from(brotli.compress(buf));
});
router.get('/test/gzip', this.logRequest, function *(next) {
this.status = 200;
this.response.set('Content-Encoding', 'gzip');
this.response.set('Content-Type', 'application/json');
const bufStream = new stream.PassThrough();
bufStream.end(new Buffer('{"type":"gzip","message":"This is a gzip encoding response"}'));
this.response.body = bufStream.pipe(zlib.createGzip());
});
return router;
};

@ -2,13 +2,14 @@
* test for rule replaceOption rule
*
*/
const ip = require('ip');
const AnyProxy = require('../../proxy');
const { proxyGet, directGet } = require('../util/HttpUtil.js');
const {
proxyGet,
directGet,
generateUrl,
} = require('../util/HttpUtil.js');
const Server = require('../server/server.js');
const OUT_BOUND_IP = ip.address();
describe('AnyProxy.proxyServer basic test', () => {
it('should successfully start a proxy server', done => {
const options = {
@ -67,17 +68,17 @@ describe('AnyProxy.proxyServer high order test', () => {
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
done();
})
.catch(done)
.catch(done);
});
it('should work as expected for ip host', done => {
// test if proxy server works
proxyGet(`https://${OUT_BOUND_IP}:3001/test`, {}, {})
proxyGet(generateUrl('https', '/test'), {}, {})
.then(res => {
expect(res && res.statusCode && res.statusCode === 200).toBe(true);
done();
})
.catch(done)
.catch(done);
});
it('should start webinterface correctly', done => {
@ -87,6 +88,26 @@ describe('AnyProxy.proxyServer high order test', () => {
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
done();
})
.catch(done)
.catch(done);
});
it('should deal well with the gzip encoding compressed response', done => {
proxyGet(generateUrl('https', '/test/gzip'), {}, {})
.then(res => {
expect(res && res.statusCode === 200).toBe(true);
expect(JSON.parse(res.body).type).toBe('gzip');
done();
})
.catch(done);
});
it('should deal well with the brotli encoding compressed response', done => {
proxyGet(generateUrl('https', '/test/brotli'), {}, {})
.then(res => {
expect(res && res.statusCode === 200).toBe(true);
expect(JSON.parse(res.body).type).toBe('brotli');
done();
})
.catch(done);
});
});