Skip to content

Commit 60b542f

Browse files
committed
fixup! fix(lib/httpapi): redirect based on chatBasePath
1 parent ace9c6a commit 60b542f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/httpapi/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ func NewServer(ctx context.Context, agentType mf.AgentType, process *termexec.Pr
106106
return s
107107
}
108108

109+
// Router returns the underlying chi.Router for testing purposes.
110+
func (s *Server) Router() http.Handler {
111+
return s.router
112+
}
113+
109114
func (s *Server) StartSnapshotLoop(ctx context.Context) {
110115
s.conversation.StartSnapshotLoop(ctx)
111116
go func() {

lib/httpapi/server_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"io"
88
"log/slog"
9+
"net/http"
10+
"net/http/httptest"
911
"os"
1012
"sort"
1113
"testing"
@@ -73,3 +75,38 @@ func TestOpenAPISchema(t *testing.T) {
7375

7476
require.Equal(t, currentSchema, diskSchema)
7577
}
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

Comments
 (0)