Repository navigation
Expand file tree
/
Copy pathrequest_test.go
More file actions
117 lines (101 loc) · 2.77 KB
/
Copy pathrequest_test.go
File metadata and controls
117 lines (101 loc) · 2.77 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package axon
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDecodeJSON_Success(t *testing.T) {
type req struct {
Name string `json:"name"`
Age int `json:"age"`
}
body := strings.NewReader(`{"name":"alice","age":30}`)
r := httptest.NewRequest(http.MethodPost, "/", body)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
got, ok := DecodeJSON[req](w, r)
if !ok {
t.Fatalf("expected ok=true, response: %s", w.Body.String())
}
if got.Name != "alice" || got.Age != 30 {
t.Errorf("unexpected result: %+v", got)
}
}
func TestDecodeJSON_InvalidJSON(t *testing.T) {
body := strings.NewReader(`{invalid}`)
r := httptest.NewRequest(http.MethodPost, "/", body)
w := httptest.NewRecorder()
_, ok := DecodeJSON[map[string]string](w, r)
if ok {
t.Fatal("expected ok=false for invalid JSON")
}
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestDecodeJSON_EmptyBody(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", nil)
w := httptest.NewRecorder()
_, ok := DecodeJSON[map[string]string](w, r)
if ok {
t.Fatal("expected ok=false for empty body")
}
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
func TestDecodeJSON_OversizedBody(t *testing.T) {
// Default limit is 1MB; send 2MB
big := strings.Repeat("x", 2<<20)
body := strings.NewReader(`{"name":"` + big + `"}`)
r := httptest.NewRequest(http.MethodPost, "/", body)
w := httptest.NewRecorder()
type req struct {
Name string `json:"name"`
}
_, ok := DecodeJSON[req](w, r)
if ok {
t.Fatal("expected ok=false for oversized body")
}
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
}
type validReq struct {
Name string `json:"name"`
}
func (v validReq) Validate() error {
if v.Name == "" {
return errors.New("name is required")
}
return nil
}
func TestDecodeJSON_Validatable_Pass(t *testing.T) {
body := strings.NewReader(`{"name":"alice"}`)
r := httptest.NewRequest(http.MethodPost, "/", body)
w := httptest.NewRecorder()
got, ok := DecodeJSON[validReq](w, r)
if !ok {
t.Fatalf("expected ok=true, response: %s", w.Body.String())
}
if got.Name != "alice" {
t.Errorf("unexpected name: %s", got.Name)
}
}
func TestDecodeJSON_Validatable_Fail(t *testing.T) {
body := strings.NewReader(`{"name":""}`)
r := httptest.NewRequest(http.MethodPost, "/", body)
w := httptest.NewRecorder()
_, ok := DecodeJSON[validReq](w, r)
if ok {
t.Fatal("expected ok=false for failed validation")
}
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("expected 422, got %d", w.Code)
}
if !strings.Contains(w.Body.String(), "name is required") {
t.Errorf("expected validation message in body, got: %s", w.Body.String())
}
}