Skip to content

Commit

Permalink
feat(repeater): get rid of supporting web sockets
Browse files Browse the repository at this point in the history
closes #196
  • Loading branch information
ostridm committed May 16, 2024
1 parent 92c6eca commit fd2e5cd
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 433 deletions.
23 changes: 2 additions & 21 deletions packages/repeater/src/api/ExecuteRequestEventHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'reflect-metadata';
import { ExecuteRequestEventHandler } from './ExecuteRequestEventHandler';
import { Protocol } from '../models';
import { Request, RequestRunner } from '../request-runner';
import { anything, capture, instance, mock, reset, when } from 'ts-mockito';
import { RequestRunner } from '../request-runner';
import { anything, instance, mock, reset, when } from 'ts-mockito';

describe('ExecuteRequestEventHandler', () => {
const requestRunnerResponse = {
Expand Down Expand Up @@ -65,24 +65,5 @@ describe('ExecuteRequestEventHandler', () => {

await expect(res).rejects.toThrow(`Unsupported protocol "http"`);
});

it('`correlation_id_regex` should become `correlationIdRegex` in runner input', async () => {
const payload = {
protocol: Protocol.HTTP,
url: 'http://foo.bar/',
headers: {},
correlation_id_regex: 'baz'
};
const handler = new ExecuteRequestEventHandler([
instance(mockedRequestRunner)
]);

await handler.handle(payload);

const [request]: [Request] = capture<Request>(
mockedRequestRunner.run
).first();
expect(request.correlationIdRegex).toBeInstanceOf(RegExp);
});
});
});
29 changes: 3 additions & 26 deletions packages/repeater/src/lib/RepeaterFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { RepeaterFactory } from './RepeaterFactory';
import {
HttpRequestRunner,
RequestRunner,
RequestRunnerOptions,
WsRequestRunner
RequestRunnerOptions
} from '../request-runner';
import { Repeater } from './Repeater';
import { RepeatersManager } from '../api';
Expand Down Expand Up @@ -215,7 +214,7 @@ describe('RepeaterFactory', () => {
const factory = new RepeaterFactory(configuration);

await factory.createRepeater({
requestRunners: [HttpRequestRunner, WsRequestRunner]
requestRunners: [HttpRequestRunner]
});

verify(
Expand All @@ -229,17 +228,6 @@ describe('RepeaterFactory', () => {
})
)
).once();
verify(
mockedChildContainer.register(
RequestRunner,
deepEqual({
useClass: WsRequestRunner
}),
deepEqual({
lifecycle: Lifecycle.ContainerScoped
})
)
).once();
});

it('should throw an error if name prefix is too long', async () => {
Expand Down Expand Up @@ -331,7 +319,7 @@ describe('RepeaterFactory', () => {
const existingRepeaterId = '123';

await factory.createRepeaterFromExisting(existingRepeaterId, {
requestRunners: [HttpRequestRunner, WsRequestRunner]
requestRunners: [HttpRequestRunner]
});

verify(
Expand All @@ -345,17 +333,6 @@ describe('RepeaterFactory', () => {
})
)
).once();
verify(
mockedChildContainer.register(
RequestRunner,
deepEqual({
useClass: WsRequestRunner
}),
deepEqual({
lifecycle: Lifecycle.ContainerScoped
})
)
).once();
});
});
});
3 changes: 1 addition & 2 deletions packages/repeater/src/models/Protocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export enum Protocol {
HTTP = 'http',
WS = 'ws'
HTTP = 'http'
}
7 changes: 1 addition & 6 deletions packages/repeater/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { DefaultRepeatersManager, RepeatersManager } from './api';
import {
HttpRequestRunner,
RequestRunner,
RequestRunnerOptions,
WsRequestRunner
RequestRunnerOptions
} from './request-runner';
import {
container,
Expand All @@ -27,10 +26,6 @@ container.register(RequestRunner, {
useClass: HttpRequestRunner
});

container.register(RequestRunner, {
useClass: WsRequestRunner
});

container.register(RequestRunnerOptions, {
useValue: {
timeout: 30000,
Expand Down
11 changes: 0 additions & 11 deletions packages/repeater/src/request-runner/Request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ describe('Request', () => {
).toThrow('Body must be string.');
});

it('should throw Error on invalid correlationIdRegex', () => {
expect(
() =>
new Request({
url: 'http://foo.bar',
correlationIdRegex: '(',
protocol: Protocol.HTTP
})
).toThrow('Correlation id must be regular expression.');
});

it('should create an instance', () => {
expect(
() =>
Expand Down
25 changes: 1 addition & 24 deletions packages/repeater/src/request-runner/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface RequestOptions {
method?: string;
headers?: Record<string, string | string[]>;
body?: string;
correlationIdRegex?: string | RegExp;
}

export class Request {
Expand All @@ -30,7 +29,6 @@ export class Request {
public readonly protocol: Protocol;
public readonly url: string;
public readonly body?: string;
public readonly correlationIdRegex?: RegExp;

private readonly _method?: string;

Expand All @@ -48,20 +46,11 @@ export class Request {
return this.url.startsWith('https');
}

constructor({
protocol,
method,
url,
body,
correlationIdRegex,
headers = {}
}: RequestOptions) {
constructor({ protocol, method, url, body, headers = {} }: RequestOptions) {
this.protocol = protocol;
this._method = method?.toUpperCase() ?? 'GET';
this.validateUrl(url);
this.url = url;
this.correlationIdRegex =
this.normalizeCorrelationIdRegex(correlationIdRegex);
this.setHeaders(headers);
this.precheckBody(body);
this.body = body;
Expand Down Expand Up @@ -99,16 +88,4 @@ export class Request {
throw new Error('Body must be string.');
}
}

private normalizeCorrelationIdRegex(
correlationIdRegex: RegExp | string | undefined
): RegExp | undefined {
if (correlationIdRegex) {
try {
return new RegExp(correlationIdRegex, 'i');
} catch {
throw new Error('Correlation id must be regular expression.');
}
}
}
}
4 changes: 2 additions & 2 deletions packages/repeater/src/request-runner/Response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('Response', () => {
describe('constructor', () => {
it('should create an instance having only protocol', () => {
const responseOptions = {
protocol: Protocol.WS
protocol: Protocol.HTTP
};

const response = new Response(responseOptions);
Expand All @@ -15,7 +15,7 @@ describe('Response', () => {

it('should create an instance with full fieldset', () => {
const responseOptions = {
protocol: Protocol.WS,
protocol: Protocol.HTTP,
statusCode: 200,
headers: { 'x-key': ['x-value'] },
body: '{}',
Expand Down
133 changes: 0 additions & 133 deletions packages/repeater/src/request-runner/protocols/WsRequestRunner.spec.ts

This file was deleted.

Loading

0 comments on commit fd2e5cd

Please sign in to comment.