Skip to content

Commit

Permalink
Add WithNotificationDisabled() to push/reply/multicast message API
Browse files Browse the repository at this point in the history
- support notificationDisabled, which is added to API recently.
- add omitempty to `notificationDisabled` since it's optional
  • Loading branch information
clsung committed Jun 18, 2019
1 parent 78e8bff commit 1b9cd33
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 27 deletions.
84 changes: 57 additions & 27 deletions linebot/send_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
// PushMessage method
func (client *Client) PushMessage(to string, messages ...SendingMessage) *PushMessageCall {
return &PushMessageCall{
c: client,
to: to,
messages: messages,
c: client,
to: to,
messages: messages,
notificationDisabled: false,
}
}

Expand All @@ -35,8 +36,9 @@ type PushMessageCall struct {
c *Client
ctx context.Context

to string
messages []SendingMessage
to string
messages []SendingMessage
notificationDisabled bool
}

// WithContext method
Expand All @@ -45,14 +47,22 @@ func (call *PushMessageCall) WithContext(ctx context.Context) *PushMessageCall {
return call
}

// WithNotificationDisabled method will disable push notification
func (call *PushMessageCall) WithNotificationDisabled() *PushMessageCall {
call.notificationDisabled = true
return call
}

func (call *PushMessageCall) encodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
To string `json:"to"`
Messages []SendingMessage `json:"messages"`
To string `json:"to"`
Messages []SendingMessage `json:"messages"`
NotificationDisabled bool `json:"notificationDisabled,omitempty"`
}{
To: call.to,
Messages: call.messages,
To: call.to,
Messages: call.messages,
NotificationDisabled: call.notificationDisabled,
})
}

Expand All @@ -73,9 +83,10 @@ func (call *PushMessageCall) Do() (*BasicResponse, error) {
// ReplyMessage method
func (client *Client) ReplyMessage(replyToken string, messages ...SendingMessage) *ReplyMessageCall {
return &ReplyMessageCall{
c: client,
replyToken: replyToken,
messages: messages,
c: client,
replyToken: replyToken,
messages: messages,
notificationDisabled: false,
}
}

Expand All @@ -84,8 +95,9 @@ type ReplyMessageCall struct {
c *Client
ctx context.Context

replyToken string
messages []SendingMessage
replyToken string
messages []SendingMessage
notificationDisabled bool
}

// WithContext method
Expand All @@ -94,14 +106,22 @@ func (call *ReplyMessageCall) WithContext(ctx context.Context) *ReplyMessageCall
return call
}

// WithNotificationDisabled method will disable push notification
func (call *ReplyMessageCall) WithNotificationDisabled() *ReplyMessageCall {
call.notificationDisabled = true
return call
}

func (call *ReplyMessageCall) encodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
ReplyToken string `json:"replyToken"`
Messages []SendingMessage `json:"messages"`
ReplyToken string `json:"replyToken"`
Messages []SendingMessage `json:"messages"`
NotificationDisabled bool `json:"notificationDisabled,omitempty"`
}{
ReplyToken: call.replyToken,
Messages: call.messages,
ReplyToken: call.replyToken,
Messages: call.messages,
NotificationDisabled: call.notificationDisabled,
})
}

Expand All @@ -122,9 +142,10 @@ func (call *ReplyMessageCall) Do() (*BasicResponse, error) {
// Multicast method
func (client *Client) Multicast(to []string, messages ...SendingMessage) *MulticastCall {
return &MulticastCall{
c: client,
to: to,
messages: messages,
c: client,
to: to,
messages: messages,
notificationDisabled: false,
}
}

Expand All @@ -133,8 +154,9 @@ type MulticastCall struct {
c *Client
ctx context.Context

to []string
messages []SendingMessage
to []string
messages []SendingMessage
notificationDisabled bool
}

// WithContext method
Expand All @@ -143,14 +165,22 @@ func (call *MulticastCall) WithContext(ctx context.Context) *MulticastCall {
return call
}

// WithNotificationDisabled method will disable push notification
func (call *MulticastCall) WithNotificationDisabled() *MulticastCall {
call.notificationDisabled = true
return call
}

func (call *MulticastCall) encodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
To []string `json:"to"`
Messages []SendingMessage `json:"messages"`
To []string `json:"to"`
Messages []SendingMessage `json:"messages"`
NotificationDisabled bool `json:"notificationDisabled,omitempty"`
}{
To: call.to,
Messages: call.messages,
To: call.to,
Messages: call.messages,
NotificationDisabled: call.notificationDisabled,
})
}

