-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_test.go
103 lines (80 loc) · 3.13 KB
/
get_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
package fetch
import (
"fmt"
"testing"
)
func Test_Get(t *testing.T) {
response, _ := Get("https://httpbin.zcorky.com/get")
if response.Status != 200 {
t.Error("Expected status code 200, got", response.Status)
}
if response.Headers.Get("content-type") != "application/json; charset=utf-8" {
t.Error("Expected content-type application/json; charset=utf-8, got", response.Headers.Get("content-type"))
}
if response.Headers.Get("server") != "openresty" {
t.Error("Expected server openresty, got", response.Headers.Get("server"))
}
if response.Get("url").String() != "/get" {
t.Error("Expected url /get, got", response.Get("url").String())
}
if response.Get("method").String() != "GET" {
t.Error("Expected method GET, got", response.Get("method").String())
}
if response.Get("headers.host").String() != "httpbin.zcorky.com" {
t.Error("Expected Host httpbin.zcorky.com, got", response.Get("headers.Host").String())
}
if response.Get("headers.accept-encoding").String() != "gzip" {
t.Error("Expected accept-encoding gzip, got", response.Get("headers.accept-encoding").String())
}
if response.Get("headers.user-agent").String() != DefaultUserAgent() {
t.Error(fmt.Sprintf("Expected user-agent %s, got", DefaultUserAgent()), response.Get("headers.user-agent").String())
}
if response.Get("headers.connection").String() != "close" {
t.Error("Expected connection close, got", response.Get("headers.connection").String())
}
if response.Get("origin").String() != "https://httpbin.zcorky.com" {
t.Error("Expected origin https://httpbin.zcorky.com, got", response.Get("origin").String())
}
}
func Test_Get_With_Header(t *testing.T) {
response, _ := Get("https://httpbin.zcorky.com/get", &Config{
Headers: map[string]string{
"X-CUSTOM-VAR": "custom-value",
"x-custom-var-2": "custom-value-2",
},
})
// fmt.Println("raw: ", response.JSON())
if response.Get("headers.x-custom-var").String() != "custom-value" {
t.Error("Expected x-custom-var custom-value, got", response.Get("headers.x-custom-var").String())
}
if response.Get("headers.x-custom-var-2").String() != "custom-value-2" {
t.Error("Expected x-custom-var-2 custom-value, got", response.Get("headers.x-custom-var").String())
}
}
func Test_Get_With_Query(t *testing.T) {
response, _ := Get("https://httpbin.zcorky.com/get", &Config{
Query: map[string]string{
"foo": "bar",
"foo2": "bar2",
},
})
if response.Get("query.foo").String() != "bar" {
t.Error("Expected foo bar, got", response.Get("query.foo").String())
}
if response.Get("query.foo2").String() != "bar2" {
t.Error("Expected foo2 bar2, got", response.Get("query.foo2").String())
}
}
func Test_Get_With_BasicAuth_In_URL(t *testing.T) {
response, err := Get("https://user1:[email protected]/headers")
if err != nil {
t.Error("Expected no error, got", err)
}
if response.Status != 200 {
t.Error("Expected status code 200, got", response.Status)
}
if response.Get("headers.authorization").String() != "Basic dXNlcjE6cGFzczE=" {
t.Error("Expected Authorization Basic dXNlcjE6cGFzczE=, got", response.Get("headers.authorization").String())
}
fmt.Println(response.String())
}