Skip to content

keighl/dierks

Repository files navigation

Dierks

Build Status codecov.io GoDoc

Dierks is a library for stubbing HTTP responses in Golang tests using chained methods. Perfect for testing API wrappers!

Installation

go get -u github.com/keighl/dierks

Basic Usage

Dierks stubs HTTP responses for requests made through an http.Client. Here's a simple example showing how to stub the response body for a request to google.com.

// example_test.go

package dierks

import (
    "io/ioutil"
    "strings"
    "testing"
)

func TestGoogle(t *testing.T) {

    responsePayload := `{"user": {"id": 3, "name": "Kyle"}}`

    // Dierks generates a test server, and an http.Client.
    // Any request through the client will generate a response with
    // `responsePayload` in the body
    server, client := dierks.Res().Body(responsePayload).Start()
    defer server.Close()

    // Make a request through the client
    resp, _ := client.Get("http://google.com")

    // Let's look at what the response has...
    body, _ := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()

    // What?! Google returned my JSON! Cool!
    expect(t, string(body), responsePayload)
}

More realistically, you might use dierks to test a API wrapper-lib method.

func TestGetUser(t *testing.T) {
    responsePayload := `{"user": {"id": 3, "name": "Kyle"}}`

    server, client := dierks.Res().Body(responsePayload).Start()
    defer server.Close()

    apiClient := &APIClient{CustomHTTPClient: client}

    user, err := apiClient.GetUser(3)

    expect(t, err, nil)
    expect(t, user.ID, 3)
    expect(t, user.Name, "Kyle")
}

Building a Response

About

Stub HTTP responses in Golang tests using chained methods

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages