-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_param_test.go
69 lines (56 loc) · 1.37 KB
/
http_param_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
package poteto
import (
"errors"
"testing"
"bou.ke/monkey"
"github.com/goccy/go-json"
"github.com/poteto-go/poteto/constant"
)
func TestAddAndGetParam(t *testing.T) {
hp := NewHttpParam()
pu := ParamUnit{"key", "value"}
hp.AddParam(constant.PARAM_TYPE_PATH, pu)
tests := []struct {
name string
key string
expected_val string
expected_ok bool
}{
{"test ok case", "key", "value", true},
{"test unexpected", "unexpected", "", false},
}
for _, it := range tests {
t.Run(it.name, func(t *testing.T) {
value, ok := hp.GetParam(constant.PARAM_TYPE_PATH, it.key)
if value != it.expected_val {
t.Errorf("Don't Work")
}
if ok != it.expected_ok {
t.Errorf("Unmatched")
}
})
}
}
func TestJsonSerializeHttpParam(t *testing.T) {
hp := NewHttpParam()
hp.AddParam(constant.PARAM_TYPE_PATH, ParamUnit{key: "key", value: "value"})
expected := `{"path":{"key":"value"},"query":{}}`
serialized, _ := hp.JsonSerialize()
if string(serialized) != expected {
t.Errorf(
"Unmatched actual(%s) -> expected(%s)",
string(serialized),
expected,
)
}
}
func TestJsonSerializeHttpHandleError(t *testing.T) {
defer monkey.UnpatchAll()
hp := NewHttpParam()
monkey.Patch(json.Marshal, func(v any) ([]byte, error) {
return []byte(""), errors.New("error")
})
if _, err := hp.JsonSerialize(); err == nil {
t.Errorf("Unmatched")
}
}