Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions slack_bolt/request/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ def parse_body(body: str, content_type: Optional[str]) -> Dict[str, Any]:
if (content_type is not None and content_type == "application/json") or body.startswith("{"):
return json.loads(body)
else:
if "payload" in body: # This is not JSON format yet
params = dict(parse_qsl(body, keep_blank_values=True))
payload = params.get("payload")
if payload is not None:
return json.loads(payload)
else:
return {}
else:
return dict(parse_qsl(body, keep_blank_values=True))
params = dict(parse_qsl(body, keep_blank_values=True))
payload = params.get("payload")
if payload is not None: # This is not JSON format yet
return json.loads(payload)
return params


def extract_is_enterprise_install(payload: Dict[str, Any]) -> Optional[bool]:
Expand Down
20 changes: 20 additions & 0 deletions tests/scenario_tests/test_slash_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ def command_handler(ack):
assert response.body == """{"error": "invalid request"}"""
assert command_called is False

def test_text_containing_payload(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
received_text = None

def command_handler(ack, command):
nonlocal received_text
received_text = command["text"]
ack()

app.command("/hello-world")(command_handler)

timestamp, body = str(int(time())), slash_command_body.replace("&text=Hi", "&text=show+payload+size")
request = BoltRequest(body=body, headers=self.build_headers(timestamp, body))
response = app.dispatch(request)
assert response.status == 200
assert received_text == "show payload size"


slash_command_body = (
"token=verification_token"
Expand Down
21 changes: 21 additions & 0 deletions tests/scenario_tests_async/test_slash_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ async def command_handler(ack):
assert response.body == """{"error": "invalid request"}"""
assert command_called is False

@pytest.mark.asyncio
async def test_text_containing_payload(self):
app = AsyncApp(
client=self.web_client,
signing_secret=self.signing_secret,
)
received_text = None

async def command_handler(ack, command):
nonlocal received_text
received_text = command["text"]
await ack()

app.command("/hello-world")(command_handler)

timestamp, body = str(int(time())), slash_command_body.replace("&text=Hi", "&text=show+payload+size")
request = AsyncBoltRequest(body=body, headers=self.build_headers(timestamp, body))
response = await app.async_dispatch(request)
assert response.status == 200
assert received_text == "show payload size"


slash_command_body = (
"token=verification_token"
Expand Down
17 changes: 17 additions & 0 deletions tests/slack_bolt/request/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
extract_user_id,
extract_team_id,
extract_enterprise_id,
parse_body,
parse_query,
extract_is_enterprise_install,
extract_actor_enterprise_id,
Expand Down Expand Up @@ -589,6 +590,22 @@ def test_actor_user_id(self):
user_id = extract_actor_user_id(req)
assert user_id is None

def test_parse_body_json(self):
assert parse_body('{"type": "event_callback"}', "application/json") == {"type": "event_callback"}

def test_parse_body_payload_parameter(self):
body = "payload=%7B%22type%22%3A%22block_actions%22%7D"
assert parse_body(body, "application/x-www-form-urlencoded") == {"type": "block_actions"}

def test_parse_body_form_value_containing_payload(self):
# "payload" appearing in a value must not be taken for the payload parameter
body = "command=%2Fdeploy&text=show+payload+size&user_id=W111"
assert parse_body(body, "application/x-www-form-urlencoded") == {
"command": "/deploy",
"text": "show payload size",
"user_id": "W111",
}

def test_parse_query(self):
expected = {"foo": ["bar"], "baz": ["123"]}

Expand Down