This commit is contained in:
2026-01-27 13:27:34 -05:00
parent fb6f260630
commit 28b1ad32f5
4 changed files with 302 additions and 169 deletions

View File

@@ -18,7 +18,7 @@ func TestFetch(t *testing.T) {
}))
defer server.Close()
result, err := Fetch(server.URL, nil)
result, err := Fetch(FetchArgs{URL: server.URL})
require.NoError(t, err)
assert.True(t, result.OK)
@@ -32,7 +32,7 @@ func TestFetch(t *testing.T) {
func TestFetchHTTPBin(t *testing.T) {
t.Skip("httpbin.org test is flaky")
result, err := Fetch("https://httpbin.org/get", nil)
result, err := Fetch(FetchArgs{URL: "https://httpbin.org/get"})
require.NoError(t, err)
assert.True(t, result.OK)
@@ -42,7 +42,7 @@ func TestFetchHTTPBin(t *testing.T) {
}
func TestFetchWith404(t *testing.T) {
result, err := Fetch("https://httpbin.org/status/404", nil)
result, err := Fetch(FetchArgs{URL: "https://httpbin.org/status/404"})
require.NoError(t, err)
assert.False(t, result.OK)
@@ -50,7 +50,58 @@ func TestFetchWith404(t *testing.T) {
}
func TestFetchWithInvalidURL(t *testing.T) {
_, err := Fetch("http://this-domain-does-not-exist-12345.com", nil)
_, err := Fetch(FetchArgs{URL: "http://this-domain-does-not-exist-12345.com"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to fetch")
}
func TestFetchWithHeaders(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization"))
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`ok`))
}))
defer server.Close()
headers := map[string]string{
"Authorization": "Bearer test-token",
}
options := &FetchOptions{
Method: "GET",
Headers: &headers,
}
result, err := Fetch(FetchArgs{URL: server.URL, Options: options})
require.NoError(t, err)
assert.True(t, result.OK)
}
func TestFetchDefaults(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method, "default method should be GET")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`ok`))
}))
defer server.Close()
options := &FetchOptions{}
result, err := Fetch(FetchArgs{URL: server.URL, Options: options})
require.NoError(t, err)
assert.True(t, result.OK)
}
func TestAdd(t *testing.T) {
result := add(AddArgs{A: 5, B: 10})
assert.Equal(t, 15, result)
result = add(AddArgs{A: -3, B: 7})
assert.Equal(t, 4, result)
}
func TestGreet(t *testing.T) {
result := greet(GreetArgs{Name: "World"})
assert.Equal(t, "Hello, World!", result)
result = greet(GreetArgs{Name: "Alice"})
assert.Equal(t, "Hello, Alice!", result)
}