@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
77 lines (76 loc) • 3.2 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 _shared = require("../../../common/shared");
const _webdav = require("../constants/webdav");
const _webdavfilter = require("./webdav.filter");
describe('WebDAVExceptionsFilter', ()=>{
let filter;
const createMockResponse = ()=>{
const res = {};
res.header = jest.fn().mockReturnValue(res);
res.type = jest.fn().mockReturnValue(res);
res.status = jest.fn().mockReturnValue(res);
res.send = jest.fn().mockReturnValue(res);
return res;
};
const createMockHost = (res)=>({
switchToHttp: ()=>({
getResponse: ()=>res
})
});
beforeEach(async ()=>{
const moduleRef = await _testing.Test.createTestingModule({
providers: [
_webdavfilter.WebDAVExceptionsFilter
]
}).compile();
filter = moduleRef.get(_webdavfilter.WebDAVExceptionsFilter);
});
afterEach(()=>{
jest.clearAllMocks();
});
it('should set WWW-Authenticate header and send empty body for 401', ()=>{
const res = createMockResponse();
const host = createMockHost(res);
const exception = new _common.HttpException('Unauthorized', 401);
filter.catch(exception, host);
expect(res.header).toHaveBeenCalledWith('WWW-Authenticate', `Basic realm="${_shared.SERVER_NAME}"`);
expect(res.status).toHaveBeenCalledWith(401);
expect(res.type).not.toHaveBeenCalled();
expect(res.send).toHaveBeenCalled();
// Ensure no body is sent
expect(res.send.mock.calls[0][0]).toBeUndefined();
});
it('should set XML content type and forward string response for non-401', ()=>{
const res = createMockResponse();
const host = createMockHost(res);
const exception = new _common.HttpException('Internal error', 500);
filter.catch(exception, host);
expect(res.type).toHaveBeenCalledWith(_webdav.XML_CONTENT_TYPE);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.send).toHaveBeenCalledWith('Internal error');
expect(res.header).not.toHaveBeenCalled();
});
it('should extract "message" from object response and send it as XML for non-401', ()=>{
const res = createMockResponse();
const host = createMockHost(res);
const payload = {
message: '<error>Bad Request</error>'
};
const exception = new _common.HttpException(payload, 400);
filter.catch(exception, host);
expect(res.type).toHaveBeenCalledWith(_webdav.XML_CONTENT_TYPE);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.send).toHaveBeenCalledWith(payload.message);
expect(res.header).not.toHaveBeenCalled();
});
});
//# sourceMappingURL=webdav.filter.spec.js.map