-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_test.go
73 lines (67 loc) · 1.56 KB
/
request_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
package gss
import "testing"
func TestValidation(t *testing.T) {
r := &RequestData{}
var err error
err = r.Validate()
if err == nil {
t.Error("error should exists")
}
if err.Error() != "require channel" {
t.Error("unexpected error message")
}
r.Channel = "foo"
err = r.Validate()
if err == nil {
t.Error("error should exists")
}
if err.Error() != "require payload" {
t.Error("unexpected error message")
}
r.Payload = "foo"
err = r.Validate()
if err != nil {
t.Error("error should not exists")
}
}
func TestReadFromBytes(t *testing.T) {
d, err := NewRequestDataFromBytes([]byte("hoge{"))
if d != nil {
t.Error("data should not exists")
}
if err == nil {
t.Error("error should exists")
}
if err.Error() != "cannot parse invalid JSON request data" {
t.Error("unexpected error message")
}
d, err = NewRequestDataFromBytes([]byte(`{"users":["hoge","fuga"]}`))
if d != nil {
t.Error("data should not exists")
}
if err == nil {
t.Error("error should exists")
}
if err.Error() != "require channel" {
t.Error("unexpected error message")
}
d, err = NewRequestDataFromBytes([]byte(`{"users":["hoge","fuga"],"channel":"foo","field":"hey","payload":"bar"}`))
if d == nil {
t.Error("data should exists")
}
if err != nil {
t.Error("error should not exists")
}
if d.Channel != "foo" {
t.Error("channel data is wrong")
}
if len(d.Users) != 2 {
t.Error("users length is wrong")
}
if d.Users[0] != "hoge" || d.Users[1] != "fuga" {
t.Error("users data is wrong")
}
if d.Payload.(string) != "bar" {
t.Error("payload data is wrong")
}
}