131 lines
3.5 KiB
Go
131 lines
3.5 KiB
Go
package stdlib
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestFetch(t *testing.T) {
|
|
// Create Context
|
|
ctx := context.Background()
|
|
|
|
// Create Test Server
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("X-Custom-Header", "test-value")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`{"status":"ok","message":"Hello from httptest"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
// Execute Fetch
|
|
result, err := Fetch(ctx, FetchArgs{Input: server.URL})
|
|
require.NoError(t, err)
|
|
|
|
// Verify Response
|
|
assert.True(t, result.OK)
|
|
assert.Equal(t, http.StatusOK, result.Status)
|
|
assert.Contains(t, result.Body, "Hello from httptest")
|
|
assert.Contains(t, result.Body, `"status":"ok"`)
|
|
assert.Equal(t, "application/json", result.Headers["Content-Type"])
|
|
assert.Equal(t, "test-value", result.Headers["X-Custom-Header"])
|
|
}
|
|
|
|
func TestFetchHTTPBin(t *testing.T) {
|
|
// Create Context
|
|
ctx := context.Background()
|
|
|
|
// Create Test Server
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`{"args":{}}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
// Execute Fetch
|
|
result, err := Fetch(ctx, FetchArgs{Input: server.URL})
|
|
require.NoError(t, err)
|
|
|
|
// Verify Response
|
|
assert.True(t, result.OK)
|
|
assert.Equal(t, http.StatusOK, result.Status)
|
|
assert.Contains(t, result.Body, `"args"`)
|
|
assert.Equal(t, "application/json", result.Headers["Content-Type"])
|
|
}
|
|
|
|
func TestFetchWith404(t *testing.T) {
|
|
// Create Context
|
|
ctx := context.Background()
|
|
|
|
// Execute Fetch
|
|
result, err := Fetch(ctx, FetchArgs{Input: "https://httpbin.org/status/404"})
|
|
require.NoError(t, err)
|
|
|
|
// Verify Response
|
|
assert.False(t, result.OK)
|
|
assert.Equal(t, http.StatusNotFound, result.Status)
|
|
}
|
|
|
|
func TestFetchWithInvalidURL(t *testing.T) {
|
|
// Create Context
|
|
ctx := context.Background()
|
|
|
|
// Execute Fetch - Should Fail
|
|
_, err := Fetch(ctx, FetchArgs{Input: "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) {
|
|
// Create Context
|
|
ctx := context.Background()
|
|
|
|
// Create Test Server
|
|
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()
|
|
|
|
// Configure Request Options
|
|
headers := map[string]string{
|
|
"Authorization": "Bearer test-token",
|
|
}
|
|
options := &RequestInit{
|
|
Method: "GET",
|
|
Headers: headers,
|
|
}
|
|
|
|
// Execute Fetch with Headers
|
|
result, err := Fetch(ctx, FetchArgs{Input: server.URL, Init: options})
|
|
require.NoError(t, err)
|
|
assert.True(t, result.OK)
|
|
}
|
|
|
|
func TestFetchDefaults(t *testing.T) {
|
|
// Create Context
|
|
ctx := context.Background()
|
|
|
|
// Create Test Server
|
|
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()
|
|
|
|
// Execute Fetch with Empty Options
|
|
options := &RequestInit{}
|
|
result, err := Fetch(ctx, FetchArgs{Input: server.URL, Init: options})
|
|
require.NoError(t, err)
|
|
assert.True(t, result.OK)
|
|
}
|