Skip to content

Commit

Permalink
feat(tcp): implements tcp parser and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Apr 6, 2020
1 parent 8929d9e commit 4b1b8fa
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/platform-tcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
"url": "git+https://github.com/jmcdo29/ogma.git"
},
"scripts": {
<<<<<<< HEAD
"test": "echo \"No tests to run\""
=======
"build": "tsc -b tsconfig.build.json",
"prebuild": "rimraf lib",
"build": "tsc -p tsconfig.build.json",
"postbuild": "mv ./lib/src/* ./lib && rmdir lib/src",
"test": "jest",
"test:cov": "jest --coverage"
>>>>>>> feat(tcp): implements first draft of tcp parser
},
"bugs": {
"url": "https://github.com/jmcdo29/ogma/issues"
Expand Down
94 changes: 94 additions & 0 deletions packages/platform-tcp/test/tcp-interceptor.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createMock } from '@golevelup/ts-jest';
import { ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { PATTERN_METADATA } from '@nestjs/microservices/constants';
import { Test } from '@nestjs/testing';
import { color } from '@ogma/logger';
import { TcpParser } from '../src';

describe('TcpParser', () => {
let parser: TcpParser;
let reflector: Reflector;

beforeEach(async () => {
const mod = await Test.createTestingModule({
providers: [
TcpParser,
{
provide: Reflector,
useValue: {
get: jest.fn(() => 'message'),
},
},
],
}).compile();
parser = mod.get(TcpParser);
reflector = mod.get(Reflector);
});

describe('getCallPoint', () => {
it('should get the reflected message', () => {
const funcMock = () => 'string';
const ctxMock = createMock<ExecutionContext>({
getHandler: () => funcMock(),
});
expect(parser.getCallPoint(ctxMock)).toBe(JSON.stringify('message'));
expect(reflector.get).toBeCalledWith(PATTERN_METADATA, funcMock());
});
});
describe('getCallerIp', () => {
it('should get the ip from handshake', () => {
const ctxMock = createMock<ExecutionContext>({
switchToRpc: () => ({
getContext: () => ({
getSocketRef: () => ({
socket: {
remoteAddress: '127.0.0.1',
remotePort: '3333',
},
}),
}),
}),
});
expect(parser.getCallerIp(ctxMock)).toBe('127.0.0.1:3333');
});
});
describe('getProtocol', () => {
it('should return IPv4', () => {
const ctxMock = createMock<ExecutionContext>({
switchToRpc: () => ({
getContext: () => ({
getSocketRef: () => ({
socket: {
remoteFamily: 'IPv4',
},
}),
}),
}),
});
expect(parser.getProtocol(ctxMock)).toBe('IPv4');
});
});
describe('getMethod', () => {
it('should return TCP', () => {
expect(parser.getMethod()).toBe('TCP');
});
});
describe('getStatus', () => {
it('should return a 200', () => {
expect(parser.getStatus(createMock<ExecutionContext>(), false)).toBe(
'200',
);
});
it('should return a 500', () => {
expect(
parser.getStatus(createMock<ExecutionContext>(), false, new Error()),
).toBe('500');
});
it('should return a 200 in color', () => {
expect(parser.getStatus(createMock<ExecutionContext>(), true)).toBe(
color.green(200),
);
});
});
});

0 comments on commit 4b1b8fa

Please sign in to comment.