Skip to content

Commit 0a63bec

Browse files
committed
Add test for error handler
1 parent 60ad881 commit 0a63bec

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

coderd/workspaceapps.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/coder/coder/site"
1919
)
2020

21+
// workspaceAppsProxyPath proxies requests to a workspace application
22+
// through a relative URL path.
2123
func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request) {
2224
user := httpmw.UserParam(r)
2325
// This can be in the form of: "<workspace-name>.[workspace-agent]" or "<workspace-name>"
@@ -115,12 +117,15 @@ func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request)
115117
}
116118

117119
proxy := httputil.NewSingleHostReverseProxy(appURL)
118-
// Write an error using our embed handler
119120
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
121+
// This is a browser-facing route so JSON responses are not viable here.
122+
// To pass friendly errors to the frontend, special meta tags are overridden
123+
// in the index.html with the content passed here.
120124
r = r.WithContext(site.WithAPIResponse(r.Context(), site.APIResponse{
121125
StatusCode: http.StatusBadGateway,
122126
Message: err.Error(),
123127
}))
128+
w.WriteHeader(http.StatusBadGateway)
124129
api.siteHandler.ServeHTTP(w, r)
125130
}
126131
path := chi.URLParam(r, "*")

coderd/workspaceapps_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
5757
Apps: []*proto.App{{
5858
Name: "example",
5959
Url: fmt.Sprintf("http://127.0.0.1:%d?query=true", tcpAddr.Port),
60+
}, {
61+
Name: "fake",
62+
Url: "http://127.0.0.2",
6063
}},
6164
}},
6265
}},
@@ -111,4 +114,12 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
111114
require.Equal(t, "", string(body))
112115
require.Equal(t, http.StatusOK, resp.StatusCode)
113116
})
117+
118+
t.Run("ProxyError", func(t *testing.T) {
119+
t.Parallel()
120+
resp, err := client.Request(context.Background(), http.MethodGet, "/@me/"+workspace.Name+"/apps/fake/", nil)
121+
require.NoError(t, err)
122+
defer resp.Body.Close()
123+
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
124+
})
114125
}

site/site.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ func (h *handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
134134

135135
apiResponseRaw := req.Context().Value(apiResponseContextKey{})
136136
if apiResponseRaw != nil {
137-
state.APIResponse = apiResponseRaw.(APIResponse)
137+
apiResponse, ok := apiResponseRaw.(APIResponse)
138+
if !ok {
139+
panic("dev error: api response in context isn't the correct type")
140+
}
141+
state.APIResponse = apiResponse
138142
}
139143

140144
// First check if it's a file we have in our templates

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy