Lightweight REST client to interact with a running WireMock server
npm install wiremock-rest-client
- WireMock REST Client
- Installation
- Usage
- API
- Global
- Stub mappings
- Recordings
- Requests
- Scenarios
- Configuration
- Proxy
- Headers
- Log level
- Continue on failure
- CLI
npm install wiremock-rest-client
`Usage
The WireMockRestClient has 5 services which correspond with request paths as specified in the OpenAPI spec.
- global - Global operations
- mappings - Operations on stub mappings
- recordings - Stub mapping record and snapshot functions
- requests - Logged requests and responses received
- scenarios - Scenarios support modeling of stateful behaviorSee the API for the available methods. All methods are related to available operations on the WireMock server endpoints.
`js
import { WireMockRestClient } from 'wiremock-rest-client';const wireMock = new WireMockRestClient('http://localhost:8080');
const stubMappings = await wireMock.mappings.getAllMappings();
console.log(stubMappings);
await wireMock.global.shutdown();
`API
$3
- updateGlobalSettings(delayDefinition: DelayDefinition): Promise
- resetAll(): Promise
- shutdown(): PromiseExample:
`js
await wireMockRestClient.global.resetAll();
`$3
- getAllMappings(): Promise
- createMapping(stubMapping: StubMapping): Promise
- createMappingFromFile(fileName: string): Promise
- createMappingsFromDir(directoryName: string): Promise
- deleteAllMappings(): Promise
- resetAllMappings(): Promise
- getMapping(uuid: string): Promise
- updateMapping(uuid: string, stubMapping: StubMapping): Promise
- deleteMapping(uuid: string): Promise
- saveAllMappings(): Promise
- findByMetaData(contentPattern: ContentPattern): Promise
- removeByMetaData(contentPattern: ContentPattern): PromiseExample:
`js
await wireMockRestClient.mappings.resetAllMappings();const stubMapping = {
"request": {
"method": "GET",
"urlPathPattern": "/api/helloworld"
},
"response": {
"status": 200,
"jsonBody": {"hello": "world"},
"headers": {
"Content-Type": "application/json"
}
}
};
const response = await wireMock.mappings.createMapping(stubMapping);
// Create mapping from current working directory
await wireMock.mappings.createMappingFromFile('stubs/hello-world.json');
// Create mappings from a directory recursively (based on current working directory)
await wireMock.mappings.createMappingsFromDir('stubs');
`$3
- startRecording(recordSpec: RecordSpec): Promise
- stopRecording(): Promise
- getRecordingStatus(): Promise
- takeSnapshotRecording(snapshotSpec: RecordSpec): PromiseExample:
`js
const recordingStatus = wireMockRestClient.recordings.getRecordingStatus();
`$3
- getAllRequests(): Promise
- deleteAllRequests(): Promise
- getRequest(uuid: string): Promise
- deleteRequest(uuid: string): Promise
- resetAllRequests(): Promise
- getCount(requestPattern: RequestPattern): Promise
- removeRequests(requestPattern: RequestPattern): Promise
- removeRequestsByMetadata(contentPattern: ContentPattern): Promise
- findRequests(requestPattern: RequestPattern): Promise
- getUnmatchedRequests(): Promise
- getUnmatchedNearMisses(): Promise
- getNearMissesByRequest(loggedRequest: LoggedRequest): Promise
- getNearMissesByRequestPattern(requestPattern: RequestPattern): PromiseExample:
`js
const requests = await wireMockRestClient.requests.getAllRequests();
`$3
- getAllScenarios(): Promise
- resetAllScenarios(): Promise
- resetScenario(scenarioId: string): Promise
- setScenarioState(scenarioId: string, state: string): PromiseExample:
`js
await wireMockRestClient.scenarios.resetAllScenarios();
`Configuration
The following optional configuration options are available, to be set via options or environment variables.| Configuration option | Default | Options property | Environment variable |
|----------------------------------------------|-----------------------|---------------------|---------------------------|
| Proxy | No proxy |
proxy | WRC_HTTP_PROXY |
| Headers | No additional headers | headers | WRC_HEADERS |
| Log level | info | logLevel | WRC_LOG_LEVEL |
| Continue on failure | false | continueOnFailure | WRC_CONTINUE_ON_FAILURE |Example using options:
`js
const wireMock = new WireMockRestClient('http://localhost:8080', {
proxy: 'http://mycorporateproxy.com',
headers: {Authorization: 'some-token'},
logLevel: 'debug',
continueOnFailure: true
});
`Note: Environment variables have the highest priority
$3
The proxy URL can be HTTP or HTTPS. Credentials for authentication can be passed in the URL.Example:
`
WRC_HTTP_PROXY=http://username:secret@mycorporateproxy.com
`$3
Additional headers can be added to all HTTP requests.Example:
`
WRC_HEADERS="{Authorization: 'some-token'}"
`$3
- Default log level is info
- Each log line contains a unique id to trace logs for a single request
- Log level debug will log the request body for each request.A different log level can be configured by setting the environment variable
WRC_LOG_LEVEL to specific a log level (trace/debug/info/warn/error/silent)`shell
2019-12-11T20:43:18.157Z INFO wiremock-rest-client: [Uub8jVUBq91F3MIyKXrON] Request: [POST] http://localhost:8080/__admin/mappings
2019-12-11T20:43:18.157Z DEBUG wiremock-rest-client: [Uub8jVUBq91F3MIyKXrON] Request body: {"request":{"method":"GET","urlPathPattern":"/api/helloworld"},"response":{"status":200,"jsonBody":{"hello":"world"},"headers":{"Content-Type":"application/json"}}}
2019-12-11T20:43:18.158Z INFO wiremock-rest-client: [Uub8jVUBq91F3MIyKXrON] Response: [201] Created
2019-12-11T20:43:18.158Z INFO wiremock-rest-client: [EDYjmman5BLb7tgws-VGg] Request: [POST] http://localhost:8080/__admin/shutdown
2019-12-11T20:43:18.161Z INFO wiremock-rest-client: [EDYjmman5BLb7tgws-VGg] Response: [200] OK
`$3
- By default the node process is exited in case of a failure
- To change this behavior to continue on failure, set the value to trueCLI
A small CLI is available to load data from the command line by passing the folder which contains the stub mappings to be loaded. By default it will reset all stub mappings first (configurable).`
$ wrc load --helpUsage: wrc load [options]
Options:
-f, --folders Comma separated list of folders containing stub mappings to be loaded
--no-reset Skip resetting all stub mappings
-u, --uri [uri] WireMock base URI (default: "http://localhost:8080")
-h, --help display help for command
``