types
This commit is contained in:
@@ -42,9 +42,11 @@ type Response struct {
|
||||
}
|
||||
|
||||
func Fetch(ctx context.Context, args FetchArgs) (Response, error) {
|
||||
// Set Default Method and Headers
|
||||
method := "GET"
|
||||
headers := make(map[string]string)
|
||||
|
||||
// Apply Init Options
|
||||
if args.Init != nil {
|
||||
if args.Init.Method != "" {
|
||||
method = args.Init.Method
|
||||
@@ -54,15 +56,18 @@ func Fetch(ctx context.Context, args FetchArgs) (Response, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Create Request
|
||||
req, err := http.NewRequestWithContext(ctx, method, args.Input, nil)
|
||||
if err != nil {
|
||||
return Response{}, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
// Set Request Headers
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
// Execute Request
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return Response{}, fmt.Errorf("failed to fetch: %w", err)
|
||||
@@ -71,11 +76,13 @@ func Fetch(ctx context.Context, args FetchArgs) (Response, error) {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
// Read Response Body
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return Response{}, fmt.Errorf("failed to read body: %w", err)
|
||||
}
|
||||
|
||||
// Collect Response Headers
|
||||
resultHeaders := make(map[string]string)
|
||||
for key, values := range resp.Header {
|
||||
if len(values) > 0 {
|
||||
@@ -85,6 +92,7 @@ func Fetch(ctx context.Context, args FetchArgs) (Response, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Return Response
|
||||
return Response{
|
||||
OK: resp.StatusCode >= 200 && resp.StatusCode < 300,
|
||||
Status: resp.StatusCode,
|
||||
|
||||
@@ -11,7 +11,10 @@ import (
|
||||
)
|
||||
|
||||
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")
|
||||
@@ -20,9 +23,11 @@ func TestFetch(t *testing.T) {
|
||||
}))
|
||||
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")
|
||||
@@ -32,7 +37,10 @@ func TestFetch(t *testing.T) {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -40,9 +48,11 @@ func TestFetchHTTPBin(t *testing.T) {
|
||||
}))
|
||||
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"`)
|
||||
@@ -50,23 +60,33 @@ func TestFetchHTTPBin(t *testing.T) {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -75,6 +95,7 @@ func TestFetchWithHeaders(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Configure Request Options
|
||||
headers := map[string]string{
|
||||
"Authorization": "Bearer test-token",
|
||||
}
|
||||
@@ -82,13 +103,18 @@ func TestFetchWithHeaders(t *testing.T) {
|
||||
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)
|
||||
@@ -96,6 +122,7 @@ func TestFetchDefaults(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Execute Fetch with Empty Options
|
||||
options := &RequestInit{}
|
||||
result, err := Fetch(ctx, FetchArgs{Input: server.URL, Init: options})
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user