package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/h2non/gock"
)
func main() {
defer gock.Off()
defer gock.DisableNetworking()
gock.EnableNetworking()
gock.New("http://httpbin.org").
Get("/get").
Reply(201).
SetHeader("Server", "gock")
res, err := http.Get("http://httpbin.org/get")
if err != nil {
fmt.Errorf("Error: %s", err)
}
// The response status comes from the mock
fmt.Printf("Status: %d\n", res.StatusCode)
// The server header comes from mock as well
fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
// Response body is the original
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Body: %s", string(body))
}
The response I get is
➜ test go run gock.go
Status: 201
Server header: gunicorn/19.9.0
Body: {
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1",
"X-Amzn-Trace-Id": "Root=1-637bb59b-7debf317641b7b092a0d1887"
},
"origin": "64.94.4.36",
"url": "http://httpbin.org/get"
}
Note the server header is gunicorn/19.9.0 instead of gock
Using the example in the README:
The response I get is
Note the server header is
gunicorn/19.9.0
instead ofgock