This project was created to help with tests mocking Rest API from Backend.
To use the package you need execute the command:
npm i -D simple-wiremock
After installed the package, above following the slice example code:
- Example in Typescript:
import { SimpleWiremock } from 'simple-wiremock'
const should = require('should');
const request = require('request');
describe('TestComponent', () => {
let simpleWiremock: SimpleWiremock;
beforeAll(() => {
simpleWiremock = new SimpleWiremock().start();
simpleWiremock.get('/users/1', {
status: 200,
headers: { 'Content-Type': 'applications/json' },
body: {
name: 'Austin Power',
age: 40
}
});
});
it('Should simple request get for users by id', () => {
request('http://localhost:5001/users/1', (err, res, body) =>{
should.not.exist(err);
should.exist(res);
JSON.parse(body).should.containDeep({ name: 'Austin Power', age: 40 });
done();
});
});
afterAll(() => {
simpleWiremock.stop();
});
});
- Example in Javascript:
const sw = require('simple-wiremock');
const should = require('should');
const request = require('request');
let simpleWiremock = new sw.SimpleWiremock().start();
simpleWiremock.get('/users/1', {
status: 200,
headers: { 'Content-Type': 'applications/json' },
body: {
name: 'Austin Power',
age: 40
}
});
request('http://localhost:5001/users/1', (err, res, body) =>{
should.not.exist(err);
should.exist(res);
JSON.parse(body).should.containDeep({ name: 'Austin Power', age: 40 });
simpleWiremock.stop();
});
By default SimpleWiremock stay listening at port 5001
This SimpleWiremock has following methods: [ GET (simpleWiremock.get(url, object)), POST (simpleWiremock.post(url, object)), PUT (simpleWiremock.put(url, object)), PATCH (simpleWiremock.patch(url, object)), OPTIONS (simpleWiremock.options(url, object)), HEAD (simpleWiremock.head(url, object)) DELETE (simpleWiremock.delete(url, object)) ]
{
status: number,
headers: { '<Key>': 'value' },
body: your object response.
}
body accept a object and list.
simpleWiremock.get('/users', {
status: 200,
headers: { 'Content-Type': 'applications/json' },
body: [
{
name: 'Austin Power',
age: 40
},
{
name: 'Ana Clara',
age: 28
}
]
});
- Using HTTP POST
simpleWiremock.post('/users', {
status: 201,
headers: { 'Content-Type': 'applications/json' },
body:{
name: 'Austin Power',
age: 40
}
});
- Using HTTP PATCH
simpleWiremock.patch('/users', {
status: 202,
headers: { 'Content-Type': 'applications/json' },
body:{
name: 'Austin Power',
age: 40
}
});
- Using HTTP DELETE
simpleWiremock.delete('/users/1', {
status: 202,
headers: { 'Content-Type': 'applications/json' },
body:{}
});
- Using HTTP OPTIONS
simpleWiremock.options('/users/1', {
status: 204,
headers: {},
body:{}
});
When We don't send the headers, the SimpleWiremock always return Content-Type = application/json.
beforeAll(() => {
simpleWiremock = new SimpleWiremock().setPort(5002).start();
simpleWiremock.get('/users/1', {
status: 200,
headers: { 'Content-Type': 'applications/json' },
body: {
name: 'Austin Power',
age: 40
}
});
});
...
OBS: We always need to stop the server after our tests.