Help us shape the future of Go! Take the 2026 Go Developer Survey

spyserver

package module
v0.0.0-...-8e6f7d1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: MIT Imports: 4 Imported by: 0

README

SpyServer for Go

Create simple, local test servers that records requests and responds with pre-configured results.

Why use this

Test code making http calls without having a slow, unreliable network in the way. Simplify testing when making calls to external services.

Go's httptest package is good, but this library gives you a few more benefits:

  1. You don't need to point your tests to a different URL.
  2. You can test that your client code closes the response body.
  3. You can simulate various errors that may happen with the transport.

How to use

First, create a spyserver like this:

response := &http.Response{
    StatusCode: http.StatusOK,
    Body:       body,
}
err := fmt.Errorf("error")
spy := SpyServer{response, err}

Next, set the Transport in your http client to the spyserver and call it:

client := &http.Client{
    Transport: spy,
}
client.Do(request)

You can inspect the request sent to the server in your tests:

spy.GetRequest()

You can even test that you close the response body in your production code.

body := spyserver.NewCloseDetectorFromString(jsonString)

// Use the body the response you configure with your spy server
// Then just inject the spy server as usual and call your code

body.IsClosed() // returns true if you closed the response body

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseDetector

type CloseDetector struct {
	io.ReadCloser
	// contains filtered or unexported fields
}

CloseDetector is an io.ReadCloser that tracks whether Close has been called. This struct is useful because the behavior of Close after the first call is undefined.

func NewCloseDetector

func NewCloseDetector(rc io.ReadCloser) *CloseDetector

NewCloseDetector wraps an existing io.ReadCloser.

func NewCloseDetectorFromString

func NewCloseDetectorFromString(s string) *CloseDetector

NewCloseDetectorFromString wraps a NopCloser containing the string

func (*CloseDetector) Close

func (cd *CloseDetector) Close() error

Close delegates to the wrapped Close function and tracks whether the function was called.

func (*CloseDetector) IsClosed

func (cd *CloseDetector) IsClosed() bool

IsClosed returns whether Close was called

func (*CloseDetector) Read

func (cd *CloseDetector) Read(p []byte) (n int, err error)

Read is a pass-through function that delegates to the wrapped Read function.

type SpyServer

type SpyServer struct {
	Response *http.Response
	Error    error
	// contains filtered or unexported fields
}

SpyServer implements the http.RoundTripper interface to serve a custom http.Response or error while capturing the request for later inspection A pointer to this type can be assigned to http.Client.Transport to override the default RoundTripper

func (*SpyServer) GetRequest

func (s *SpyServer) GetRequest() *http.Request

GetRequest returns the http.Request that was passed during the last RoundTrip

func (*SpyServer) RoundTrip

func (s *SpyServer) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface

Jump to

Keyboard shortcuts

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