-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbind_test.go
91 lines (75 loc) · 1.88 KB
/
bind_test.go
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
package pine
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestBindJSON_Success(t *testing.T) {
body := `{"name": "John", "age": 30}`
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
ctx := &Ctx{Request: req}
var data struct {
Name string `json:"name"`
Age int `json:"age"`
}
err := ctx.BindJSON(&data)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if data.Name != "John" || data.Age != 30 {
t.Fatalf("expected name 'John' and age 30, got name '%s' and age %d", data.Name, data.Age)
}
}
func TestBindJSON_InvalidJSON(t *testing.T) {
body := `{"name": "John", "age":}`
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
ctx := &Ctx{Request: req}
var data struct {
Name string `json:"name"`
Age int `json:"age"`
}
err := ctx.BindJSON(&data)
if !errors.Is(err, ErrParse) {
t.Fatalf("expected ErrParse, got %v", err)
}
}
func TestBindParam_Success(t *testing.T) {
ctx := Mock_Ctx()
var id int
err := ctx.BindParam("id", &id)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if id != 42 {
t.Fatalf("expected id to be 42, got %d", id)
}
}
func TestBindParam_NotFound(t *testing.T) {
ctx := Mock_Ctx()
var id int
err := ctx.BindParam("missing", &id)
if !errors.Is(err, ErrValidation) {
t.Fatalf("expected ErrValidation, got %v", err)
}
}
func TestBindQuery_Success(t *testing.T) {
ctx := Mock_Ctx()
var value string
err := ctx.BindQuery("query", &value)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if value != "queryValue" {
t.Fatalf("expected query value to be 'queryValue', got '%s'", value)
}
}
func TestBindQuery_NotFound(t *testing.T) {
ctx := Mock_Ctx()
var value string
err := ctx.BindQuery("missing", &value)
if !errors.Is(err, ErrValidation) {
t.Fatalf("expected ErrValidation, got %v", err)
}
}