diff --git a/coderd/httpmw/httpmw.go b/coderd/httpmw/httpmw.go index 11f363e7ea244..74dd987248b87 100644 --- a/coderd/httpmw/httpmw.go +++ b/coderd/httpmw/httpmw.go @@ -26,7 +26,7 @@ func parseUUID(rw http.ResponseWriter, r *http.Request, param string) (uuid.UUID parsed, err := uuid.Parse(rawID) if err != nil { httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{ - Message: fmt.Sprintf("Invalid UUID %q.", param), + Message: fmt.Sprintf("Invalid UUID %q.", rawID), Detail: err.Error(), }) return uuid.UUID{}, false diff --git a/coderd/httpmw/httpmw_internal_test.go b/coderd/httpmw/httpmw_internal_test.go new file mode 100644 index 0000000000000..381c8608d2649 --- /dev/null +++ b/coderd/httpmw/httpmw_internal_test.go @@ -0,0 +1,55 @@ +package httpmw + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/coder/coder/codersdk" +) + +const ( + testParam = "workspaceagent" + testWorkspaceAgentID = "8a70c576-12dc-42bc-b791-112a32b5bd43" +) + +func TestParseUUID_Valid(t *testing.T) { + t.Parallel() + + rw := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/{workspaceagent}", nil) + + ctx := chi.NewRouteContext() + ctx.URLParams.Add(testParam, testWorkspaceAgentID) + r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx)) + + parsed, ok := parseUUID(rw, r, "workspaceagent") + assert.True(t, ok, "UUID should be parsed") + assert.Equal(t, testWorkspaceAgentID, parsed.String()) +} + +func TestParseUUID_Invalid(t *testing.T) { + t.Parallel() + + rw := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/{workspaceagent}", nil) + + ctx := chi.NewRouteContext() + ctx.URLParams.Add(testParam, "wrong-id") + r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx)) + + _, ok := parseUUID(rw, r, "workspaceagent") + assert.False(t, ok, "UUID should not be parsed") + assert.Equal(t, http.StatusBadRequest, rw.Code) + + var response codersdk.Response + err := json.Unmarshal(rw.Body.Bytes(), &response) + require.NoError(t, err) + assert.Contains(t, response.Message, `Invalid UUID "wrong-id"`) +} diff --git a/site/vite.config.ts b/site/vite.config.ts index 9c0d2f50a76ba..72816177d7675 100644 --- a/site/vite.config.ts +++ b/site/vite.config.ts @@ -37,6 +37,23 @@ export default defineConfig({ changeOrigin: true, target: process.env.CODER_HOST || "http://localhost:3000", secure: process.env.NODE_ENV === "production", + configure: (proxy) => { + // Vite does not catch socket errors, and stops the webserver. + // As /startup-logs endpoint can return HTTP 4xx status, we need to embrace + // Vite with a custom error handler to prevent from quitting. + proxy.on("proxyReqWs", (proxyReq, req, socket) => { + if (process.env.NODE_ENV === "development") { + proxyReq.setHeader( + "origin", + process.env.CODER_HOST || "http://localhost:3000", + ) + } + + socket.on("error", (error) => { + console.error(error) + }) + }) + }, }, "/swagger": { target: process.env.CODER_HOST || "http://localhost:3000",