-
Notifications
You must be signed in to change notification settings - Fork 51
/
http_test.go
47 lines (42 loc) · 1.26 KB
/
http_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
package netemx
import (
"net/http"
"testing"
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/netem"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
func TestHTTPCleartextServerFactory(t *testing.T) {
env := MustNewQAEnv(
QAEnvOptionNetStack(AddressWwwExampleCom, &HTTPCleartextServerFactory{
Factory: HTTPHandlerFactoryFunc(func(env NetStackServerFactoryEnv, stack *netem.UNetStack) http.Handler {
return ExampleWebPageHandler()
}),
Ports: []int{80},
}),
)
defer env.Close()
env.AddRecordToAllResolvers("www.example.com", "", AddressWwwExampleCom)
env.Do(func() {
// TODO(https://github.com/ooni/probe/issues/2534): NewHTTPClientStdlib has QUIRKS but they're not needed here
client := netxlite.NewHTTPClientStdlib(log.Log)
req := runtimex.Try1(http.NewRequest("GET", "http://www.example.com/", nil))
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatal("unexpected StatusCode", resp.StatusCode)
}
data, err := netxlite.ReadAllContext(req.Context(), resp.Body)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(ExampleWebPage, string(data)); diff != "" {
t.Fatal(diff)
}
})
}