httpstub

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 12 Imported by: 3

README

httpstub Go Reference Coverage Code to Test Ratio Test Execution Time

httpstub provides router ( http.Handler ), server ( *httptest.Server ) and client ( *http.Client ) for stubbing, for testing in Go.

There is an gRPC version stubbing tool with the same design concept, grpcstub.

Usage

package myapp

import (
	"io"
	"net/http"
	"testing"

	"github.com/k1LoW/httpstub"
)

func TestGet(t *testing.T) {
	ts := httpstub.NewServer(t)
	t.Cleanup(func() {
		ts.Close()
	})
	ts.Method(http.MethodGet).Path("/api/v1/users/1").Header("Content-Type", "application/json").ResponseString(http.StatusOK, `{"name":"alice"}`)
	tc := ts.Client()

	res, err := tc.Get("https://example.com/api/v1/users/1")
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		res.Body.Close()
	})
	body, err := io.ReadAll(res.Body)
	if err != nil {
		t.Fatal(err)
	}
	got := string(body)
	want := `{"name":"alice"}`
	if got != want {
		t.Errorf("got %v\nwant %v", got, want)
	}
	if len(ts.Requests()) != 1 {
		t.Errorf("got %v\nwant %v", len(ts.Requests()), 1)
	}
}

or

package myapp

import (
	"io"
	"net/http"
	"testing"

	"github.com/k1LoW/httpstub"
)

func TestGet(t *testing.T) {
	r := httpstub.NewRouter(t)
	r.Method(http.MethodGet).Path("/api/v1/users/1").Header("Content-Type", "application/json").ResponseString(http.StatusOK, `{"name":"alice"}`)
	ts := r.Server()
	t.Cleanup(func() {
		ts.Close()
	})
	tc := ts.Client()

	res, err := tc.Get("https://example.com/api/v1/users/1")
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		res.Body.Close()
	})
	body, err := io.ReadAll(res.Body)
	if err != nil {
		t.Fatal(err)
	}
	got := string(body)
	want := `{"name":"alice"}`
	if got != want {
		t.Errorf("got %v\nwant %v", got, want)
	}
	if len(r.Requests()) != 1 {
		t.Errorf("got %v\nwant %v", len(r.Requests()), 1)
	}
}

Example

Stub Twilio
package client_test

import (
	"net/http"
	"testing"

	"github.com/k1LoW/httpstub"
	twilio "github.com/twilio/twilio-go"
	twclient "github.com/twilio/twilio-go/client"
	api "github.com/twilio/twilio-go/rest/api/v2010"
)

func TestTwilioClient(t *testing.T) {
	r := httpstub.NewRouter(t)
	r.Method(http.MethodPost).Path("/2010-04-01/Accounts/*/Messages.json").ResponseString(http.StatusCreated, `{"status":"sending"}`)
	ts := r.Server()
	t.Cleanup(func() {
		ts.Close()
	})
	tc := ts.Client()

	accountSid := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
	authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
	client := twilio.NewRestClientWithParams(twilio.ClientParams{
		Client: &twclient.Client{
			Credentials: twclient.NewCredentials(accountSid, authToken),
			HTTPClient:  tc,
		},
	})
	params := &api.CreateMessageParams{}
	params.SetTo("08000000000")
	params.SetFrom("05000000000")
	params.SetBody("Hello there")
	res, err := client.ApiV2010.CreateMessage(params)
	if err != nil {
		t.Error(err)
	}

	got := res.Status
	want := "sending"
	if *got != want {
		t.Errorf("got %v\nwant %v", *got, want)
	}
}

Alternatives

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v0.4.0

type Option func(*config) error

func CACert added in v0.4.1

func CACert(cacert []byte) Option

CACert set CA

func Certificates added in v0.4.1

func Certificates(cert, key []byte) Option

Certificates set certificates ( cert, key )

func ClientCACert added in v0.5.0

func ClientCACert(cacert []byte) Option

ClientCACert set client CA

func ClientCertificates added in v0.5.0

func ClientCertificates(cert, key []byte) Option

ClientCertificates set client certificates ( cert, key )

func UseTLS added in v0.4.0

func UseTLS() Option

UseTLS enable TLS

func UseTLSWithCertificates added in v0.4.0

func UseTLSWithCertificates(cert, key []byte) Option

UseTLSWithCertificates enable TLS with certificates ( cert, key )

type Router added in v0.3.0

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter(t *testing.T, opts ...Option) *Router

NewRouter returns a new router with methods for stubbing.

func NewServer added in v0.2.0

func NewServer(t *testing.T, opts ...Option) *Router

NewServer returns a new router including *httptest.Server.

func NewTLSServer added in v0.4.0

func NewTLSServer(t *testing.T, opts ...Option) *Router

NewTLSServer returns a new router including TLS *httptest.Server.

func (*Router) ClearRequests added in v0.6.0

func (rt *Router) ClearRequests()

ClearRequests clear []*http.Request received by router.

func (*Router) Client added in v0.3.0

func (rt *Router) Client() *http.Client

Client returns *http.Client which requests *httptest.Server.

func (*Router) Close added in v0.3.0

func (rt *Router) Close()

Close shuts down *httptest.Server

func (*Router) DefaultHeader added in v0.3.0

func (rt *Router) DefaultHeader(key, value string)

DefaultHeader append default middleware which append header.

func (*Router) DefaultMiddleware added in v0.3.0

func (rt *Router) DefaultMiddleware(mw func(next http.HandlerFunc) http.HandlerFunc)

DefaultMiddleware append default middleware.

func (*Router) Match added in v0.3.0

func (rt *Router) Match(fn func(r *http.Request) bool) *matcher

Match create request matcher with matchFunc (func(r *http.Request) bool).

func (*Router) Method added in v0.3.0

func (rt *Router) Method(method string) *matcher

Method create request matcher using method.

func (*Router) Path added in v0.3.0

func (rt *Router) Path(path string) *matcher

Path create request matcher using path.

func (*Router) Requests added in v0.3.0

func (rt *Router) Requests() []*http.Request

Requests returns []*http.Request received by router.

func (*Router) ServeHTTP added in v0.3.0

func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) Server added in v0.3.0

func (rt *Router) Server() *httptest.Server

Server returns *httptest.Server with *Router set.

func (*Router) TLSServer added in v0.4.0

func (rt *Router) TLSServer() *httptest.Server

TLSServer returns TLS *httptest.Server with *Router set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL