node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

@toincrease/node-mountebank

node-mountebank

A simple node package that lets you manage your Mountebank test server.

Installation

go get "bitbucket.org/to-increase/go-mountebank"
 
import(mb "bitbucket.org/to-increase/go-mountebank")

Usage

Pre-Requisite

Install Mountebank:

npm install -g mountebank --production

Start Mountebank:

mb 

I recommend reading the Mountebank documentation for a deeper understanding of their API.

Create Imposter

imp := mb.NewImposter().WithPort(1974)WithStub(stb)
err := imp.Create()

Create a Stub

stb := mb.NewStub()

Create a new Predicate

    mb.NewPredicate().WithEquals().
        WithMethod("GET").
        WithPath("/test123").
        WithHeader("Content-Type", "application/json").
        WithHeader("Authorization", "bearer xxx")

Create a new Response

    emp := &Employee{}
    mb.NewResponse().
        WithStatusCode(200).
        WithBody(emp).
        WithHeader("Conent-Type", "application/text"))

Create a new Response with a wait behavior

    emp := &Employee{}
    mb.NewResponse().
        WithStatusCode(200).
        WithBody(emp).
        WithWait(1000).
        WithHeader("Conent-Type", "application/text"))

Using mountebank in go tests

check if mountebank is running in TestMain

    func TestMain(m *testing.M) {
        if !mb.IsRunning() {
            panic(fmt.Errorf("Mountebank is not running; see http://mbtest.org for installation instructions"))
        }
        os.Exit(m.Run())
    }

add a configuration

    imp, err := mb.NewImposter().WithStub(mb.NewStub().WithResponse(mb.NewResponse().WithBody(b))).Create()
    assert.Nil(t, err)

this returns an imposter with a randomly assinged port number;

client := authentication.New(config.NewConfig().WithEndpoint(fmt.Sprintf("http://localhost:%v", imp.Port)))

create a default imposter

    emp := &Employee{}
    imp, err := mb.DefaultPostImposter("/item", emp, http.StatusCreated)