httpcheck

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteAuthorizedRequest

func ExecuteAuthorizedRequest(request *http.Request, router httpServer, token string) (responseRecorder *httptest.ResponseRecorder)

ExecuteAuthorizedRequest makes an http request with the router provided and returns the responseRecorder but will append an authorization token beforehand

func ExecuteRequest

func ExecuteRequest(request *http.Request, router httpServer) (responseRecorder *httptest.ResponseRecorder)

ExecuteRequest makes an http request with the router provided and returns the response

func ResponseCode

func ResponseCode(t *testing.T, expected, actual int)

ResponseCode asserts both codes should be equal, otherwise fails the test

Types

type EndpointTester

type EndpointTester struct {
	Token    *string
	Response *httptest.ResponseRecorder
	// contains filtered or unexported fields
}

EndpointTester will test an endpoint

func NewAuthEndpointTester

func NewAuthEndpointTester(method string, url string, router httpServer, token string) *EndpointTester

NewAuthEndpointTester is the same as NewEndpointTester but sets an authentication token

func NewEndpointTester

func NewEndpointTester(method string, url string, router httpServer) (response *EndpointTester)

NewEndpointTester creates a new endpoint tester object receiving a method and a url, this object's purpose is to just provide a constant wrap around checking response codes and executing requests to the same url with the same mmethod

func (*EndpointTester) ChangeEndpoint

func (et *EndpointTester) ChangeEndpoint(method string, url string)

ChangeEndpoint will change the method and url up for testing

func (*EndpointTester) Test

func (et *EndpointTester) Test(exp int, payload []byte) func(*testing.T)

Test will execute a request on the url with the given method and return a test function to be used in a subtest to describe it's functionality

Example
package main

import (
	"net/http"
	"testing"

	"github.com/jramonrod/go-test/httpcheck"
)

type fakeRouter struct{}

func (fr fakeRouter) ServeHTTP(http.ResponseWriter, *http.Request) {}
func main() {
	// this would be your testing function
	func(t *testing.T) {
		googleGetter := httpcheck.NewEndpointTester("GET", "google.com", fakeRouter{})
		t.Run("this tests an empty payload to a google.com with a GET request", googleGetter.Test(323, nil))
		t.Run("this is another option", httpcheck.NewEndpointTester("GET", "google.com", fakeRouter{}).Test(http.StatusAccepted, nil))
		httpcheck.NewEndpointTester("PUT", "myspace.com", fakeRouter{}).Test(http.StatusAccepted, nil)(t)
	}(&testing.T{})
}
Output:

Jump to

Keyboard shortcuts

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