Expand Down
108 changes: 108 additions & 0 deletions linebot/send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,114 @@ func TestMulticastMessagesWithContext(t *testing.T) {
expectCtxDeadlineExceed(ctx, err, t)
}

func TestMessagesWithNotificationDisabled(t *testing.T) {
type testMethod interface {
Do() (*BasicResponse, error)
}
var toUserIDs = []string{
"U0cc15697597f61dd8b01cea8b027050e",
"U38ecbecfade326557b6971140741a4a6",
}
type want struct {
RequestBody []byte
Response *BasicResponse
Error error
}
var testCases = []struct {
Label string
TestMethod testMethod
Messages []SendingMessage
Response []byte
ResponseCode int
Want want
}{
{
Label: "A text message for Push Message",
TestMethod: new(PushMessageCall),
Messages: []SendingMessage{NewTextMessage("Hello, world")},
ResponseCode: 200,
Response: []byte(`{}`),
Want: want{
RequestBody: []byte(`{"to":"U0cc15697597f61dd8b01cea8b027050e","messages":[{"type":"text","text":"Hello, world"}],"notificationDisabled":true}` + "\n"),
Response: &BasicResponse{},
},
},
{
Label: "A text message for Reply Message",
TestMethod: new(ReplyMessageCall),
Messages: []SendingMessage{NewTextMessage("Hello, world")},
ResponseCode: 200,
Response: []byte(`{}`),
Want: want{
RequestBody: []byte(`{"replyToken":"U0cc15697597f61dd8b01cea8b027050e","messages":[{"type":"text","text":"Hello, world"}],"notificationDisabled":true}` + "\n"),
Response: &BasicResponse{},
},
},
{
Label: "A text message for Multicast",
TestMethod: new(MulticastCall),
Messages: []SendingMessage{NewTextMessage("Hello, world")},
ResponseCode: 200,
Response: []byte(`{}`),
Want: want{
RequestBody: []byte(`{"to":["U0cc15697597f61dd8b01cea8b027050e","U38ecbecfade326557b6971140741a4a6"],"messages":[{"type":"text","text":"Hello, world"}],"notificationDisabled":true}` + "\n"),
Response: &BasicResponse{},
},
},
}

var currentTestIdx int
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if r.Method != http.MethodPost {
t.Errorf("Method %s; want %s", r.Method, http.MethodPost)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
tc := testCases[currentTestIdx]
if !reflect.DeepEqual(body, tc.Want.RequestBody) {
t.Errorf("RequestBody %s; want %s", body, tc.Want.RequestBody)
}
w.WriteHeader(tc.ResponseCode)
w.Write(tc.Response)
}))
defer server.Close()
client, err := mockClient(server)
if err != nil {
t.Fatal(err)
}
var res *BasicResponse
for i, tc := range testCases {
currentTestIdx = i
t.Run(strconv.Itoa(i)+"/"+tc.Label, func(t *testing.T) {
switch tc.TestMethod.(type) {
case *PushMessageCall:
res, err = client.PushMessage(toUserIDs[0], tc.Messages...).WithNotificationDisabled().Do()
case *ReplyMessageCall: // use toUserIDs as replyToken because it doesn't matter
res, err = client.ReplyMessage(toUserIDs[0], tc.Messages...).WithNotificationDisabled().Do()
case *MulticastCall:
res, err = client.Multicast(toUserIDs, tc.Messages...).WithNotificationDisabled().Do()
}
if tc.Want.Error != nil {
if !reflect.DeepEqual(err, tc.Want.Error) {
t.Errorf("Error %d %v; want %v", i, err, tc.Want.Error)
}
} else {
if err != nil {
t.Error(err)
}
}
if tc.Want.Response != nil {
if !reflect.DeepEqual(res, tc.Want.Response) {
t.Errorf("Response %d %v; want %v", i, res, tc.Want.Response)
}
}
})
}
}

func BenchmarkPushMessages(b *testing.B) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
Expand Down

0 comments on commit 1b9cd33

Please sign in to comment.