Skip to content

Commit

Permalink
feat: add support for options
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-bonnefond authored and ybonnefond committed Oct 22, 2024
1 parent e8f3e78 commit d452bf9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
18 changes: 14 additions & 4 deletions src/Stubborn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export class Stubborn implements Emitter {
private server: Server;
private port: number | null;
private router: Router;
private options: StubbornOptions;
private stubbornOptions: StubbornOptions;
private emitter: EventEmitter = new EventEmitter();

constructor(options: StubbornOptions = {}) {
this.options = Object.assign(
this.stubbornOptions = Object.assign(
{
host: 'localhost',
defaultHeaders: {},
Expand All @@ -44,7 +44,7 @@ export class Stubborn implements Emitter {

this.port = null;
this.server = createServer();
this.router = new Router({ host: this.options.host }, this.emitter);
this.router = new Router({ host: this.stubbornOptions.host }, this.emitter);
}

/**
Expand All @@ -63,7 +63,7 @@ export class Stubborn implements Emitter {
* @returns the server origin
*/
public getOrigin(): string {
return `http://${this.options.host}:${this.getPort()}`;
return `http://${this.stubbornOptions.host}:${this.getPort()}`;
}

/**
Expand Down Expand Up @@ -135,6 +135,16 @@ export class Stubborn implements Emitter {
return this.router.createRoute(METHODS.HEAD, path);
}

/**
* Create and Register a new HEAD route
*
* @param path Path matching definition
* @param body Request body definition
*/
public options(path: PathDefinition) {
return this.router.createRoute(METHODS.OPTIONS, path);
}

/**
* Register a new route
*
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum METHODS {
PUT = 'PUT',
PATCH = 'PATCH',
DELETE = 'DELETE',
OPTIONS = 'OPTIONS',
}

export enum STATUS_CODES {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Stubborn } from '../../src';
export interface HttpClientRequest {
path?: string;
headers?: Record<string, string>;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
responseType?: 'json' | 'text' | 'arraybuffer';
data?: string | Record<string, unknown> | FormData;
query?: Record<string, string> | URLSearchParams;
Expand Down
33 changes: 21 additions & 12 deletions test/specs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ describe('index', () => {
});
});

it('should return SUCCESS if method is OPTIONS', async () => {
sb.options('/');

expect(
await httpClient.request({
method: 'OPTIONS',
path: '/',
}),
).toReplyWith({
status: STATUS_CODES.SUCCESS,
});
});

it('should return SUCCESS if method is POST', async () => {
sb.post('/');

Expand Down Expand Up @@ -684,12 +697,10 @@ describe('index', () => {
const form = new FormData();
form.append('stream1', Readable.from(Buffer.from('Hello')));

sb.post('/', { stream1: 'Hello' })
.setHeaders({
...form.getHeaders(),
'transfer-encoding': 'chunked',
})
.logDiffOn501();
sb.post('/', { stream1: 'Hello' }).setHeaders({
...form.getHeaders(),
'transfer-encoding': 'chunked',
});

expect(
await httpClient.request({
Expand All @@ -711,12 +722,10 @@ describe('index', () => {
filename: 'file.txt',
content: 'Hello',
},
})
.setHeaders({
...form.getHeaders(),
'transfer-encoding': 'chunked',
})
.logDiffOn501();
}).setHeaders({
...form.getHeaders(),
'transfer-encoding': 'chunked',
});

expect(
await httpClient.request({
Expand Down

0 comments on commit d452bf9

Please sign in to comment.