validate_mutation_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package middleware
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // The accept side of validate.go:45 — `c.ShouldBindWith(&dst, binding.JSON)` must SUCCEED
  10. // for a well-formed JSON body and decode it into the destination struct. If the conditional
  11. // is flipped (err != nil -> err == nil) or the bind call is dropped, a valid body would be
  12. // rejected or the fields would come back zero-valued; both fail these assertions.
  13. func TestBindJSONAndValidate_ValidJSONDecodesAndPasses(t *testing.T) {
  14. var got *sampleBody
  15. r := newRouter(func(c *gin.Context) {
  16. var ok bool
  17. got, ok = BindJSONAndValidate[sampleBody](c)
  18. if !ok {
  19. t.Fatalf("expected ok=true for valid JSON; got false")
  20. }
  21. })
  22. rec := httptest.NewRecorder()
  23. req := httptest.NewRequest(http.MethodPost, "/submit",
  24. strings.NewReader(`{"port":443,"protocol":"vless","tag":"inbound-443"}`))
  25. req.Header.Set("Content-Type", "application/json")
  26. r.ServeHTTP(rec, req)
  27. if got == nil {
  28. t.Fatal("expected decoded struct; got nil")
  29. }
  30. if got.Port != 443 || got.Protocol != "vless" || got.Tag != "inbound-443" {
  31. t.Fatalf("decoded JSON mismatch: %+v", got)
  32. }
  33. }
  34. // The reject side of validate.go:45 — a malformed JSON body must be caught by the bind
  35. // conditional, returning (nil,false) with a parse-error Message and NO validator Issues.
  36. // If the conditional is flipped so malformed input bypasses the bind check, control falls
  37. // through to validate.Struct on a zero-valued struct, which would instead emit validator
  38. // Issues (e.g. rule="required"/"gte"). Asserting empty Issues + non-empty Message pins the
  39. // distinct parse-failure path that line 45 owns.
  40. func TestBindJSONAndValidate_MalformedJSONRejectedWithoutValidatorIssues(t *testing.T) {
  41. r := newRouter(func(c *gin.Context) {
  42. if _, ok := BindJSONAndValidate[sampleBody](c); ok {
  43. t.Fatal("expected ok=false on malformed JSON; got true")
  44. }
  45. })
  46. rec := httptest.NewRecorder()
  47. req := httptest.NewRequest(http.MethodPost, "/submit",
  48. strings.NewReader(`{"port":}`))
  49. req.Header.Set("Content-Type", "application/json")
  50. r.ServeHTTP(rec, req)
  51. msg := decodeMsg(t, rec.Body.String())
  52. if msg.Success {
  53. t.Fatal("expected Success=false on malformed JSON")
  54. }
  55. payload, err := payloadFromObj(msg.Obj)
  56. if err != nil {
  57. t.Fatalf("payload extraction: %v", err)
  58. }
  59. if len(payload.Issues) != 0 {
  60. t.Fatalf("expected empty Issues for a JSON parse error (not validator output); got %+v", payload.Issues)
  61. }
  62. if payload.Message == "" {
  63. t.Fatal("expected non-empty Message describing the JSON parse error")
  64. }
  65. }
  66. // BindJSONAndValidateInto shares the same line-45-style bind conditional (line 57). Cover its
  67. // accept side: a valid JSON body must bind onto the caller-supplied destination and pass,
  68. // overwriting any pre-populated field. A flipped/dropped bind check leaves the destination
  69. // untouched (or returns false), which these assertions catch.
  70. func TestBindJSONAndValidateInto_ValidJSONBindsOntoDestination(t *testing.T) {
  71. dst := &sampleBody{Tag: "preset"}
  72. r := newRouter(func(c *gin.Context) {
  73. if !BindJSONAndValidateInto(c, dst) {
  74. t.Fatal("expected ok=true for valid JSON; got false")
  75. }
  76. })
  77. rec := httptest.NewRecorder()
  78. req := httptest.NewRequest(http.MethodPost, "/submit",
  79. strings.NewReader(`{"port":8443,"protocol":"trojan","tag":"inbound-8443"}`))
  80. req.Header.Set("Content-Type", "application/json")
  81. r.ServeHTTP(rec, req)
  82. if dst.Port != 8443 || dst.Protocol != "trojan" {
  83. t.Fatalf("expected JSON to bind onto destination; got %+v", dst)
  84. }
  85. if dst.Tag != "inbound-8443" {
  86. t.Fatalf("expected payload Tag to overwrite preset; got %q", dst.Tag)
  87. }
  88. }