caller

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

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

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

README

HTTP Test Caller

This project is a wrapper for testing HTTP handler, it will pass the request to the handler and record the response using httptest.ResponseRecorder. If the response type is set, it will try to unmarshal the response body to the type.

Usage

Importing
import "github.com/AccelByte/http-test-caller"
Make a request
resp, body, err :=
    caller.Call(handler).
        To(gorequest.New().    // using gorequest to construct the request
            Post("/example").  // we are testing handler locally, doesn't need full URL
            Type(restful.MIME_JSON).
            SendString(`{"Example": "test"}`).
            MakeRequest()).
        Read(&models.ExampleResponse{}).
        Execute()
    
// we can assert the resp.Code & unmarshaled body

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPTestCaller

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

HTTPTestCaller is a wrapper for testing HTTP handler, it will pass the request to the handler and record the response using httptest.ResponseRecorder. If the response type is set, it will try to unmarshal the response body to the type

func Call

func Call(handler http.Handler) *HTTPTestCaller

Call creates the caller with handler specified, use this function to start the caller

func (*HTTPTestCaller) Execute

func (caller *HTTPTestCaller) Execute() (*httptest.ResponseRecorder, interface{}, error)

Execute calls the handler with the request and records the response in httptest.ResponseRecorder. If the expected response type exists, it will try to unmarshal the response body to the type

func (*HTTPTestCaller) Read

func (caller *HTTPTestCaller) Read(response interface{}) *HTTPTestCaller

Read specifies the expected type of response returned by the handler being tested

func (*HTTPTestCaller) To

func (caller *HTTPTestCaller) To(request *http.Request, _ ...interface{}) *HTTPTestCaller

To receives a request to be sent to the handler. The second parameter is a convenience if you want to pass directly from http.NewRequest() or gorequest.MakeRequest()

Jump to

Keyboard shortcuts

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