Skip to content

Commit aa5d6b5

Browse files
committed
feat: add ability to delete requests
1 parent 01712b1 commit aa5d6b5

5 files changed

+64
-3
lines changed

lib/wiremock_mapper.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,36 @@ describe('WireMockMapper', () => {
396396
await expect(promise).rejects.toThrow();
397397
});
398398
});
399+
400+
describe('deleteRequests', () => {
401+
it('sends a DELETE request to wiremock', async () => {
402+
nock('http://localhost:8080', {}).delete('/__admin/requests').reply(200);
403+
404+
const promise = WireMockMapper.deleteRequests();
405+
406+
await expect(promise).resolves.toBeUndefined();
407+
});
408+
409+
it('sends a DELETE request with stub id to wiremock', async () => {
410+
const stubId = 'abc';
411+
412+
nock('http://localhost:8080', {})
413+
.delete(`/__admin/requests/${stubId}`)
414+
.reply(200);
415+
416+
const promise = WireMockMapper.deleteRequests({ stubId });
417+
418+
await expect(promise).resolves.toBeUndefined();
419+
});
420+
421+
it('rejects the promise if there was an error with the request', async () => {
422+
nock('http://localhost:8080')
423+
.delete('/__admin/requests')
424+
.replyWithError('something went wrong...sorry dude...');
425+
426+
const promise = WireMockMapper.deleteRequests();
427+
428+
await expect(promise).rejects.toThrow();
429+
});
430+
});
399431
});

lib/wiremock_mapper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ export class WireMockMapper {
5050
.catch(reject);
5151
});
5252
}
53+
54+
public static async deleteRequests(
55+
deleteRequestOptions?: GetRequestOptions
56+
): Promise<void> {
57+
return WireMockService.deleteRequests(deleteRequestOptions);
58+
}
5359
}

lib/wiremock_service.ts

+23
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ export class WireMockService {
4444
});
4545
}
4646

47+
public static async deleteRequests(
48+
option?: DeleteRequestOptions
49+
): Promise<void> {
50+
return new Promise<void>((resolve, reject) => {
51+
const request = http.request(
52+
{
53+
hostname: Configuration.wireMockHost,
54+
method: 'DELETE',
55+
path: [this.WIREMOCK_REQUESTS_PATH, option?.stubId]
56+
.filter(Boolean)
57+
.join('/'),
58+
port: Configuration.wireMockPort
59+
},
60+
WireMockService.responseHandler(() => {
61+
resolve();
62+
}, reject)
63+
);
64+
request.on('error', reject);
65+
request.end();
66+
});
67+
}
68+
4769
public static async getRequests(option?: GetRequestOptions): Promise<string> {
4870
const queryString = option?.stubId ? `matchingStub=${option.stubId}` : '';
4971

@@ -124,3 +146,4 @@ export class WireMockService {
124146

125147
type RequestsByStubId = { stubId: string };
126148
export type GetRequestOptions = undefined | RequestsByStubId;
149+
export type DeleteRequestOptions = undefined | RequestsByStubId;

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wiremock-mapper",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "DSL for setting up WireMock mappings.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)