-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgohttp_test.go
148 lines (132 loc) · 3.92 KB
/
gohttp_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
package gohttp
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestSetProxy(t *testing.T) {
if err := SetProxy("://localhost"); err == nil {
t.Error("gave nil error; want some error")
}
s := NewSession()
if err := s.SetProxy("http://localhost"); err != nil {
t.Error(err)
}
if err := s.SetProxy("://localhost"); err == nil {
t.Error("gave nil error; want some error")
}
}
func TestSetClient(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatal("gave panic; want no panic")
}
}()
SetClient(http.DefaultClient)
}
func TestGetAndHead(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
}))
defer ts.Close()
SetNoProxy()
resp, err := Get(ts.URL, H{"hello": "world"})
if err != nil {
t.Fatal(err)
}
if resp.Request().Method != "GET" {
t.Errorf("expected method %q; got %q", "GET", resp.Request().Method)
}
if resp.Request().URL.String() != ts.URL {
t.Errorf("expected URL %q; got %q", ts.URL, resp.Request().URL.String())
}
if h := resp.Request().Header.Get("hello"); h != "world" {
t.Errorf("expected hello header %q; got %q", "world", h)
}
if ua := resp.Request().Header.Get("user-agent"); ua != "Go-HTTP-Client" {
t.Errorf("expected user agent %q; got %q", "Go-HTTP-Client", ua)
}
if s := resp.String(); s != "Hello, world!" {
t.Error("Incorrect get response body:", s)
}
resp, err = Head(ts.URL, H{"hello": "world"})
if err != nil {
t.Fatal(err)
}
if resp.Request().Method != "HEAD" {
t.Errorf("expected method %q; got %q", "HEAD", resp.Request().Method)
}
if l := resp.Request().ContentLength; l != 0 {
t.Error("Incorrect head response body:", l)
}
}
func TestPost(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, _ := io.ReadAll(r.Body)
fmt.Fprint(w, string(c))
}))
defer ts.Close()
SetAgent("test")
resp, err := Post(ts.URL, H{"hello": "world"}, nil)
if err != nil {
t.Fatal(err)
}
defer resp.Close()
if resp.Request().Method != "POST" {
t.Errorf("expected method %q; got %q", "POST", resp.Request().Method)
}
if resp.Request().URL.String() != ts.URL {
t.Errorf("expected URL %q; got %q", ts.URL, resp.Request().URL.String())
}
if h := resp.Request().Header.Get("hello"); h != "world" {
t.Errorf("expected hello header %q; got %q", "world", h)
}
if ua := resp.Request().Header.Get("user-agent"); ua != "test" {
t.Errorf("expected user agent %q; got %q", "test", ua)
}
if l := resp.Request().ContentLength; l != 0 {
t.Error("Incorrect response body:", l)
}
resp, err = Post(ts.URL, nil, url.Values{"test": []string{"test"}})
if err != nil {
t.Fatal(err)
}
defer resp.Close()
if ct := resp.Request().Header.Get("Content-Type"); ct != "application/x-www-form-urlencoded" {
t.Errorf("expected Content-Type header %q; got %q", "application/x-www-form-urlencoded", ct)
}
if s := resp.String(); s != "test=test" {
t.Errorf("expected response body %q; got %q", "test=test", s)
}
resp, err = Post(ts.URL, nil, map[string]any{"test": "test"})
if err != nil {
t.Fatal(err)
}
defer resp.Close()
if ct := resp.Request().Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("expected Content-Type header %q; got %q", "application/json", ct)
}
var json struct{ Test string }
if err := resp.JSON(&json); err != nil {
t.Error(err)
}
if json != struct{ Test string }{Test: "test"} {
t.Error("Incorrect response body:", json)
}
}
func TestUpload(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, _ := io.ReadAll(r.Body)
fmt.Fprint(w, string(c))
}))
defer ts.Close()
if _, err := Upload(ts.URL, nil, nil, &File{ReadCloser: errReader(0)}); err == nil {
t.Error("gave nil error; want error")
}
if _, err := Upload(ts.URL, H{"header": "value"}, nil, F("readme", "README.md")); err != nil {
t.Error(err)
}
}