File: transport_test.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 (53 lines) | stat: -rw-r--r-- 1,149 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
package gock

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/nbio/st"
)

func TestTransportMatch(t *testing.T) {
	defer after()
	const uri = "http://foo.com"
	New(uri).Reply(204)
	u, _ := url.Parse(uri)
	req := &http.Request{URL: u}
	res, err := NewTransport().RoundTrip(req)
	st.Expect(t, err, nil)
	st.Expect(t, res.StatusCode, 204)
	st.Expect(t, res.Request, req)
}

func TestTransportCannotMatch(t *testing.T) {
	defer after()
	New("http://foo.com").Reply(204)
	u, _ := url.Parse("http://127.0.0.1:1234")
	req := &http.Request{URL: u}
	_, err := NewTransport().RoundTrip(req)
	st.Expect(t, err, ErrCannotMatch)
}

func TestTransportNotIntercepting(t *testing.T) {
	t.Skip("Flaky")
	defer after()

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, world")
	}))
	defer ts.Close()

	New(ts.URL).Reply(200)
	Disable()

	u, _ := url.Parse(ts.URL)
	req := &http.Request{URL: u, Header: make(http.Header)}

	res, err := NewTransport().RoundTrip(req)
	st.Expect(t, err, nil)
	st.Expect(t, Intercepting(), false)
	st.Expect(t, res.StatusCode, 200)
}