|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/RewstApp/agent-smith-go/internal/version" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewRequestWithContext_SetsVersionHeader(t *testing.T) { |
| 13 | + req, err := NewRequestWithContext(context.Background(), http.MethodGet, "https://example.com", nil) |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("expected no error, got %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + expected := version.Version[1:] |
| 19 | + got := req.Header.Get("x-rewst-agent-smith-version") |
| 20 | + if got != expected { |
| 21 | + t.Errorf("expected header %q, got %q", expected, got) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestNewRequestWithContext_SetsMethodAndURL(t *testing.T) { |
| 26 | + req, err := NewRequestWithContext(context.Background(), http.MethodPost, "https://example.com/path", nil) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("expected no error, got %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + if req.Method != http.MethodPost { |
| 32 | + t.Errorf("expected method POST, got %s", req.Method) |
| 33 | + } |
| 34 | + |
| 35 | + if req.URL.String() != "https://example.com/path" { |
| 36 | + t.Errorf("expected URL 'https://example.com/path', got %s", req.URL.String()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestNewRequestWithContext_WithBody(t *testing.T) { |
| 41 | + body := bytes.NewBufferString(`{"key":"value"}`) |
| 42 | + req, err := NewRequestWithContext(context.Background(), http.MethodPost, "https://example.com", body) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("expected no error, got %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + if req.Body == nil { |
| 48 | + t.Error("expected non-nil body") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestNewRequestWithContext_InvalidURL(t *testing.T) { |
| 53 | + _, err := NewRequestWithContext(context.Background(), http.MethodGet, "://invalid-url", nil) |
| 54 | + if err == nil { |
| 55 | + t.Error("expected error for invalid URL, got nil") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestNewRequestWithContext_PropagatesContext(t *testing.T) { |
| 60 | + ctx, cancel := context.WithCancel(context.Background()) |
| 61 | + cancel() |
| 62 | + |
| 63 | + req, err := NewRequestWithContext(ctx, http.MethodGet, "https://example.com", nil) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("expected no error creating request, got %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + if req.Context().Err() == nil { |
| 69 | + t.Error("expected cancelled context to propagate to request") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestNewRequest_SetsVersionHeader(t *testing.T) { |
| 74 | + req, err := NewRequest(http.MethodGet, "https://example.com", nil) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("expected no error, got %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + expected := version.Version[1:] |
| 80 | + got := req.Header.Get("x-rewst-agent-smith-version") |
| 81 | + if got != expected { |
| 82 | + t.Errorf("expected header %q, got %q", expected, got) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestNewRequest_UsesBackgroundContext(t *testing.T) { |
| 87 | + req, err := NewRequest(http.MethodGet, "https://example.com", nil) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("expected no error, got %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + if req.Context() == nil { |
| 93 | + t.Error("expected non-nil context") |
| 94 | + } |
| 95 | + |
| 96 | + if req.Context().Err() != nil { |
| 97 | + t.Errorf("expected non-cancelled context, got %v", req.Context().Err()) |
| 98 | + } |
| 99 | +} |
0 commit comments