Documentation ¶
Overview ¶
Helper package to add more functions to httpmock (github.com/jarcoal/httpmock)
Features: - RegisterQueryMapResponder form query based based on a QueryMap - ActivateRecorder to record HTTP requests during the development of tests and examples
Example ¶
package main import ( "bytes" "fmt" "github.com/NETWAYS/go-check/http/mock" "github.com/jarcoal/httpmock" "io/ioutil" "net/http" ) func main() { // Activate httpmock as normal httpmock.Activate() defer httpmock.DeactivateAndReset() // Use any normal responder httpmock.RegisterResponder("GET", "https://example.com/test.json", func(request *http.Request) (*http.Response, error) { return httpmock.NewStringResponse(200, `{"allgood":true}`), nil }) req, _ := http.NewRequest("GET", "https://example.com/test.json", nil) //nolint:noctx requestAndDump(req) // Use additional responders checkhttpmock.RegisterQueryMapResponder("POST", "https://exampleapi.com/", checkhttpmock.QueryMap{ "test=1": "test.json", }) req, _ = http.NewRequest("POST", "https://exampleapi.com/", bytes.NewBufferString("test=1")) //nolint:noctx req.Header.Set("Content-Type", "application/x-www-form-urlencoded") requestAndDump(req) } func requestAndDump(req *http.Request) { resp, err := http.DefaultTransport.RoundTrip(req) if err != nil { panic(err) } defer resp.Body.Close() data, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(data)) }
Output: {"allgood":true} {"example":true}
Index ¶
Examples ¶
Constants ¶
const TestData = "./testdata"
Where response data is stored, relative to the package being tested
Variables ¶
This section is empty.
Functions ¶
func NewQueryMapResponder ¶
Return a responder function for httpmock, to return different results based on a QueryMap
Queries from the QueryMap are matched partially and the response is read from `./testdata`
func RegisterQueryMapResponder ¶
Register a NewQueryMapResponder with httpmock
See QueryMap and NewQueryMapResponder
Types ¶
type QueryMap ¶
Mapping a partial form request to a response
The response will be expected and read from the local testdata directory of your package.
QueryMap{ "test=1": "response.json", }
type Record ¶
Data structure to store information about a http.Request and http.Response in a simplified way
type Recorder ¶
Helper to record http.Response and http.Request for httpmock when no responser was found
func ActivateRecorder ¶
func ActivateRecorder() (rec *Recorder)
Activate a Recorder and set it as noResponder with httpmock
Usually you would use this during developing unit tests, and not for finished tests.
By default records to RecordFile
Example ¶
package main import ( "fmt" "github.com/NETWAYS/go-check/http/mock" "github.com/jarcoal/httpmock" "io" "io/ioutil" "net/http" "os" ) func main() { // Activate the normal httpmock httpmock.Activate() defer httpmock.DeactivateAndReset() // Activate recorder _ = os.Remove(checkhttpmock.RecordFile) // Remove any prior recording checkhttpmock.ActivateRecorder() // We don't set any mock examples here //httpmock.RegisterResponder("GET", "http://localhost:8080/test", // func(request *http.Request) (*http.Response, error) { // return httpmock.NewStringResponse(200, "Hello World"), nil // }) // Start a simple HTTP server runHTTP() // Do any HTTP request resp, err := http.Get("http://localhost:64888/test") // nolint:noctx if err != nil { fmt.Println(err) os.Exit(1) } // Print response body data, _ := ioutil.ReadAll(resp.Body) fmt.Printf("%s\n", data) // Print recording data, _ = ioutil.ReadFile(checkhttpmock.RecordFile) fmt.Printf("%s\n", data) _ = resp.Body.Close() } func runHTTP() { http.HandleFunc("/test", func(w http.ResponseWriter, req *http.Request) { _, _ = io.WriteString(w, `Hello World`) }) go http.ListenAndServe(":64888", nil) //nolint:errcheck }
Output: Hello World --- url: http://localhost:64888/test method: GET query: "" status: 200 OK body: Hello World