Skip to content

Commit d5289c9

Browse files
authored
Fix url_verification error with the Flask adapter (#543)
1 parent e8556c1 commit d5289c9

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

examples/app_authorize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ def event_test(body, say, logger):
3939
# pip install slack_bolt
4040
# export SLACK_SIGNING_SECRET=***
4141
# export MY_TOKEN=xoxb-***
42-
# python app.py
42+
# python app_authorize.py

examples/async_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ async def command(ack, body):
3232
# pip install slack_bolt
3333
# export SLACK_SIGNING_SECRET=***
3434
# export SLACK_BOT_TOKEN=xoxb-***
35-
# python app.py
35+
# python async_app.py

slack_bolt/adapter/flask/handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def to_bolt_request(req: Request) -> BoltRequest:
1717
def to_flask_response(bolt_resp: BoltResponse) -> Response:
1818
resp: Response = make_response(bolt_resp.body, bolt_resp.status)
1919
for k, values in bolt_resp.headers.items():
20+
if k.lower() == "content-type" and resp.headers.get("content-type") is not None:
21+
# Remove the one set by Flask
22+
resp.headers.pop("content-type")
2023
for v in values:
2124
resp.headers.add_header(k, v)
2225
return resp

tests/adapter_tests/flask/test_flask.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def endpoint():
9999
headers=self.build_headers(timestamp, body),
100100
)
101101
assert rv.status_code == 200
102+
assert rv.headers.get("content-type") == "text/plain;charset=utf-8"
102103
assert_auth_test_count(self, 1)
103104

104105
def test_shortcuts(self):
@@ -142,6 +143,7 @@ def endpoint():
142143
headers=self.build_headers(timestamp, body),
143144
)
144145
assert rv.status_code == 200
146+
assert rv.headers.get("content-type") == "text/plain;charset=utf-8"
145147
assert_auth_test_count(self, 1)
146148

147149
def test_commands(self):
@@ -185,6 +187,7 @@ def endpoint():
185187
headers=self.build_headers(timestamp, body),
186188
)
187189
assert rv.status_code == 200
190+
assert rv.headers.get("content-type") == "text/plain;charset=utf-8"
188191
assert_auth_test_count(self, 1)
189192

190193
def test_oauth(self):
@@ -205,4 +208,35 @@ def endpoint():
205208

206209
with flask_app.test_client() as client:
207210
rv = client.get("/slack/install")
211+
assert rv.headers.get("content-type") == "text/html; charset=utf-8"
208212
assert rv.status_code == 200
213+
214+
def test_url_verification(self):
215+
app = App(
216+
client=self.web_client,
217+
signing_secret=self.signing_secret,
218+
)
219+
220+
input = {
221+
"token": "Jhj5dZrVaK7ZwHHjRyZWjbDl",
222+
"challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P",
223+
"type": "url_verification",
224+
}
225+
226+
timestamp, body = str(int(time())), f"payload={quote(json.dumps(input))}"
227+
228+
flask_app = Flask(__name__)
229+
230+
@flask_app.route("/slack/events", methods=["POST"])
231+
def endpoint():
232+
return SlackRequestHandler(app).handle(request)
233+
234+
with flask_app.test_client() as client:
235+
rv = client.post(
236+
"/slack/events",
237+
data=body,
238+
headers=self.build_headers(timestamp, body),
239+
)
240+
assert rv.status_code == 200
241+
assert rv.headers.get("content-type") == "application/json;charset=utf-8"
242+
assert_auth_test_count(self, 1)

0 commit comments

Comments
 (0)