|
1 | | -import sys |
2 | 1 | import unittest |
3 | 2 |
|
4 | 3 | from mock import Mock |
5 | 4 |
|
6 | 5 | from zappa.handler import LambdaHandler |
7 | 6 | from zappa.utilities import merge_headers |
8 | 7 |
|
| 8 | +from .utils import is_base64 |
| 9 | + |
9 | 10 |
|
10 | 11 | def no_args(): |
11 | 12 | return |
@@ -223,6 +224,188 @@ def test_exception_handler_on_web_request(self): |
223 | 224 | self.assertEqual(response["statusCode"], 500) |
224 | 225 | mocked_exception_handler.assert_called() |
225 | 226 |
|
| 227 | + def test_wsgi_script_binary_support_with_content_encoding(self): |
| 228 | + """ |
| 229 | + Ensure that response body is base64 encoded when BINARY_SUPPORT is enabled and Content-Encoding header is present. |
| 230 | + """ |
| 231 | + lh = LambdaHandler("tests.test_binary_support_settings") |
| 232 | + |
| 233 | + text_plain_event = { |
| 234 | + "body": "", |
| 235 | + "resource": "/{proxy+}", |
| 236 | + "requestContext": {}, |
| 237 | + "queryStringParameters": {}, |
| 238 | + "headers": { |
| 239 | + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", |
| 240 | + }, |
| 241 | + "pathParameters": {"proxy": "return/request/url"}, |
| 242 | + "httpMethod": "GET", |
| 243 | + "stageVariables": {}, |
| 244 | + "path": "/content_encoding_header_json1", |
| 245 | + } |
| 246 | + |
| 247 | + # A likely scenario is that the application would be gzip compressing some json response. That's checked first. |
| 248 | + response = lh.handler(text_plain_event, None) |
| 249 | + |
| 250 | + self.assertEqual(response["statusCode"], 200) |
| 251 | + self.assertIn("isBase64Encoded", response) |
| 252 | + self.assertTrue(is_base64(response["body"])) |
| 253 | + |
| 254 | + # We also verify that some unknown mimetype with a Content-Encoding also encodes to b64. This route serves |
| 255 | + # bytes in the response. |
| 256 | + |
| 257 | + text_arbitrary_event = { |
| 258 | + **text_plain_event, |
| 259 | + **{"path": "/content_encoding_header_textarbitrary1"}, |
| 260 | + } |
| 261 | + |
| 262 | + response = lh.handler(text_arbitrary_event, None) |
| 263 | + |
| 264 | + self.assertEqual(response["statusCode"], 200) |
| 265 | + self.assertIn("isBase64Encoded", response) |
| 266 | + self.assertTrue(is_base64(response["body"])) |
| 267 | + |
| 268 | + # This route is similar to the above, but it serves its response as text and not bytes. That the response |
| 269 | + # isn't bytes shouldn't matter because it still has a Content-Encoding header. |
| 270 | + |
| 271 | + application_json_event = { |
| 272 | + **text_plain_event, |
| 273 | + **{"path": "/content_encoding_header_textarbitrary2"}, |
| 274 | + } |
| 275 | + |
| 276 | + response = lh.handler(application_json_event, None) |
| 277 | + |
| 278 | + self.assertEqual(response["statusCode"], 200) |
| 279 | + self.assertIn("isBase64Encoded", response) |
| 280 | + self.assertTrue(is_base64(response["body"])) |
| 281 | + |
| 282 | + def test_wsgi_script_binary_support_without_content_encoding_edgecases( |
| 283 | + self, |
| 284 | + ): |
| 285 | + """ |
| 286 | + Ensure zappa response bodies are NOT base64 encoded when BINARY_SUPPORT is enabled and the mimetype is "application/json" or starts with "text/". |
| 287 | + """ |
| 288 | + |
| 289 | + lh = LambdaHandler("tests.test_binary_support_settings") |
| 290 | + |
| 291 | + text_plain_event = { |
| 292 | + "body": "", |
| 293 | + "resource": "/{proxy+}", |
| 294 | + "requestContext": {}, |
| 295 | + "queryStringParameters": {}, |
| 296 | + "headers": { |
| 297 | + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", |
| 298 | + }, |
| 299 | + "pathParameters": {"proxy": "return/request/url"}, |
| 300 | + "httpMethod": "GET", |
| 301 | + "stageVariables": {}, |
| 302 | + "path": "/textplain_mimetype_response1", |
| 303 | + } |
| 304 | + |
| 305 | + for path in [ |
| 306 | + "/textplain_mimetype_response1", # text/plain mimetype should not be turned to base64 |
| 307 | + "/textarbitrary_mimetype_response1", # text/arbitrary mimetype should not be turned to base64 |
| 308 | + "/json_mimetype_response1", # application/json mimetype should not be turned to base64 |
| 309 | + ]: |
| 310 | + event = {**text_plain_event, "path": path} |
| 311 | + response = lh.handler(event, None) |
| 312 | + |
| 313 | + self.assertEqual(response["statusCode"], 200) |
| 314 | + self.assertNotIn("isBase64Encoded", response) |
| 315 | + self.assertFalse(is_base64(response["body"])) |
| 316 | + |
| 317 | + def test_wsgi_script_binary_support_without_content_encoding( |
| 318 | + self, |
| 319 | + ): |
| 320 | + """ |
| 321 | + Ensure zappa response bodies are base64 encoded when BINARY_SUPPORT is enabled and Content-Encoding is absent. |
| 322 | + """ |
| 323 | + |
| 324 | + lh = LambdaHandler("tests.test_binary_support_settings") |
| 325 | + |
| 326 | + text_plain_event = { |
| 327 | + "body": "", |
| 328 | + "resource": "/{proxy+}", |
| 329 | + "requestContext": {}, |
| 330 | + "queryStringParameters": {}, |
| 331 | + "headers": { |
| 332 | + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", |
| 333 | + }, |
| 334 | + "pathParameters": {"proxy": "return/request/url"}, |
| 335 | + "httpMethod": "GET", |
| 336 | + "stageVariables": {}, |
| 337 | + "path": "/textplain_mimetype_response1", |
| 338 | + } |
| 339 | + |
| 340 | + for path in [ |
| 341 | + "/arbitrarybinary_mimetype_response1", |
| 342 | + "/arbitrarybinary_mimetype_response2", |
| 343 | + ]: |
| 344 | + event = {**text_plain_event, "path": path} |
| 345 | + response = lh.handler(event, None) |
| 346 | + |
| 347 | + self.assertEqual(response["statusCode"], 200) |
| 348 | + self.assertIn("isBase64Encoded", response) |
| 349 | + self.assertTrue(is_base64(response["body"])) |
| 350 | + |
| 351 | + def test_wsgi_script_binary_support_userdefined_additional_text_mimetypes__defined( |
| 352 | + self, |
| 353 | + ): |
| 354 | + """ |
| 355 | + Ensure zappa response bodies are NOT base64 encoded when BINARY_SUPPORT is True, and additional_text_mimetypes are defined |
| 356 | + """ |
| 357 | + lh = LambdaHandler("tests.test_binary_support_additional_text_mimetypes_settings") |
| 358 | + expected_additional_mimetypes = ["application/vnd.oai.openapi"] |
| 359 | + self.assertEqual(lh.settings.ADDITIONAL_TEXT_MIMETYPES, expected_additional_mimetypes) |
| 360 | + |
| 361 | + event = { |
| 362 | + "body": "", |
| 363 | + "resource": "/{proxy+}", |
| 364 | + "requestContext": {}, |
| 365 | + "queryStringParameters": {}, |
| 366 | + "headers": { |
| 367 | + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", |
| 368 | + }, |
| 369 | + "pathParameters": {"proxy": "return/request/url"}, |
| 370 | + "httpMethod": "GET", |
| 371 | + "stageVariables": {}, |
| 372 | + "path": "/userdefined_additional_mimetype_response1", |
| 373 | + } |
| 374 | + |
| 375 | + response = lh.handler(event, None) |
| 376 | + |
| 377 | + self.assertEqual(response["statusCode"], 200) |
| 378 | + self.assertNotIn("isBase64Encoded", response) |
| 379 | + self.assertFalse(is_base64(response["body"])) |
| 380 | + |
| 381 | + def test_wsgi_script_binary_support_userdefined_additional_text_mimetypes__undefined( |
| 382 | + self, |
| 383 | + ): |
| 384 | + """ |
| 385 | + Ensure zappa response bodies are base64 encoded when BINARY_SUPPORT is True and mimetype not defined in additional_text_mimetypes |
| 386 | + """ |
| 387 | + lh = LambdaHandler("tests.test_binary_support_settings") |
| 388 | + |
| 389 | + event = { |
| 390 | + "body": "", |
| 391 | + "resource": "/{proxy+}", |
| 392 | + "requestContext": {}, |
| 393 | + "queryStringParameters": {}, |
| 394 | + "headers": { |
| 395 | + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", |
| 396 | + }, |
| 397 | + "pathParameters": {"proxy": "return/request/url"}, |
| 398 | + "httpMethod": "GET", |
| 399 | + "stageVariables": {}, |
| 400 | + "path": "/userdefined_additional_mimetype_response1", |
| 401 | + } |
| 402 | + |
| 403 | + response = lh.handler(event, None) |
| 404 | + |
| 405 | + self.assertEqual(response["statusCode"], 200) |
| 406 | + self.assertIn("isBase64Encoded", response) |
| 407 | + self.assertTrue(is_base64(response["body"])) |
| 408 | + |
226 | 409 | def test_wsgi_script_on_cognito_event_request(self): |
227 | 410 | """ |
228 | 411 | Ensure that requests sent by cognito behave sensibly |
|
0 commit comments