Documentation ¶
Overview ¶
Package testutils contains utilties for mocking various kinds of network interfaces and asserting networked events did or did not happen
Index ¶
- func AssertConnected(host string, t *testing.T)
- func AssertJSONEquals(a, b []byte, t *testing.T)
- func GetFastURLResponse(uri []byte, responseHandler func(resp *fasthttp.Response)) (res []byte, err error)
- func GetFreePort(t *testing.T) int
- func GetFreePorts(count int, t *testing.T) []int
- func GetGitRoot(t *testing.T) string
- func GetUnFreePort() (port int, err error)
- func GetUnfreePort(t *testing.T) (port int)
- func GetUnfreePorts(count int, t *testing.T) (unfreePorts []int)
- func MockFile(t *testing.T) string
- func MockHTTPServer(port int) error
- func MockHandler() http.HandlerFunc
- func PortIsAvailable(port int) bool
- func WaitForConnect(host string) bool
- func WaitForConnectTimeout(host string, timeout time.Duration) bool
- func WrapHandler(handler http.Handler) httpmock.Responder
- type FastRouter
- func (r FastRouter) ANY(path string, handler http.HandlerFunc)
- func (r FastRouter) AnyFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) CONNECT(path string, handler http.HandlerFunc)
- func (r FastRouter) ConnectFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) DELETE(path string, handler http.HandlerFunc)
- func (r FastRouter) DeleteFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) GET(path string, handler http.HandlerFunc)
- func (r FastRouter) GETFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) Handle(method, path string, handler http.HandlerFunc)
- func (r FastRouter) HandleFastHTTP(method, path string, handler fasthttp.RequestHandler)
- func (r *FastRouter) Handler() fasthttp.RequestHandler
- func (r FastRouter) OPTIONS(path string, handler http.HandlerFunc)
- func (r FastRouter) OptionsFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) PATCH(path string, handler http.HandlerFunc)
- func (r FastRouter) POST(path string, handler http.HandlerFunc)
- func (r FastRouter) PUT(path string, handler http.HandlerFunc)
- func (r FastRouter) PatchFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) PostFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) PutFastHTTP(path string, handler fasthttp.RequestHandler)
- func (r FastRouter) TRACE(path string, handler http.HandlerFunc)
- func (r FastRouter) TraceFastHTTP(path string, handler fasthttp.RequestHandler)
- type HTTPFastHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertConnected ¶
AssertConnected tries to connect to a given host several times fails if unable to connect
func AssertJSONEquals ¶ added in v0.25.0
AssertJSONEquals makes sure two json byte slices are equal Note: Does not currently work on arrays
func GetFastURLResponse ¶ added in v0.32.0
func GetFastURLResponse(uri []byte, responseHandler func(resp *fasthttp.Response)) (res []byte, err error)
GetFastURLResponse gets a fasthttp url with standard headers allows a user to run a function against the response before it is released TODO make this use the fasthttp library
func GetFreePort ¶
GetFreePort returns a freeport, throw error if not available
func GetFreePorts ¶
GetFreePorts returns a freeport, throw error into test object if not available
func GetGitRoot ¶ added in v0.36.0
GetGitRoot gets the root directory of the current package this is useful for testing file generation, especially for automated swagger-like documentation
func GetUnFreePort ¶
GetUnFreePort gets a port and start an http server on it to mock a taken port
func GetUnfreePort ¶
GetUnfreePort gets a free port and start an http server on it so its taken.
func GetUnfreePorts ¶ added in v0.8.0
GetUnfreePorts gets a list of ports that are taken
func MockHTTPServer ¶ added in v0.25.0
MockHTTPServer runs a mock http server on a given port
Example ¶
package main import ( "fmt" "net/http" "strconv" "github.com/xplorfin/netutils/testutils" "github.com/jarcoal/httpmock" ) func main() { // turn on http mocking httpmock.Activate() defer httpmock.Deactivate() requestCount := 0 testServer := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { requestCount++ rw.WriteHeader(200) _, _ = rw.Write([]byte(strconv.Itoa(requestCount))) }) httpmock.RegisterResponder("GET", "https://requestcounter.com", testutils.WrapHandler(testServer)) resp, err := http.Get("https://requestcounter.com") if err != nil { panic(err) } // print the response count, should be 1 fmt.Print(resp) }
Output:
func MockHandler ¶
func MockHandler() http.HandlerFunc
MockHandler mocks an http handler that's not nil
func PortIsAvailable ¶
PortIsAvailable will determine if a port is available
func WaitForConnect ¶
WaitForConnect on a port progressively backing off returns false if we couldn't establish a connection uses default timeout of 5 seconds deprecated: use WaitForConnect in main package
func WaitForConnectTimeout ¶ added in v0.8.0
WaitForConnectTimeout will wait for a connection on a port progressively backing off returns false if we couldn't establish a connection by timeout after 10 timeouts deprecated: use WaitForConnectTimeout in main package
Types ¶
type FastRouter ¶ added in v0.25.0
FastRouter creates a fasthttp router that is compatible with standard http.Handler's in golang
func NewRouter ¶
func NewRouter() *FastRouter
NewRouter allows you to wrap a fast http server around regular http.handler funcs
func (FastRouter) ANY ¶ added in v0.25.0
func (r FastRouter) ANY(path string, handler http.HandlerFunc)
ANY returns a request through the FastRouter.Router handler
func (FastRouter) AnyFastHTTP ¶ added in v0.25.0
func (r FastRouter) AnyFastHTTP(path string, handler fasthttp.RequestHandler)
AnyFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) CONNECT ¶ added in v0.25.0
func (r FastRouter) CONNECT(path string, handler http.HandlerFunc)
CONNECT returns a request through the FastRouter.Router handler
func (FastRouter) ConnectFastHTTP ¶ added in v0.25.0
func (r FastRouter) ConnectFastHTTP(path string, handler fasthttp.RequestHandler)
ConnectFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) DELETE ¶ added in v0.25.0
func (r FastRouter) DELETE(path string, handler http.HandlerFunc)
DELETE returns a request through the FastRouter.Router handler
func (FastRouter) DeleteFastHTTP ¶ added in v0.25.0
func (r FastRouter) DeleteFastHTTP(path string, handler fasthttp.RequestHandler)
DeleteFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) GET ¶ added in v0.25.0
func (r FastRouter) GET(path string, handler http.HandlerFunc)
GET returns a request through the FastRouter.Router handler
func (FastRouter) GETFastHTTP ¶ added in v0.25.0
func (r FastRouter) GETFastHTTP(path string, handler fasthttp.RequestHandler)
GETFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) Handle ¶ added in v0.25.0
func (r FastRouter) Handle(method, path string, handler http.HandlerFunc)
Handle returns a request through the FastRouter.Router handler
func (FastRouter) HandleFastHTTP ¶ added in v0.25.0
func (r FastRouter) HandleFastHTTP(method, path string, handler fasthttp.RequestHandler)
HandleFastHTTP returns a request through the FastRouter.Router handler
func (*FastRouter) Handler ¶ added in v0.25.0
func (r *FastRouter) Handler() fasthttp.RequestHandler
Handler returns the fasthttp.RequestHandler object for use in a server/with a mock client
func (FastRouter) OPTIONS ¶ added in v0.25.0
func (r FastRouter) OPTIONS(path string, handler http.HandlerFunc)
OPTIONS returns a request through the FastRouter.Router handler
func (FastRouter) OptionsFastHTTP ¶ added in v0.25.0
func (r FastRouter) OptionsFastHTTP(path string, handler fasthttp.RequestHandler)
OptionsFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) PATCH ¶ added in v0.25.0
func (r FastRouter) PATCH(path string, handler http.HandlerFunc)
PATCH returns a request through the FastRouter.Router handler
func (FastRouter) POST ¶ added in v0.25.0
func (r FastRouter) POST(path string, handler http.HandlerFunc)
POST returns a request through the FastRouter.Router handler
func (FastRouter) PUT ¶ added in v0.25.0
func (r FastRouter) PUT(path string, handler http.HandlerFunc)
PUT returns a request through the FastRouter.Router handler
func (FastRouter) PatchFastHTTP ¶ added in v0.25.0
func (r FastRouter) PatchFastHTTP(path string, handler fasthttp.RequestHandler)
PatchFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) PostFastHTTP ¶ added in v0.25.0
func (r FastRouter) PostFastHTTP(path string, handler fasthttp.RequestHandler)
PostFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) PutFastHTTP ¶ added in v0.25.0
func (r FastRouter) PutFastHTTP(path string, handler fasthttp.RequestHandler)
PutFastHTTP returns a request through the FastRouter.Router handler
func (FastRouter) TRACE ¶ added in v0.25.0
func (r FastRouter) TRACE(path string, handler http.HandlerFunc)
TRACE returns a request through the FastRouter.Router handler
func (FastRouter) TraceFastHTTP ¶ added in v0.25.0
func (r FastRouter) TraceFastHTTP(path string, handler fasthttp.RequestHandler)
TraceFastHTTP returns a request through the FastRouter.Router handler
type HTTPFastHandler ¶ added in v0.25.0
type HTTPFastHandler struct { // Listener is the server object Listener *fasthttputil.InmemoryListener // Test is the test object (for throwing errors) Test *testing.T }
HTTPFastHandler - turns client requests into methods locally handled by overriding dial for wrapping gock/other mock servers
func NewFastHTTPMock ¶ added in v0.25.0
func NewFastHTTPMock(t *testing.T) *HTTPFastHandler
NewFastHTTPMock creates a mock server by overriding client this allows us to test fathttp servers without actually standing up a server
func (HTTPFastHandler) Dial ¶ added in v0.25.0
func (server HTTPFastHandler) Dial() (net.Conn, error)
Dial creates a dial object corresponding to mock server
func (HTTPFastHandler) FastHTTPMockClient ¶ added in v0.25.0
func (server HTTPFastHandler) FastHTTPMockClient() *fasthttp.Client
FastHTTPMockClient creates a fasthttp client with the server name note: this will override every request with dial
func (HTTPFastHandler) HTTPMockClient ¶ added in v0.25.0
func (server HTTPFastHandler) HTTPMockClient() http.Client
HTTPMockClient creates an http client with the server name Note: this will override every request with dial
func (HTTPFastHandler) Start ¶ added in v0.25.0
func (server HTTPFastHandler) Start(handler fasthttp.RequestHandler)
Start starts the fastHTTP server and routes request to a given handler