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';
test('authorize$ throws error if incoming request is not authorized', done => {
// given
const mockedSecret = 'test_secret';
const mockedToken = 'TEST_TOKEN';
const mockedRequest = { headers: { authorization: `Bearer ${mockedToken}`} } as HttpRequest;
const expectedError = new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED);
const req$ = of(mockedRequest);
const res = {} as HttpResponse;
// when
utilModule.parseAuthorizationHeader = jest.fn(() => mockedToken);
factoryModule.verifyToken$ = jest.fn(() => () => throwError(expectedError));
const middleware$ = authorize$({ secret: mockedSecret }, verifyPayload$)(req$, res, effectMeta);
// then
middleware$.subscribe(
() => {
fail(`Stream should throw an error`);
done();
},
test('authorize$ throws error if verifyPayload$ handler doesn\'t pass', done => {
// given
const mockedSecret = 'test_secret';
const mockedToken = 'TEST_TOKEN';
const mockedTokenPayload = { id: 'test_id_wrong' };
const mockedRequest = { headers: { authorization: `Bearer ${mockedToken}`} } as HttpRequest;
const expectedError = new HttpError('Unauthorized', HttpStatus.UNAUTHORIZED);
const req$ = of(mockedRequest);
const res = {} as HttpResponse;
// when
utilModule.parseAuthorizationHeader = jest.fn(() => mockedToken);
factoryModule.verifyToken$ = jest.fn(() => () => of(mockedTokenPayload));
const middleware$ = authorize$({ secret: mockedSecret }, verifyPayload$)(req$, res, effectMeta);
// then
middleware$.subscribe(
() => {
fail(`Stream should throw an error`);
done();
},
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())
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';
const mockedToken = 'TEST_TOKEN';
test('handles error if error$ is defined', () => {
// given
const client = { sendResponse: jest.fn() } as any as MarbleWebSocketClient;
const error = new EventError({ type: 'EVENT' }, '');
const error$: WsErrorEffect = event$ => event$.pipe(
mapTo({ type: error.event.type, error: {} }),
);
// when
handleEffectsError(defaultMetadata, client, error$)(error);
// then
expect(client.sendResponse).toHaveBeenCalled();
});
test('returns stream of error events for defined error object', () => {
// given
const incomingEvent = { type: 'TEST_EVENT' };
const error = new EventError(incomingEvent, 'Test error message', { errorData: 'test_error_data' });
const outgoingEvent = {
type: incomingEvent.type,
error: {
message: error.message,
data: error.data,
},
};
// then
Marbles.assertEffect(error$, [
['--a--', { a: incomingEvent }],
['--b--', { b: outgoingEvent }],
], { meta: { error } });
});
test('does nothing if error$ is undefined', () => {
// given
const client = { sendResponse: jest.fn() } as any as MarbleWebSocketClient;
const error = new EventError({ type: 'EVENT' }, '');
// when
handleEffectsError(defaultMetadata, client, undefined)(error);
// then
expect(client.sendResponse).not.toHaveBeenCalled();
});
});
import { getCommentByArticleEffect$ } from './effects/get-comment-by-article.effect';
import { postCommentByArticleEffect$ } from './effects/post-comment-by-article.effect';
const getCommentByArticle$ = EffectFactory.matchPath('/')
.matchType('GET')
.use(getCommentByArticleEffect$);
const postCommentByArticle$ = EffectFactory.matchPath('/')
.matchType('POST')
.use(postCommentByArticleEffect$);
// const removeCommentByArticle$ = EffectFactory.matchPath("/:commentId")
// .matchType("DELETE")
// .use(removeCommentEffect$);
export const comment$ = combineRoutes('/article/:articleId/comment', {
effects: [getCommentByArticle$, postCommentByArticle$],
});