1.0.10 • Published 4 years ago

simple-wiremock v1.0.10

Weekly downloads
14
License
ISC
Repository
github
Last release
4 years ago

SIMPLE WIREMOCK


Summary

This project was created to help with tests mocking Rest API from Backend.

Instructions to setup the project

To use the package you need execute the command:

npm i -D simple-wiremock

Importing and using it

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))

The example object response used to response:

    {
        status: number,
        headers: { '<Key>': 'value' },
        body: your object response.
    }

body accept a object and list.

Example mock returning a list:

simpleWiremock.get('/users', {
    status: 200,
    headers: { 'Content-Type': 'applications/json' },
    body: [
        {
            name: 'Austin Power',
            age: 40
        },
        {
            name: 'Ana Clara',
            age: 28
        }
    ]
});

Example with another http methods

  • 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.

How to start the SimpleWiremock in specific port?

    
    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.

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago