-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint_test.go
More file actions
187 lines (159 loc) · 4.01 KB
/
endpoint_test.go
File metadata and controls
187 lines (159 loc) · 4.01 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
182
183
184
185
186
187
package fetch_test
import (
"os"
"testing"
"github.com/tinywasm/fetch"
)
type MockUser struct {
ID string
}
func (m MockUser) HandlerName() string {
return "/post_json"
}
func TestEndpointResolution(t *testing.T) {
t.Run("String endpoint", func(t *testing.T) {
req := fetch.Get("/users")
if req == nil {
t.Fatal("Request should not be nil")
}
})
t.Run("EndpointProvider interface", func(t *testing.T) {
user := MockUser{ID: "42"}
req := fetch.Get(user)
if req == nil {
t.Fatal("Request should not be nil")
}
})
}
func TestIntegration_BaseURL(t *testing.T) {
urlBytes, err := os.ReadFile(".test_server_url")
if err != nil {
t.Skip("Test server URL not found, skipping integration test")
}
baseURL := string(urlBytes)
t.Run("Global BaseURL", func(t *testing.T) {
fetch.SetBaseURL(baseURL)
defer fetch.SetBaseURL("")
done := make(chan bool)
var responseBody string
var responseErr error
fetch.Get("/get").Send(func(resp *fetch.Response, err error) {
if err != nil {
responseErr = err
} else {
responseBody = resp.Text()
}
done <- true
})
<-done
if responseErr != nil {
t.Fatalf("Expected no error, got %v", responseErr)
}
if responseBody != "get success" {
t.Errorf("Expected 'get success', got '%s'", responseBody)
}
})
t.Run("Per-request BaseURL override", func(t *testing.T) {
fetch.SetBaseURL("https://invalid.url")
defer fetch.SetBaseURL("")
done := make(chan bool)
var responseBody string
var responseErr error
fetch.Get("/get").
BaseURL(baseURL).
Send(func(resp *fetch.Response, err error) {
if err != nil {
responseErr = err
} else {
responseBody = resp.Text()
}
done <- true
})
<-done
if responseErr != nil {
t.Fatalf("Expected no error, got %v", responseErr)
}
if responseBody != "get success" {
t.Errorf("Expected 'get success', got '%s'", responseBody)
}
})
t.Run("EndpointProvider integration", func(t *testing.T) {
fetch.SetBaseURL(baseURL)
defer fetch.SetBaseURL("")
user := MockUser{ID: "42"}
done := make(chan bool)
var status int
fetch.Post(user).
ContentTypeJSON().
Body([]byte(`{"message":"hello"}`)).
Send(func(resp *fetch.Response, err error) {
if err == nil {
status = resp.Status
}
done <- true
})
<-done
if status != 200 {
t.Errorf("Expected status 200, got %d", status)
}
})
}
func TestFetchEdgeCases(t *testing.T) {
t.Run("GetBaseURL", func(t *testing.T) {
fetch.SetBaseURL("test")
if fetch.GetBaseURL() != "test" {
t.Error("GetBaseURL failed")
}
fetch.SetBaseURL("")
})
t.Run("Response Body getter", func(t *testing.T) {
resp := &fetch.Response{}
if len(resp.Body()) != 0 {
t.Error("Empty body expected")
}
})
t.Run("Dispatch no handler error", func(t *testing.T) {
fetch.SetHandler(nil)
fetch.Get("/").Dispatch() // Should just log and return
})
t.Run("resolveEndpoint errors", func(t *testing.T) {
// Nil endpoint
req := fetch.Get(nil)
req.Send(func(resp *fetch.Response, err error) {
if err == nil {
t.Error("Expected error for nil endpoint")
}
})
// Invalid type
req = fetch.Get(123)
req.Send(func(resp *fetch.Response, err error) {
if err == nil {
t.Error("Expected error for invalid endpoint type")
}
})
})
t.Run("joinURLPath empty path", func(t *testing.T) {
// Since we can't call internal functions, we test via buildFullURL if it was exposed
// or just trust that usual paths cover it if we use empty string endpoint
fetch.SetBaseURL("http://api.com")
defer fetch.SetBaseURL("")
fetch.Get("").Send(func(resp *fetch.Response, err error) {
// buildURL should return error for empty endpoint
if err == nil {
t.Error("Expected error for empty endpoint")
}
})
})
t.Run("Logging", func(t *testing.T) {
var logged bool
fetch.SetLog(func(args ...any) {
logged = true
})
fetch.SetHandler(nil)
fetch.Get("/").Dispatch() // This should trigger a log message
if !logged {
t.Error("Logger was not called")
}
fetch.SetLog(nil)
})
}