-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_it_test.go
125 lines (110 loc) · 3.3 KB
/
fetch_it_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
package fetch
import (
"context"
"errors"
"net/http"
"testing"
"time"
)
func TestRequestIntegration(t *testing.T) {
mock = false
defer func() { mock = true }()
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "noone.com")
w.WriteHeader(303)
_, err := w.Write([]byte("wrong neighborhood"))
if err != nil {
panic(err)
}
})
mux.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.WriteHeader(200)
w.Write([]byte(`{"name":"Lola"}`))
} else {
w.WriteHeader(405)
w.Write([]byte(`{"message":"get out"}`))
}
})
server := &http.Server{Addr: ":7349", Handler: mux}
go server.ListenAndServe()
defer server.Shutdown(context.Background())
time.Sleep(time.Millisecond)
_, err := Get[string]("localhost:7349/hello")
if err == nil {
t.Fatalf("expected 303 status error")
}
if err.(*Error).Status != 303 {
t.Fatalf("expected 303 status, got=%d", err.(*Error).Status)
}
if err.(*Error).Headers["Access-Control-Allow-Origin"] != "noone.com" {
t.Fatalf("expected header, got=%s", err.(*Error).Headers["Access-Control-Allow-Origin"])
}
if err.Error() != "http response with status=303, body: wrong neighborhood" {
t.Fatalf("wrong error message, got=%s", err)
}
type Pet struct {
Name string
}
p, err := Get[Pet]("localhost:7349/get")
if err != nil {
t.Fatal(err)
}
if p.Name != "Lola" {
t.Errorf("unexpected name, got=%s", p.Name)
}
_, err = Post[Pet]("localhost:7349/get", "i'm post")
if err.Error() != `http response with status=405, body: {"message":"get out"}` {
t.Errorf("expected 405 status error, got=%s", err)
}
}
func TestIssue1(t *testing.T) {
mock = false
defer func() { mock = true }()
mux := http.NewServeMux()
mux.HandleFunc("/v3/sessions", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write([]byte("all good"))
if err != nil {
panic(err)
}
})
server := &http.Server{Addr: ":7349", Handler: mux}
go server.ListenAndServe()
defer server.Shutdown(context.Background())
time.Sleep(time.Millisecond)
SetBaseURL("http://localhost:7349/v3")
defer SetBaseURL("")
res, err := Post[string]("/sessions", M{"key": "value"})
if err != nil {
t.Fatalf("Got error: %s", err)
}
if res != "all good" {
t.Errorf("wrong response")
}
}
func TestTimeout(t *testing.T) {
mock = false
defer func() { mock = true }()
mux := http.NewServeMux()
mux.HandleFunc("/delay", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Minute)
w.WriteHeader(200)
})
server := &http.Server{Addr: ":7349", Handler: mux}
go server.ListenAndServe()
serverCtx, cancel := context.WithCancel(context.Background())
defer server.Shutdown(serverCtx)
time.Sleep(time.Millisecond)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond)
_, err := Post[string]("http://localhost:7349/delay", nil, Config{Ctx: ctx})
if err == nil || !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context deadealine, got=%v", err)
}
_, err = Post[string]("http://localhost:7349/delay", nil, Config{Timeout: 10 * time.Millisecond})
if err == nil || !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context deadealine, got=%v", err)
}
cancel() // kill sleeping http handlers
}