mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
fix: vite fatals on receiving HTTP4xx (#7306)
* fix: vite fatals on receiving HTTP4xx * tune Vite * fmt * rewrite * fmt
This commit is contained in:
@ -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
|
||||
|
55
coderd/httpmw/httpmw_internal_test.go
Normal file
55
coderd/httpmw/httpmw_internal_test.go
Normal file
@ -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"`)
|
||||
}
|
Reference in New Issue
Block a user