-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpassthrough_test.go
More file actions
86 lines (74 loc) · 2.24 KB
/
passthrough_test.go
File metadata and controls
86 lines (74 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package aibridge
import (
"net/http"
"net/http/httptest"
"testing"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/aibridge/internal/testutil"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel"
)
var testTracer = otel.Tracer("bridge_test")
func TestPassthroughRoutes(t *testing.T) {
t.Parallel()
upstreamRespBody := "upstream response"
tests := []struct {
name string
baseURLPath string
passthroughRoute string
expectRequestPath string
expectRespStatus int
expectRespBody string
}{
{
name: "passthrough_route_no_path",
passthroughRoute: "/v1/conversations",
expectRequestPath: "/v1/conversations",
expectRespStatus: http.StatusOK,
expectRespBody: upstreamRespBody,
},
{
name: "base_URL_path_is_preserved_in_passthrough_routes",
baseURLPath: "/api/v2",
passthroughRoute: "/v1/models",
expectRequestPath: "/api/v2/v1/models",
expectRespStatus: http.StatusOK,
expectRespBody: upstreamRespBody,
},
{
name: "passthrough_route_break_parse_base_url",
baseURLPath: "/%zz",
passthroughRoute: "/v1/models/",
expectRespStatus: http.StatusBadGateway,
expectRespBody: "request error",
},
{
name: "passthrough_route_break_join_path",
baseURLPath: "/%25",
passthroughRoute: "/v1/models",
expectRespStatus: http.StatusInternalServerError,
expectRespBody: "failed to join upstream path",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
logger := slogtest.Make(t, nil)
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tc.expectRequestPath, r.URL.Path)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(upstreamRespBody))
}))
t.Cleanup(upstream.Close)
prov := &testutil.MockProvider{
URL: upstream.URL + tc.baseURLPath,
}
handler := newPassthroughRouter(prov, logger, nil, testTracer)
req := httptest.NewRequest("", tc.passthroughRoute, nil)
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, tc.expectRespStatus, resp.Code)
assert.Contains(t, resp.Body.String(), tc.expectRespBody)
})
}
}