Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('triggers connection error', done => {
// given
const error = new WebSocketConnectionError('Unauthorized', HttpStatus.UNAUTHORIZED);
const connection$: WsConnectionEffect = req$ => req$.pipe(mergeMapTo(throwError(error)));
const webSocketServer = webSocketListener({ connection$ });
const targetClient1 = testBed.getClient(0);
const targetClient2 = testBed.getClient(1);
const server = testBed.getServer();
const context = createContext();
// when
webSocketServer({ server }).run(context);
// then
merge(
fromEvent(targetClient1, 'unexpected-response'),
fromEvent(targetClient2, 'unexpected-response'),
)
.pipe(take(2), toArray())
.subscribe(
(data) => {
expect(data[0][1].statusCode).toEqual(error.status);
expect(data[1][1].statusCode).toEqual(error.status);
expect(data[0][1].statusMessage).toEqual(error.message);
expect(data[1][1].statusMessage).toEqual(error.message);
describe('JWT middleware', () => {
let utilModule;
let factoryModule;
const context = createContext();
const effectMeta = createEffectMetadata({ ask: lookup(context) });
beforeEach(() => {
jest.unmock('../jwt.util.ts');
jest.unmock('../jwt.factory.ts');
utilModule = require('../jwt.util.ts');
factoryModule = require('../jwt.factory.ts');
});
afterEach(() => {
jest.clearAllMocks();
});
test('authorize$ authorizes incoming request and saves JWT payload to "req.user"', done => {
// given
const mockedSecret = 'test_secret';
describe('versionEffect$', () => {
const app = httpListener.run(createContext());
test('GET api/v1 responds with 200', async () =>
request(app)
.get('/api/v1')
.expect(200, '"API version: v1"'));
});
describe('bodyParser$ middleware', () => {
const context = createContext();
const effectMeta = createEffectMetadata({ ask: lookup(context) });
beforeEach(() => {
spyOn(console, 'log').and.stub();
spyOn(console, 'error').and.stub();
});
test('passes through non POST || PATCH || PUT requests', () => {
const request = new MockReq({
method: 'GET',
});
Marbles.assertEffect(bodyParser$(), [
['-a-', { a: request }],
['-a-', { a: request }],
]);
describe('notFoundEffect$', () => {
const app = httpListener.run(createContext());
test('GET api/v1/undefined responds with 400', async () =>
request(app)
.get('/api/v1/undefined')
.expect(404, { error: { status: 404, message: 'Route not found' } }));
});
describe('getMeEffect$', () => {
const app = httpListener.run(createContext());
test('GET /api/v1/users/me returns 200 and currently logged user details', async () => {
const user = await mockUser();
const token = await mockAuthorizationFor(user)(app);
return request(app)
.get('/api/v1/users/me')
.set('Authorization', `Bearer ${token}`)
.expect(200)
.then(({ body }) => {
expect(body._id).toEqual(String(user._id));
expect(body.email).toEqual(user.email);
expect(body.firstName).toEqual(user.firstName);
expect(body.lastName).toEqual(user.lastName);
expect(body.roles).toBeDefined();
expect(body.password).toBeUndefined();
describe('getFileEffect$', () => {
const app = httpListener.run(createContext());
test('GET api/v1/assets/:dir responds with 200 for actors entity', async () =>
request(app)
.get('/api/v1/assets/img/actors/placeholder.jpg')
.expect(200));
test('GET api/v1/assets/:dir responds with 200 for movies entity', async () =>
request(app)
.get('/api/v1/assets/img/movies/placeholder.jpg')
.expect(200));
test('GET api/v1/assets/:dir responds with 404 if file is not found', async () =>
request(app)
.get('/api/v1/assets/img/not_found.jpg')
.expect(404, { error: {
status: 404,
const bootstrap = async () => {
await Database.connect();
await Server.create(httpListener.run(createContext()));
};
export const create = async (httpListener: HttpListener) => {
const httpListenerWithContext = httpListener.run(createContext());
createServer(httpListenerWithContext)
.listen(port)
.on('close', onClose)
.on('error', onError)
.on('listening', onListen);
};
}