-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_test.go
More file actions
181 lines (155 loc) · 4.54 KB
/
Copy pathfetch_test.go
File metadata and controls
181 lines (155 loc) · 4.54 KB
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
171
172
173
174
175
176
177
178
179
180
181
//go:build js && webtests
package fetch
import (
"context"
"encoding/json"
"io"
"strings"
"testing"
"time"
)
func TestFetchHttpBin(t *testing.T) {
t.Run("GET request", func(t *testing.T) {
url := "https://httpbin.org/get"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
opts := &Opts{
Method: "GET",
Signal: ctx,
}
resp, err := Fetch(url, opts)
if err != nil {
t.Fatalf("Failed to fetch: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected status code 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
url, ok := result["url"].(string)
if !ok {
t.Fatalf("Response does not contain a 'url' field of type string")
}
if url != "https://httpbin.org/get" {
t.Errorf("Expected URL to be 'https://httpbin.org/get', got '%s'", url)
}
t.Logf("Received response from URL: %s", url)
})
t.Run("POST request with body", func(t *testing.T) {
url := "https://httpbin.org/post"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
postBody := strings.NewReader(`{"key": "value"}`)
opts := &Opts{
Method: "POST",
Signal: ctx,
Body: postBody,
Header: Header{
"Content-Type": []string{"application/json"},
},
}
resp, err := Fetch(url, opts)
if err != nil {
t.Fatalf("Failed to fetch: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected status code 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
jsonData, ok := result["json"].(map[string]interface{})
if !ok {
t.Fatalf("Response does not contain a 'json' field of type map[string]interface{}")
}
value, ok := jsonData["key"].(string)
if !ok || value != "value" {
t.Errorf("Expected JSON data to contain {'key': 'value'}, got %v", jsonData)
}
t.Logf("Received response with correct POST body")
})
}
func TestMerge(t *testing.T) {
t.Run("CommonOpts.Merge", func(t *testing.T) {
base := &CommonOpts{
Mode: "cors",
Credentials: "include",
Cache: "no-cache",
}
other := &CommonOpts{
Mode: "no-cors",
ReferrerPolicy: "no-referrer",
KeepAlive: &[]bool{true}[0],
}
base.Merge(other)
if base.Mode != "no-cors" {
t.Errorf("Expected Mode to be 'no-cors', got '%s'", base.Mode)
}
if base.Credentials != "include" {
t.Errorf("Expected Credentials to be 'include', got '%s'", base.Credentials)
}
if base.Cache != "no-cache" {
t.Errorf("Expected Cache to be 'no-cache', got '%s'", base.Cache)
}
if base.ReferrerPolicy != "no-referrer" {
t.Errorf("Expected ReferrerPolicy to be 'no-referrer', got '%s'", base.ReferrerPolicy)
}
if base.KeepAlive == nil || *base.KeepAlive != true {
t.Errorf("Expected KeepAlive to be true, got %v", base.KeepAlive)
}
})
t.Run("Opts.Merge", func(t *testing.T) {
base := &Opts{
Method: "GET",
Header: Header{"Content-Type": []string{"application/json"}},
CommonOpts: CommonOpts{
Mode: "cors",
},
}
other := &Opts{
Method: "POST",
Header: Header{"Authorization": []string{"Bearer token"}},
Body: strings.NewReader("test body"),
CommonOpts: CommonOpts{
Credentials: "include",
},
}
base.Merge(other)
if base.Method != "POST" {
t.Errorf("Expected Method to be 'POST', got '%s'", base.Method)
}
if len(base.Header) != 2 {
t.Errorf("Expected 2 headers, got %d", len(base.Header))
}
if base.Header.Get("Content-Type") != "application/json" {
t.Errorf("Expected Content-Type header to be 'application/json', got '%s'", base.Header.Get("Content-Type"))
}
if base.Header.Get("Authorization") != "Bearer token" {
t.Errorf("Expected Authorization header to be 'Bearer token', got '%s'", base.Header.Get("Authorization"))
}
if base.Body == nil {
t.Error("Expected Body to be set, got nil")
}
if base.CommonOpts.Mode != "cors" {
t.Errorf("Expected Mode to be 'cors', got '%s'", base.CommonOpts.Mode)
}
if base.CommonOpts.Credentials != "include" {
t.Errorf("Expected Credentials to be 'include', got '%s'", base.CommonOpts.Credentials)
}
})
}