-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq_test.go
141 lines (127 loc) · 3.61 KB
/
req_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
package req
import (
"errors"
"net/http"
"testing"
)
func Test_urlCaller_URL(t *testing.T) {
if URL("a=%d", 1).URL() != "a=1" {
t.Fatal("test failed")
}
}
func Test_urlCaller_Headers(t *testing.T) {
c := &urlCaller{}
c.AddHeader("a", "1")
c.AddHeader("b", "2")
c.DelHeader("b")
if c.Headers()["a"] != "1" || c.Headers()["b"] != "" {
t.Fatal("test failed")
}
c.SetHeaders(nil)
if c.Headers() != nil {
t.Fatal("test failed")
}
}
func TestGET(t *testing.T) {
var posts []struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
if err := GET("https://jsonplaceholder.typicode.com/posts", &posts); err != nil {
t.Fatal(err)
}
t.Log(len(posts))
}
func TestPOST(t *testing.T) {
var post struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
if err := POST("https://jsonplaceholder.typicode.com/posts", map[string]any{"Title": "Hello"}, &post, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
if post.Id < 0 || post.Title != "Hello" {
t.Fatal(errors.New("request failed"))
}
t.Log(post)
}
func TestPATCH(t *testing.T) {
var post struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
if err := PATCH("https://jsonplaceholder.typicode.com/posts/1", map[string]any{"Title": "Hello"}, &post, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
if post.Id != 1 || post.Title != "Hello" {
t.Fatal(errors.New("request failed"))
}
t.Log(post)
}
func TestPUT(t *testing.T) {
var post struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
if err := PUT("https://jsonplaceholder.typicode.com/posts/1", map[string]any{"Title": "Hello"}, &post, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
if post.Id != 1 || post.Title != "Hello" {
t.Fatal(errors.New("request failed"))
}
t.Log(post)
}
func TestDELETE(t *testing.T) {
var ret map[string]string
if err := DELETE("https://jsonplaceholder.typicode.com/posts/1", &ret, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
t.Log(ret)
if err := DELETE("https://reqres.in/api/users/2", &ret); err == nil {
t.Fatal("test failed")
}
}
func TestDELETEWithBody(t *testing.T) {
var ret map[string]string
if err := DELETEWithBody("https://jsonplaceholder.typicode.com/posts/1", map[string]any{"Title": "Hello"}, &ret, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
t.Log(ret)
}
func TestCALL(t *testing.T) {
var post struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
if err := CALL(http.MethodPost, "https://jsonplaceholder.typicode.com/posts", map[string]any{"Title": "Hello"}, &post, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
if post.Id < 0 || post.Title != "Hello" {
t.Fatal(errors.New("request failed"))
}
t.Log(post)
if err := CALL(http.MethodPost, "https://jsonplaceholder.typicode.com/posts", map[string]any{"Title": "Hello"}, nil, map[string]string{"UID": "1"}); err != nil {
t.Fatal(err)
}
if err := CALL(" ", "https://54d483ff-bb2b-a4f7-26e4-6b8b11c09ed1.com/api/abc", nil, &post); err == nil {
t.Fatal("test failed")
}
}
func TestNewCALL(t *testing.T) {
var ret map[string]string
caller := NewCALL("POST", "https://jsonplaceholder.typicode.com/posts/1", map[string]any{"Title": "Hello"}, &ret, map[string]string{"UID": "1"})
if err := caller.Do(); err != nil {
t.Fatal(err)
}
t.Log(ret)
}