MCPcopy Index your code
hub / github.com/coder/coder / TestExtractWorkspaceProxy

Function TestExtractWorkspaceProxy

coderd/httpmw/workspaceproxy_test.go:23–189  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21)
22
23func TestExtractWorkspaceProxy(t *testing.T) {
24 t.Parallel()
25
26 successHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
27 // Only called if the API key passes through the handler.
28 httpapi.Write(context.Background(), rw, http.StatusOK, codersdk.Response{
29 Message: "It worked!",
30 })
31 })
32
33 t.Run("NoHeader", func(t *testing.T) {
34 t.Parallel()
35 var (
36 db, _ = dbtestutil.NewDB(t)
37 r = httptest.NewRequest("GET", "/", nil)
38 rw = httptest.NewRecorder()
39 )
40 httpmw.ExtractWorkspaceProxy(httpmw.ExtractWorkspaceProxyConfig{
41 DB: db,
42 })(successHandler).ServeHTTP(rw, r)
43 res := rw.Result()
44 defer res.Body.Close()
45 require.Equal(t, http.StatusUnauthorized, res.StatusCode)
46 })
47
48 t.Run("InvalidFormat", func(t *testing.T) {
49 t.Parallel()
50 var (
51 db, _ = dbtestutil.NewDB(t)
52 r = httptest.NewRequest("GET", "/", nil)
53 rw = httptest.NewRecorder()
54 )
55 r.Header.Set(httpmw.WorkspaceProxyAuthTokenHeader, "test:wow-hello")
56
57 httpmw.ExtractWorkspaceProxy(httpmw.ExtractWorkspaceProxyConfig{
58 DB: db,
59 })(successHandler).ServeHTTP(rw, r)
60 res := rw.Result()
61 defer res.Body.Close()
62 require.Equal(t, http.StatusUnauthorized, res.StatusCode)
63 })
64
65 t.Run("InvalidID", func(t *testing.T) {
66 t.Parallel()
67 var (
68 db, _ = dbtestutil.NewDB(t)
69 r = httptest.NewRequest("GET", "/", nil)
70 rw = httptest.NewRecorder()
71 )
72 r.Header.Set(httpmw.WorkspaceProxyAuthTokenHeader, "test:wow")
73
74 httpmw.ExtractWorkspaceProxy(httpmw.ExtractWorkspaceProxyConfig{
75 DB: db,
76 })(successHandler).ServeHTTP(rw, r)
77 res := rw.Result()
78 defer res.Body.Close()
79 require.Equal(t, http.StatusUnauthorized, res.StatusCode)
80 })

Callers

nothing calls this directly

Calls 14

WriteFunction · 0.92
NewDBFunction · 0.92
ExtractWorkspaceProxyFunction · 0.92
HexStringFunction · 0.92
WorkspaceProxyFunction · 0.92
WorkspaceProxyFunction · 0.92
RunMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.65
ServeHTTPMethod · 0.45
ResultMethod · 0.45

Tested by

no test coverage detected