Перенести WithUser в httpx

This commit is contained in:
2026-10-06 14:49:19 +03:00
parent f42bf97b3a
commit 6720108793
6 changed files with 60 additions and 48 deletions
+25
View File
@@ -282,6 +282,31 @@ func TestUserIDRoundTripAndAccessLogAttribution(t *testing.T) {
}
}
func TestWithUserPassesTheIdentifiedCaller(t *testing.T) {
actor := uuid.MustParse("22222222-2222-4222-8222-222222222222")
e := newEngine(t, nil)
e.GET("/actor",
func(c *gin.Context) { httpx.SetUserID(c, actor) },
httpx.WithUser(func(c *gin.Context, got uuid.UUID) {
httpx.JSON(c, http.StatusOK, gin.H{"id": got.String()})
}))
rec, _ := do(t, e, http.MethodGet, "/actor", "", nil)
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), actor.String()) {
t.Fatalf("handler did not receive the caller: %d %s", rec.Code, rec.Body.String())
}
}
// Reaching WithUser without an authentication middleware in front is a
// wiring mistake, so it must not be blamed on the client with a 401.
func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
e := newEngine(t, nil)
e.GET("/actor", httpx.WithUser(func(c *gin.Context, _ uuid.UUID) {
httpx.JSON(c, http.StatusOK, gin.H{})
}))
rec, eb := do(t, e, http.MethodGet, "/actor", "", nil)
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
}
func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any {
t.Helper()
var entry map[string]any