Documentation ¶
Index ¶
- func ExecuteAuthorizedRequest(request *http.Request, router httpServer, token string) (responseRecorder *httptest.ResponseRecorder)
- func ExecuteRequest(request *http.Request, router httpServer) (responseRecorder *httptest.ResponseRecorder)
- func ResponseCode(t *testing.T, expected, actual int)
- type EndpointTester
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 ¶
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: