r2test

package
v1.20220411.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package r2test provides helpers for writing tests involving calls with sdk/r2.

The most common example is to add a mock response as an option to a default set of options.

Lets say we have a wrapping helper client:

type APIClient struct {
	RemoteURL string
	...
	Defaults []r2.Option
}

func (a APIClient) GetFoos(ctx context.Context) (output []Foo, err error) {
	_, err = r2.New(a.RemoteURL, append(a.Defaults, r2.OptContext(ctx))...).JSON(&output)
	return
}

During tests we can add a default option:

mockedResponse := `[{"is":"a foo"}]`
a := APIClient{ Remote: "http://test.invalid", Defaults: []r2.Option{r2test.OptMockResponseString(mockedResponse)} }
foos, err := a.GetFoos(context.TODO())
...

We will now return the mocked response instead of reaching out to the remote for the call.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptMockResponse

func OptMockResponse(handler http.Handler) r2.Option

OptMockResponse mocks a response by creating an httptest server.

func OptMockResponseString

func OptMockResponseString(response string) r2.Option

OptMockResponseString mocks a string response.

func OptMockResponseStringStatus

func OptMockResponseStringStatus(statusCode int, response string) r2.Option

OptMockResponseStringStatus mocks a string response with a given status code.

Types

This section is empty.

Jump to

Keyboard shortcuts

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