No response compression when there is a referer (#47751)
* Hacking it in there, this is obviously not where this belongs * Moving implementation to a private method * Adding unit tests, I don't like the way I had to write these * Adding integration tests * Test not relying on implementation details... * No longer using .inject, thanks Mikhail!!! * Adding comment explaining the long body * Fixing nesting of describes for api integration tests
This commit is contained in:
parent
28c374ace5
commit
85e5885ccf
|
@ -577,6 +577,45 @@ test('exposes route details of incoming request to a route handler', async () =>
|
|||
});
|
||||
});
|
||||
|
||||
describe('conditional compression', () => {
|
||||
test('disables compression when there is a referer', async () => {
|
||||
const { registerRouter, server: innerServer } = await server.setup(config);
|
||||
|
||||
const router = new Router('', logger, enhanceWithContext);
|
||||
router.get({ path: '/', validate: false }, (context, req, res) =>
|
||||
// we need the large body here so that compression would normally be used
|
||||
res.ok({ body: 'hello'.repeat(500), headers: { 'Content-Type': 'text/html; charset=UTF-8' } })
|
||||
);
|
||||
registerRouter(router);
|
||||
|
||||
await server.start();
|
||||
const response = await supertest(innerServer.listener)
|
||||
.get('/')
|
||||
.set('accept-encoding', 'gzip')
|
||||
.set('referer', 'http://some-other-site/');
|
||||
|
||||
expect(response.header).not.toHaveProperty('content-encoding');
|
||||
});
|
||||
|
||||
test(`enables compression when there isn't a referer`, async () => {
|
||||
const { registerRouter, server: innerServer } = await server.setup(config);
|
||||
|
||||
const router = new Router('', logger, enhanceWithContext);
|
||||
router.get({ path: '/', validate: false }, (context, req, res) =>
|
||||
// we need the large body here so that compression will be used
|
||||
res.ok({ body: 'hello'.repeat(500), headers: { 'Content-Type': 'text/html; charset=UTF-8' } })
|
||||
);
|
||||
registerRouter(router);
|
||||
|
||||
await server.start();
|
||||
const response = await supertest(innerServer.listener)
|
||||
.get('/')
|
||||
.set('accept-encoding', 'gzip');
|
||||
|
||||
expect(response.header).toHaveProperty('content-encoding', 'gzip');
|
||||
});
|
||||
});
|
||||
|
||||
describe('setup contract', () => {
|
||||
describe('#createSessionStorage', () => {
|
||||
it('creates session storage factory', async () => {
|
||||
|
|
|
@ -96,6 +96,7 @@ export class HttpServer {
|
|||
|
||||
const basePathService = new BasePath(config.basePath);
|
||||
this.setupBasePathRewrite(config, basePathService);
|
||||
this.setupConditionalCompression();
|
||||
|
||||
return {
|
||||
registerRouter: this.registerRouter.bind(this),
|
||||
|
@ -175,6 +176,23 @@ export class HttpServer {
|
|||
});
|
||||
}
|
||||
|
||||
private setupConditionalCompression() {
|
||||
if (this.server === undefined) {
|
||||
throw new Error('Server is not created yet');
|
||||
}
|
||||
|
||||
this.server.ext('onRequest', (request, h) => {
|
||||
// whenever there is a referrer, don't use compression even if the client supports it
|
||||
if (request.info.referrer !== '') {
|
||||
this.log.debug(
|
||||
`Not using compression because there is a referer: ${request.info.referrer}`
|
||||
);
|
||||
request.info.acceptEncoding = '';
|
||||
}
|
||||
return h.continue;
|
||||
});
|
||||
}
|
||||
|
||||
private registerOnPostAuth(fn: OnPostAuthHandler) {
|
||||
if (this.server === undefined) {
|
||||
throw new Error('Server is not created yet');
|
||||
|
|
|
@ -16,21 +16,45 @@
|
|||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
import expect from '@kbn/expect';
|
||||
|
||||
export default function ({ getService }) {
|
||||
const supertest = getService('supertest');
|
||||
|
||||
describe('core request context', () => {
|
||||
it('provides access to elasticsearch', async () => (
|
||||
await supertest
|
||||
.get('/requestcontext/elasticsearch')
|
||||
.expect(200, 'Elasticsearch: true')
|
||||
));
|
||||
describe('core', () => {
|
||||
describe('request context', () => {
|
||||
it('provides access to elasticsearch', async () => (
|
||||
await supertest
|
||||
.get('/requestcontext/elasticsearch')
|
||||
.expect(200, 'Elasticsearch: true')
|
||||
));
|
||||
|
||||
it('provides access to SavedObjects client', async () => (
|
||||
await supertest
|
||||
.get('/requestcontext/savedobjectsclient')
|
||||
.expect(200, 'SavedObjects client: {"page":1,"per_page":20,"total":0,"saved_objects":[]}')
|
||||
));
|
||||
it('provides access to SavedObjects client', async () => (
|
||||
await supertest
|
||||
.get('/requestcontext/savedobjectsclient')
|
||||
.expect(200, 'SavedObjects client: {"page":1,"per_page":20,"total":0,"saved_objects":[]}')
|
||||
));
|
||||
});
|
||||
|
||||
describe('compression', () => {
|
||||
it(`uses compression when there isn't a referer`, async () => {
|
||||
await supertest
|
||||
.get('/app/kibana')
|
||||
.set('accept-encoding', 'gzip')
|
||||
.then(response => {
|
||||
expect(response.headers).to.have.property('content-encoding', 'gzip');
|
||||
});
|
||||
});
|
||||
|
||||
it(`doesn't use compression when there is a referer`, async () => {
|
||||
await supertest
|
||||
.get('/app/kibana')
|
||||
.set('accept-encoding', 'gzip')
|
||||
.set('referer', 'https://www.google.com')
|
||||
.then(response => {
|
||||
expect(response.headers).not.to.have.property('content-encoding');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -34,5 +34,6 @@ export default function ({ loadTestFile }) {
|
|||
loadTestFile(require.resolve('./status'));
|
||||
loadTestFile(require.resolve('./stats'));
|
||||
loadTestFile(require.resolve('./ui_metric'));
|
||||
loadTestFile(require.resolve('./core'));
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue