|
6 | 6 | "fmt" |
7 | 7 | "io" |
8 | 8 | "log/slog" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
9 | 11 | "os" |
10 | 12 | "sort" |
11 | 13 | "testing" |
@@ -73,3 +75,38 @@ func TestOpenAPISchema(t *testing.T) { |
73 | 75 |
|
74 | 76 | require.Equal(t, currentSchema, diskSchema) |
75 | 77 | } |
| 78 | + |
| 79 | +func TestServer_redirectToChat(t *testing.T) { |
| 80 | + cases := []struct { |
| 81 | + name string |
| 82 | + chatBasePath string |
| 83 | + expectedResponseCode int |
| 84 | + expectedLocation string |
| 85 | + }{ |
| 86 | + {"default base path", "/chat", http.StatusTemporaryRedirect, "/chat/embed"}, |
| 87 | + {"custom base path", "/custom", http.StatusTemporaryRedirect, "/custom/embed"}, |
| 88 | + } |
| 89 | + for _, tc := range cases { |
| 90 | + t.Run(tc.name, func(t *testing.T) { |
| 91 | + t.Parallel() |
| 92 | + tCtx := logctx.WithLogger(context.Background(), slog.New(slog.NewTextHandler(os.Stdout, nil))) |
| 93 | + s := httpapi.NewServer(tCtx, msgfmt.AgentTypeClaude, nil, 0, tc.chatBasePath) |
| 94 | + tsServer := httptest.NewServer(s.Router()) |
| 95 | + t.Cleanup(tsServer.Close) |
| 96 | + |
| 97 | + client := &http.Client{ |
| 98 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 99 | + return http.ErrUseLastResponse |
| 100 | + }, |
| 101 | + } |
| 102 | + resp, err := client.Get(tsServer.URL + "/") |
| 103 | + require.NoError(t, err, "unexpected error making GET request") |
| 104 | + t.Cleanup(func() { |
| 105 | + _ = resp.Body.Close() |
| 106 | + }) |
| 107 | + require.Equal(t, tc.expectedResponseCode, resp.StatusCode, "expected %d status code", tc.expectedResponseCode) |
| 108 | + loc := resp.Header.Get("Location") |
| 109 | + require.Equal(t, tc.expectedLocation, loc, "expected Location %q, got %q", tc.expectedLocation, loc) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments