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

View File

@@ -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;
};