-
Notifications
You must be signed in to change notification settings - Fork 0
/
webreq_get_test.go
170 lines (140 loc) · 3.71 KB
/
webreq_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package webreq_test
import (
"fmt"
"github.com/tonnytg/webreq"
"testing"
"time"
)
func TestPackageCall(t *testing.T) {
var headersList = make(map[string]string)
headersList["Content-Type"] = "application/json"
headers := webreq.NewHeaders(headersList)
request := webreq.NewRequest("GET")
request.SetURL("https://examples.com/values")
request.SetHeaders(headers.ListHeaders) // Pass the map directly here
request.SetTimeout(10)
body, err := request.Execute()
if err != nil {
t.Error(err)
}
bodyString := string(body)
if bodyString == "" {
t.Error("body is empty")
}
}
// TestSetURL verify URL it is correctly defined
func TestSetURL(t *testing.T) {
t.Run("Non-empty URL", func(t *testing.T) {
request := webreq.NewRequest("GET")
request.SetURL("https://example.com")
// Verifique se a URL é definida corretamente
if request.URL != "https://example.com" {
t.Errorf("URL not set correctly")
}
})
}
func TestSetTimeout(t *testing.T) {
request := webreq.NewRequest("GET")
request.SetTimeout(10)
if request.TimeoutDuration != (10 * time.Second) {
fmt.Println(request.TimeoutDuration)
t.Error("request.TimeoutDuration is not 10 * time.Second")
}
return
}
func TestSetHeaders(t *testing.T) {
var headersList = make(map[string]string)
headersList["Content-Type"] = "application/json"
headers := webreq.NewHeaders(headersList)
if headers.ListHeaders["Content-Type"] != "application/json" {
t.Error("headers.Headers[Content-Type] is not application/json")
}
request := webreq.NewRequest("GET")
request.SetHeaders(headers.ListHeaders)
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
return
}
func TestSetHeaders2(t *testing.T) {
headers := webreq.NewHeaders(nil)
headers.Add("Content-Type", "application/json")
if headers.ListHeaders["Content-Type"] != "application/json" {
t.Error("headers.Headers[Content-Type] is not application/json")
}
request := webreq.NewRequest("GET")
request.SetHeaders(headers.ListHeaders)
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
return
}
func TestSetData(t *testing.T) {
request := webreq.NewRequest("GET")
request.SetData([]byte("test"))
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
return
}
func TestSetMethod(t *testing.T) {
request := webreq.NewRequest("GET")
if request == nil {
t.Error("request is nil")
}
request.SetMethod("GET")
if request.Method != "GET" {
t.Error("request.Method is not GET")
}
request.SetMethod("POST")
if request.Method != "POST" {
t.Error("request.Method is not POST")
}
request.SetMethod("")
if request.ErrorMessage != "request method is empty" {
t.Error("request.Method is not empty")
}
return
}
func TestSetStatusCode(t *testing.T) {
request := webreq.NewRequest("GET")
if request == nil {
t.Error("request is nil")
}
request.SetStatusCode(200)
if request.StatusCode != 200 {
t.Error("request.StatusCode is not 200")
}
return
}
func TestSetNewRequest(t *testing.T) {
newRequest := webreq.NewRequest("GET")
if newRequest == nil {
t.Error("newRequest is nil")
}
}
func TestEndToEnd(t *testing.T) {
headers := webreq.NewHeaders(nil)
headers.Add("Content-Type", "application/json")
request := webreq.NewRequest("GET")
if request == nil {
t.Error("request is nil")
}
request.SetURL("https://example.com/values")
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
request.SetHeaders(headers.ListHeaders)
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
request.SetTimeout(10)
body, err := request.Execute()
if err != nil {
t.Error(err)
}
bodyString := string(body)
if bodyString == "" {
t.Error("body is empty")
}
}