-
Notifications
You must be signed in to change notification settings - Fork 54
/
httpx_test.go
161 lines (137 loc) · 4.15 KB
/
httpx_test.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package oonimkall_test
import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/testingx"
"github.com/ooni/probe-cli/v3/pkg/oonimkall"
)
func TestSessionHTTPDo(t *testing.T) {
t.Run("on success", func(t *testing.T) {
// Implementation note: because we need to backport this patch to the release/3.18
// branch, it would be quite verbose and burdensome use netem to implement this test,
// since release/3.18 is lagging behind from master in terms of netemx.
const expectedResponseBody = "Hello, World!\r\n"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(expectedResponseBody))
}))
defer server.Close()
req := &oonimkall.HTTPRequest{
Method: "GET",
Url: server.URL,
}
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
resp, err := sess.HTTPDo(sess.NewContext(), req)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expectedResponseBody, resp.Body); diff != "" {
t.Fatal(diff)
}
})
t.Run("we handle the case where the URL is invalid", func(t *testing.T) {
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
req := &oonimkall.HTTPRequest{
Method: "GET",
Url: "\t", // this URL is invalid
}
resp, err := sess.HTTPDo(sess.NewContext(), req)
if !strings.HasSuffix(err.Error(), `invalid control character in URL`) {
t.Fatal("unexpected error", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
})
t.Run("we handle the case where the response body is not 200", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer server.Close()
req := &oonimkall.HTTPRequest{
Method: "GET",
Url: server.URL,
}
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
resp, err := sess.HTTPDo(sess.NewContext(), req)
if !strings.HasSuffix(err.Error(), "HTTP request failed") {
t.Fatal("unexpected error", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
})
t.Run("we handle the case where the HTTP round trip fails", func(t *testing.T) {
// Implementation note: because we need to backport this patch to the release/3.18
// branch, it would be quite verbose and burdensome use netem to implement this test,
// since release/3.18 is lagging behind from master in terms of netemx.
server := testingx.MustNewTLSServer(testingx.TLSHandlerReset())
defer server.Close()
URL := &url.URL{
Scheme: "https",
Host: server.Endpoint(),
Path: "/",
}
req := &oonimkall.HTTPRequest{
Method: "GET",
Url: URL.String(),
}
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
resp, err := sess.HTTPDo(sess.NewContext(), req)
if !strings.HasSuffix(err.Error(), "connection_reset") {
t.Fatal("unexpected error", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
})
t.Run("we handle the case when reading the response body fails", func(t *testing.T) {
// Implementation note: because we need to backport this patch to the release/3.18
// branch, it would be quite verbose and burdensome use netem to implement this test,
// since release/3.18 is lagging behind from master in terms of netemx.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{"))
hijacker := w.(http.Hijacker)
conn, _, err := hijacker.Hijack()
runtimex.PanicOnError(err, "hijacker.Hijack failed")
if tc, ok := conn.(*net.TCPConn); ok {
tc.SetLinger(0)
}
conn.Close()
}))
defer server.Close()
req := &oonimkall.HTTPRequest{
Method: "GET",
Url: server.URL,
}
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
resp, err := sess.HTTPDo(sess.NewContext(), req)
if !strings.HasSuffix(err.Error(), "connection_reset") {
t.Fatal("unexpected error", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
})
}