@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
120 lines (119 loc) • 4.7 kB
JavaScript
/*
* Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com>
* This file is part of Sync-in | The open source file sync and share solution
* See the LICENSE file for licensing details
*/ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _common = require("@nestjs/common");
const _testing = require("@nestjs/testing");
const _rxjs = require("rxjs");
const _stream = require("stream");
const _zlib = /*#__PURE__*/ _interop_require_default(require("zlib"));
const _syncdiffgzipbodyinterceptor = require("./sync-diff-gzip-body.interceptor");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
describe('SyncDiffGzipBodyInterceptor', ()=>{
let interceptor;
const createReadableFrom = (data)=>{
const stream = new _stream.Readable();
stream.push(data);
stream.push(null);
return stream;
};
const createExecutionContextWithRequest = (req)=>{
return {
switchToHttp: ()=>({
getRequest: ()=>req
})
};
};
const createCallHandler = (value = 'ok')=>{
return {
handle: jest.fn(()=>(0, _rxjs.of)(value))
};
};
beforeEach(async ()=>{
const module = await _testing.Test.createTestingModule({
providers: [
_syncdiffgzipbodyinterceptor.SyncDiffGzipBodyInterceptor
]
}).compile();
interceptor = module.get(_syncdiffgzipbodyinterceptor.SyncDiffGzipBodyInterceptor);
});
it('should gunzip and parse JSON body when Content-Encoding is gzip', async ()=>{
const originalBody = {
a: 1,
b: 'two'
};
const gzipped = _zlib.default.gzipSync(Buffer.from(JSON.stringify(originalBody)));
const req = {
headers: {
'content-encoding': 'gzip'
},
raw: createReadableFrom(gzipped),
body: undefined
};
const ctx = createExecutionContextWithRequest(req);
const next = createCallHandler('handled');
const result$ = await interceptor.intercept(ctx, next);
const result = await (0, _rxjs.lastValueFrom)(result$);
expect(result).toBe('handled');
expect(next.handle).toHaveBeenCalledTimes(1);
expect(req.body).toEqual(originalBody);
});
it('should pass through without modifying body when Content-Encoding is not gzip', async ()=>{
const req = {
headers: {},
raw: createReadableFrom(Buffer.from('no use in this case')),
body: 'initial'
};
const ctx = createExecutionContextWithRequest(req);
const next = createCallHandler('passthrough');
const result$ = await interceptor.intercept(ctx, next);
const result = await (0, _rxjs.lastValueFrom)(result$);
expect(result).toBe('passthrough');
expect(next.handle).toHaveBeenCalledTimes(1);
expect(req.body).toBe('initial');
});
it('should throw BadRequest when gzip body is invalid', async ()=>{
const invalidGzip = Buffer.from('this-is-not-gzip');
const req = {
headers: {
'content-encoding': 'gzip'
},
raw: createReadableFrom(invalidGzip)
};
const ctx = createExecutionContextWithRequest(req);
const next = createCallHandler();
await expect(interceptor.intercept(ctx, next)).rejects.toEqual(new _common.HttpException('Invalid gzip body', _common.HttpStatus.BAD_REQUEST));
expect(next.handle).not.toHaveBeenCalled();
});
it('should throw BadRequest when decoded JSON is invalid', async ()=>{
// gzip-compressed invalid JSON (plain text)
const gzippedInvalidJson = _zlib.default.gzipSync(Buffer.from('not-json'));
const req = {
headers: {
'content-encoding': 'gzip'
},
raw: createReadableFrom(gzippedInvalidJson)
};
const ctx = createExecutionContextWithRequest(req);
const next = createCallHandler();
try {
await interceptor.intercept(ctx, next);
fail('Expected interceptor to throw for invalid JSON');
} catch (e) {
expect(e).toBeInstanceOf(_common.HttpException);
const ex = e;
expect(ex.getStatus()).toBe(_common.HttpStatus.BAD_REQUEST);
expect(String(ex.getResponse())).toContain('Invalid JSON');
expect(next.handle).not.toHaveBeenCalled();
}
});
});
//# sourceMappingURL=sync-diff-gzip-body.interceptor.spec.js.map