mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-05-23 07:58:27 +00:00
add test cases for compressed response
This commit is contained in:
parent
a8c9f590fc
commit
c040ae4578
@ -242,18 +242,16 @@ class Recorder extends events.EventEmitter {
|
|||||||
bodyContent = iconv.decode(bodyContent, currentCharset);
|
bodyContent = iconv.decode(bodyContent, currentCharset);
|
||||||
}
|
}
|
||||||
|
|
||||||
result.mime = contentType;
|
|
||||||
result.content = bodyContent.toString();
|
result.content = bodyContent.toString();
|
||||||
result.type = contentType && /application\/json/i.test(contentType) ? 'json' : 'text';
|
result.type = contentType && /application\/json/i.test(contentType) ? 'json' : 'text';
|
||||||
} else if (contentType && /image/i.test(contentType)) {
|
} else if (contentType && /image/i.test(contentType)) {
|
||||||
result.type = 'image';
|
result.type = 'image';
|
||||||
result.mime = contentType;
|
|
||||||
result.content = bodyContent;
|
result.content = bodyContent;
|
||||||
} else {
|
} else {
|
||||||
result.type = contentType;
|
result.type = contentType;
|
||||||
result.mime = contentType;
|
|
||||||
result.content = bodyContent.toString();
|
result.content = bodyContent.toString();
|
||||||
}
|
}
|
||||||
|
result.mime = contentType;
|
||||||
result.fileName = path.basename(record.path);
|
result.fileName = path.basename(record.path);
|
||||||
result.statusCode = record.statusCode;
|
result.statusCode = record.statusCode;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -146,10 +146,8 @@ class webInterface extends events.EventEmitter {
|
|||||||
if (err || !result) {
|
if (err || !result) {
|
||||||
res.json({});
|
res.json({});
|
||||||
} else if (result.statusCode === 200 && result.mime) {
|
} else if (result.statusCode === 200 && result.mime) {
|
||||||
if (result.type === 'json' ||
|
|
||||||
result.mime.indexOf('text') === 0 ||
|
|
||||||
// deal with 'application/x-javascript' and 'application/javascript'
|
// deal with 'application/x-javascript' and 'application/javascript'
|
||||||
result.mime.indexOf('javascript') > -1) {
|
if (/json|text|javascript/.test(result.mime)) {
|
||||||
_resContent();
|
_resContent();
|
||||||
} else if (result.type === 'image') {
|
} else if (result.type === 'image') {
|
||||||
_resDownload(false);
|
_resDownload(false);
|
||||||
|
@ -11,6 +11,9 @@ const color = require('colorful');
|
|||||||
const WebSocketServer = require('ws').Server;
|
const WebSocketServer = require('ws').Server;
|
||||||
const tls = require('tls');
|
const tls = require('tls');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
const stream = require('stream');
|
||||||
|
const brotli = require('brotli');
|
||||||
|
const zlib = require('zlib');
|
||||||
|
|
||||||
const createSecureContext = tls.createSecureContext || crypto.createSecureContext;
|
const createSecureContext = tls.createSecureContext || crypto.createSecureContext;
|
||||||
|
|
||||||
@ -209,11 +212,6 @@ KoaServer.prototype.constructRouter = function () {
|
|||||||
this.response.set('Allow', 'GET, HEAD, POST, OPTIONS');
|
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) {
|
router.get('/test/should_not_replace_option', this.logRequest, function *(next) {
|
||||||
this.response.body = 'the_option_that_not_be_replaced';
|
this.response.body = 'the_option_that_not_be_replaced';
|
||||||
});
|
});
|
||||||
@ -249,6 +247,23 @@ KoaServer.prototype.constructRouter = function () {
|
|||||||
this.response.body = buf;
|
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;
|
return router;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,13 +2,14 @@
|
|||||||
* test for rule replaceOption rule
|
* test for rule replaceOption rule
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
const ip = require('ip');
|
|
||||||
const AnyProxy = require('../../proxy');
|
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 Server = require('../server/server.js');
|
||||||
|
|
||||||
const OUT_BOUND_IP = ip.address();
|
|
||||||
|
|
||||||
describe('AnyProxy.proxyServer basic test', () => {
|
describe('AnyProxy.proxyServer basic test', () => {
|
||||||
it('should successfully start a proxy server', done => {
|
it('should successfully start a proxy server', done => {
|
||||||
const options = {
|
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);
|
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(done)
|
.catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work as expected for ip host', done => {
|
it('should work as expected for ip host', done => {
|
||||||
// test if proxy server works
|
// test if proxy server works
|
||||||
proxyGet(`https://${OUT_BOUND_IP}:3001/test`, {}, {})
|
proxyGet(generateUrl('https', '/test'), {}, {})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
expect(res && res.statusCode && res.statusCode === 200).toBe(true);
|
expect(res && res.statusCode && res.statusCode === 200).toBe(true);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(done)
|
.catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should start webinterface correctly', 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);
|
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
|
||||||
done();
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user