forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxoonirun_test.go
167 lines (144 loc) · 4.79 KB
/
xoonirun_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
162
163
164
165
166
167
package oonimkall_test
import (
"encoding/json"
"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"
)
func TestOONIRunFetch(t *testing.T) {
t.Run("we can fetch a OONI Run link descriptor", func(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
rawResp, err := sess.OONIRunFetch(sess.NewContext(), 9408643002)
if err != nil {
t.Fatal(err)
}
expect := map[string]any{
"archived": false,
"descriptor_creation_time": "2023-07-18T15:38:21Z",
"descriptor": map[string]any{
"author": "[email protected]",
"description": "We use this OONI Run descriptor for writing integration tests for ooni/probe-cli/v3/pkg/oonimkall.",
"description_intl": map[string]any{},
"icon": "",
"name": "OONIMkAll Integration Testing",
"name_intl": map[string]any{},
"nettests": []any{
map[string]any{
"backend_options": map[string]any{},
"inputs": []any{string("https://www.example.com/")},
"is_background_run_enabled": false,
"is_manual_run_enabled": false,
"options": map[string]any{},
"test_name": "web_connectivity",
},
},
"short_description": "Integration testing descriptor for ooni/probe-cli/v3/pkg/oonimkall.",
"short_description_intl": map[string]any{},
},
"mine": false,
"translation_creation_time": "2023-07-18T15:38:21Z",
"v": 1.0,
}
var got map[string]any
runtimex.Try0(json.Unmarshal([]byte(rawResp), &got))
t.Log(got)
if diff := cmp.Diff(expect, got); 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)
}
URL := &url.URL{Host: "\t"} // this URL is invalid
rawResp, err := sess.OONIRunFetchWithURL(sess.NewContext(), URL)
if !strings.HasSuffix(err.Error(), `invalid URL escape "%09"`) {
t.Fatal("unexpected error", err)
}
if rawResp != "" {
t.Fatal("expected empty raw 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()
URL := runtimex.Try1(url.Parse(server.URL))
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
rawResp, err := sess.OONIRunFetchWithURL(sess.NewContext(), URL)
if !strings.HasSuffix(err.Error(), "HTTP request failed") {
t.Fatal("unexpected error", err)
}
if rawResp != "" {
t.Fatal("expected empty raw 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: "/",
}
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
rawResp, err := sess.OONIRunFetchWithURL(sess.NewContext(), URL)
if !strings.HasSuffix(err.Error(), "connection_reset") {
t.Fatal("unexpected error", err)
}
if rawResp != "" {
t.Fatal("expected empty raw 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()
URL := runtimex.Try1(url.Parse(server.URL))
sess, err := NewSessionForTesting()
if err != nil {
t.Fatal(err)
}
rawResp, err := sess.OONIRunFetchWithURL(sess.NewContext(), URL)
if !strings.HasSuffix(err.Error(), "connection_reset") {
t.Fatal("unexpected error", err)
}
if rawResp != "" {
t.Fatal("expected empty raw response")
}
})
}