Skip to content

Commit 99ac7f0

Browse files
authored
Fix destination field (#187)
* Set None if 'destination' is not in the webhook request
1 parent 1386ecf commit 99ac7f0

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

linebot/webhook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def parse(self, body, signature, as_payload=False):
172172
LOGGER.warn('Unknown event type. type=' + event_type)
173173

174174
if as_payload:
175-
return WebhookPayload(events=events, destination=body_json['destination'])
175+
return WebhookPayload(events=events, destination=body_json.get('destination'))
176176
else:
177177
return events
178178

tests/test_webhook.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ def test_validate(self):
4848

4949

5050
class TestWebhookParser(unittest.TestCase):
51+
def setUp(self):
52+
parser = WebhookParser('channel_secret')
53+
# mock
54+
parser.signature_validator.validate = lambda a, b: True
55+
self.parser = parser
56+
5157
def test_parse(self):
5258
file_dir = os.path.dirname(__file__)
5359
webhook_sample_json_path = os.path.join(file_dir, 'text', 'webhook.json')
5460
with open(webhook_sample_json_path) as fp:
5561
body = fp.read()
5662

57-
parser = WebhookParser('channel_secret')
58-
# mock
59-
parser.signature_validator.validate = lambda a, b: True
60-
61-
events = parser.parse(body, 'channel_secret')
63+
events = self.parser.parse(body, 'channel_secret')
6264

6365
# MessageEvent, SourceUser, TextMessage
6466
self.assertIsInstance(events[0], MessageEvent)
@@ -387,6 +389,45 @@ def test_parse(self):
387389
self.assertIsInstance(events[24].things.result.action_results[1], ActionResult)
388390
self.assertEqual(events[24].things.result.action_results[1].type, 'void')
389391

392+
def test_parse_webhook_req_without_destination(self):
393+
body = """
394+
{
395+
"events": [
396+
{
397+
"replyToken": "00000000000000000000000000000000",
398+
"type": "message",
399+
"timestamp": 1561099010135,
400+
"source": {
401+
"type": "user",
402+
"userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
403+
},
404+
"message": {
405+
"id": "100001",
406+
"type": "text",
407+
"text": "Hello, world"
408+
}
409+
},
410+
{
411+
"replyToken": "ffffffffffffffffffffffffffffffff",
412+
"type": "message",
413+
"timestamp": 1561099010135,
414+
"source": {
415+
"type": "user",
416+
"userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
417+
},
418+
"message": {
419+
"id": "100002",
420+
"type": "sticker",
421+
"packageId": "1",
422+
"stickerId": "1"
423+
}
424+
}
425+
]
426+
}
427+
"""
428+
payload = self.parser.parse(body=body, signature='channel_secret', as_payload=True)
429+
self.assertEqual(None, payload.destination)
430+
390431

391432
class TestWebhookHandler(unittest.TestCase):
392433
def setUp(self):

0 commit comments

Comments
 (0)