-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
63 lines (51 loc) · 1.54 KB
/
Copy pathutils_test.go
File metadata and controls
63 lines (51 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package fiberkit
import (
"encoding/json"
"errors"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v3"
)
type validationHelperPayload struct {
Name string `validate:"required"`
}
func TestValidateInputAllowsNilValue(t *testing.T) {
if err := validateInput(nil); err != nil {
t.Fatalf("validateInput(nil) error = %v, want nil", err)
}
}
func TestValidateInputAllowsNonStructValue(t *testing.T) {
if err := validateInput("plain-string"); err != nil {
t.Fatalf("validateInput(non-struct) error = %v, want nil", err)
}
}
func TestValidateInputSupportsStructPointer(t *testing.T) {
payload := &validationHelperPayload{Name: "ok"}
if err := validateInput(payload); err != nil {
t.Fatalf("validateInput(pointer) error = %v, want nil", err)
}
}
func TestInvalidValidationOmitsDetailsForGenericError(t *testing.T) {
app := fiber.New()
app.Get("/", func(ctx fiber.Ctx) error {
return invalidValidation(ctx, errors.New("boom"))
})
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("app.Test() error = %v", err)
}
if resp.StatusCode != fiber.StatusBadRequest {
t.Fatalf("status = %d, want %d", resp.StatusCode, fiber.StatusBadRequest)
}
var got map[string]any
if err := json.NewDecoder(resp.Body).Decode(&got); err != nil {
t.Fatalf("decode response: %v", err)
}
if got["error"] != "validation failed" {
t.Fatalf("error = %v, want %q", got["error"], "validation failed")
}
if _, exists := got["details"]; exists {
t.Fatalf("details exists = true, want false")
}
}