Categorygithub.com/xplorfin/netutils
modulepackage
0.63.0
Repository: https://github.com/xplorfin/netutils.git
Documentation: pkg.go.dev

# README

Coverage Status Renovate enabled Build status Build status Go Report Card

Netutils

This is a series of networking utilities and test wrappers by entropy for building robust networked services in golang. See the godoc for details

What can I do with this?

The godoc should cover most of it. I've highlighted a few things below and will add more examples as time goes on. Examples are also present in the godoc

Mocking:

One thing peculiarity of the httmock library is you can't actually pass it a handler. WrapHandler let's you do so:

Handler Mocking:

(see mock_test for a more detailed example)

   package main
   import (
   	"github.com/jarcoal/httpmock"
   )

       func main(){
       httpmock.Activate()
       defer httpmock.Deactivate()
       ctx := context.Background()
       requestCount := 0
       testServer := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
           rw.WriteHeader(200)
       })
   
       httpmock.RegisterResponder("POST", "https://api.github.com/graphql", testutils.WrapHandler(testServer))
}   

There's also a fasthttp module for mocking fasthttp servers/clients with http mock (see tests)

# Packages

Package fasthttp contains fasthttp specific utilities for handling compression, authorization, and more.
Package testutils contains utilties for mocking various kinds of network interfaces and asserting networked events did or did not happen.

# Functions

NewFreeportStack is a helper method to initialize a freeport stack.
WaitForConnect on a port progressively backing off returns false if we couldn't establish a connection uses default timeout of 5 seconds.
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.

# Structs

FreePortStack stack allows you to locally store a list of autogenerated ports it is stateful to make sure ports aren't reused.