testutil

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Test Utilities

General utilities useful in tests:

  • Approximate comparisons of float64 numbers and slices;
  • Creating a parsed JSON object from a string;
  • TestServer implementation for testing HTTP calls.

Documentation

Overview

Package testutil implements generally useful test utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileExists added in v0.2.0

func FileExists(fileName string) bool

FileExists returns true if a file with the given path exists, and it is indeed a file (not a directory).

func JSON

func JSON(js string) interface{}

JSON parses js as a JSON string into the default encoding/json data structures: maps, strings, numbers, etc. This is useful e.g. for custom JSON readers.

func ReadFile added in v0.2.0

func ReadFile(fileName string) string

ReadFile returns the contents of the file as a string. If the file doesn't exist, or it is not a file, this method panics. It is a good practice to test that FileExists() is true, then call this method.

func Round

func Round(x float64, places int) float64

Round x to the given number of significant decimal digits, for approximate comparisons in tests.

func RoundFixed

func RoundFixed(x float64, digits int) float64

RoundFixed rounds x to the fixed number of decimal places. This is useful for rounding around zero, since it has no well-defined number of significant digits.

func RoundFixedSlice

func RoundFixedSlice(s []float64, places int) []float64

RoundFixedSlice rounds the elements of the slice to the given number of decimal places, for approximate comparisons in tests. This is useful when numbers may be near zero, and therefore have no well-defined number of significant digits.

func RoundSlice

func RoundSlice(s []float64, places int) []float64

RoundSlice rounds the elements of the slice to the given number of significant decimal digits, for approximate comparisons in tests.

func WriteFile added in v0.2.0

func WriteFile(fileName, text string) error

WriteFile creates and writes a file with the given content.

Types

type TestServer

type TestServer struct {
	ResponseStatusMap map[string][]int    // URL path -> status code sequence
	ResponseBodyMap   map[string][]string // URL path -> response body sequence
	ResponseStatus    []int               // default status codes sequence
	ResponseBody      []string            // default response body sequnece
	RequestPath       string              // path in the request URL
	RequestQuery      url.Values          // query received by the server in the request
	Flushed           bool                // whether the body write was flushed
	BodyWriteBytes    int                 // number of body bytes written
	BodyWriteError    error               // error value from writing body
	Server            *httptest.Server
}

TestServer is the handle for a test server. The server returns the status code and response body from the matching sequence, and then repeats the last one in the sequence.

Note, that this creates a real HTTP server running locally, and the client fetches real URLs from the network. Always use the base URL provided by URL() method to avoid calling random servers on the Internet in your tests.

Tip: pay attention to the status codes when setting a response body: some response codes do not allow a response body. Always check for BodyWriteError in your tests to catch this problem.

func NewTestServer

func NewTestServer() *TestServer

NewTestServer creates and starts a new test server.

func (*TestServer) Client

func (h *TestServer) Client() *http.Client

Client is the test server's HTTP client to be used in tests.

func (*TestServer) Close

func (h *TestServer) Close()

Close the handle's test server.

func (*TestServer) URL

func (h *TestServer) URL() string

URL returns the base test server URL.

Jump to

Keyboard shortcuts

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