File: networking.go

package info (click to toggle)
golang-gopkg-h2non-gock.v1 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 364 kB
  • sloc: makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// This example shows how to enable the networking for a request to a local server
// and mock a second request to a remote server.
package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httptest"

	"gopkg.in/h2non/gock.v1"
)

// Starts a local HTTP server in background
func startHTTPServer() *httptest.Server {
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("Local server received a GET request")

		res, err := http.Get("http://httpbin.org/nope")
		if err != nil {
			msg := fmt.Sprintf("Error from request to httpbin: %s", err)
			http.Error(w, msg, http.StatusInternalServerError)
			return
		}

		body, _ := ioutil.ReadAll(res.Body)
		// MUST NOT get original body since the networking
		// wasn't enabled for this request
		fmt.Printf("Body From httpbin: %s\n", string(body))
		fmt.Printf("Status From httpbin: %s\n", res.Status)

		io.WriteString(w, "Local Response="+res.Header.Get("Server"))
	}))
}

func main() {
	defer gock.Disable()
	defer gock.DisableNetworking()

	srv := startHTTPServer()
	defer srv.Close()

	// Register our local server
	gock.New(srv.URL).
		EnableNetworking()

	gock.New("http://httpbin.org").
		Get("/nope").
		Reply(201).
		SetHeader("Server", "gock")

	res, err := http.Get(srv.URL)
	if err != nil {
		fmt.Printf("Error from request to localhost: %s", err)
		return
	}

	// 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"))
	// MUST get original response since the networking was enabled for this request
	body, _ := ioutil.ReadAll(res.Body)
	fmt.Printf("Body From Local Server: %s", string(body))
}