diff --git a/lib/recorder.js b/lib/recorder.js index 1a37739..e85052d 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -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) { diff --git a/lib/webInterface.js b/lib/webInterface.js index f450221..1939e3e 100644 --- a/lib/webInterface.js +++ b/lib/webInterface.js @@ -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); diff --git a/test/server/server.js b/test/server/server.js index aae74b9..f122b9e 100644 --- a/test/server/server.js +++ b/test/server/server.js @@ -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; }; diff --git a/test/spec_lib/proxyServerModule.js b/test/spec_lib/proxyServerModule.js index 0b87a2c..77e6fc5 100644 --- a/test/spec_lib/proxyServerModule.js +++ b/test/spec_lib/proxyServerModule.js @@ -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); }); });