Skip to content

Commit 71bbdeb

Browse files
committed
RequestDataでfieldを指定する形にする
1 parent 8cd733b commit 71bbdeb

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

notifier_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestNotificationHandler(t *testing.T) {
9494
notifyTestCase{
9595
Label: "success",
9696
Method: "POST",
97-
Body: `{"channel":"foo","users":["hoge","fuga"],"payload":{"aaa":"iii","uu":{"ee":"oo"}}}`,
97+
Body: `{"channel":"foo","users":["hoge","fuga"],"field":"foo","payload":{"aaa":"iii","uu":{"ee":"oo"}}}`,
9898
ContentType: "application/json",
9999
StatusCode: http.StatusOK,
100100
ResponseSuccess: true,

request.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
type RequestData struct {
99
Users []string `json:"users"`
10+
Field string `json:"field"`
1011
Channel string `json:"channel"`
1112
Payload interface{} `json:"payload"`
1213
}
@@ -18,6 +19,9 @@ func (d *RequestData) Validate() error {
1819
if d.Payload == nil {
1920
return errors.New("require payload")
2021
}
22+
if d.Field == "" {
23+
return errors.New("require field")
24+
}
2125
return nil
2226
}
2327

request_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ func TestValidation(t *testing.T) {
2424

2525
r.Payload = "foo"
2626
err = r.Validate()
27-
if err != nil {
27+
if err == nil {
28+
t.Error("error should exists")
29+
}
30+
if err.Error() != "require field" {
31+
t.Error("unexpected error message")
32+
}
33+
34+
r.Field = "hoge"
35+
if err == nil {
2836
t.Error("error should not exists")
2937
}
3038
}
@@ -51,7 +59,7 @@ func TestReadFromBytes(t *testing.T) {
5159
t.Error("unexpected error message")
5260
}
5361

54-
d, err = NewRequestDataFromBytes([]byte(`{"users":["hoge","fuga"],"channel":"foo","payload":"bar"}`))
62+
d, err = NewRequestDataFromBytes([]byte(`{"users":["hoge","fuga"],"channel":"foo","field":"hey","payload":"bar"}`))
5563
if d == nil {
5664
t.Error("data should exists")
5765
}
@@ -70,4 +78,7 @@ func TestReadFromBytes(t *testing.T) {
7078
if d.Payload.(string) != "bar" {
7179
t.Error("payload data is wrong")
7280
}
81+
if d.Field != "hey" {
82+
t.Error("field data is wrong")
83+
}
7384
}

0 commit comments

Comments
 (0)