testsrv

package module
v0.0.0-...-dd99f75 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

testsrv

GoDoc Build Status

testsrv is a library for running real HTTP servers in the same process as Go tests, and inspecting the requests that the servers received.

Sample Usage

myHandler := func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Hello Gophers!"))
}
srv := testsrv.StartServer(http.HandlerFunc(myHandler))
defer srv.Close()
resp, err := http.Get(srv.URLStr())
//do something with resp and err

// get the last request that the server received
recv := srv.AcceptN(1, 1 * time.Second)

Possible Uses

Since StartServer takes in any http.Handler it's fairly flexible. Possible applications:

  • Testing your own handlers. For example, in situations where httptest.ResponseRecorder doesn't meet your needs
  • Testing your code that makes its own HTTP requests (for example, an external API call)

Development

To run tests, you can run the following if you have the Go toolchain installed.

go test ./...

But if you have Docker and Docker Compose, you don't need to do that. You can run:

docker-compose -p testsrv up --exit-code-from test --abort-on-container-exit test

If you do the docker-compose command, make sure to clean up afterwards:

docker-compose -p testsrv down

Documentation

Index

Constants

View Source
const (
	UUIDHeaderName = "HTTP-Sub-UUID"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ReceivedRequest

type ReceivedRequest struct {
	// The request the subscriber received
	Request *http.Request
	// The receipt time
	Time time.Time
	// Unique identifier of the request. will be returned as UUIDHeaderName in each
	// response that the subscriber sends
	UUID string
}

ReceivedRequest represents a request that the subscriber received.

type Server

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

Server is a HTTP server that you can send real HTTP requests to in your unit tests. Each request is handled by a http.Handler that you specify, and the server records each request after your handler finishes execution. You can later retreive details about each received request

func StartServer

func StartServer(handler http.Handler) *Server

StartServer starts the server listening in the background. All requests sent to the server at s.URLStr() are sent to handler, flushed and then recorded for later retrieval using the AcceptN func

func (*Server) AcceptN

func (s *Server) AcceptN(n int, maxWait time.Duration) []*ReceivedRequest

AcceptN consumes and returns up to n requests that were sent to the server before maxWait is up. returns all of the requests received. each individual request returned will not be returned by this func again.

func (*Server) Close

func (s *Server) Close()

Close releases all resources on this subscriber

func (*Server) URLStr

func (s *Server) URLStr() string

URLStr returns the string representation of the URL to call to hit this server

Jump to

Keyboard shortcuts

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