Repository: adamchainz/apig-wsgi

Issue:
502 bad gateway error on ALB : statusCode must be numeric
Hey,

having an issue with apig-wsgi on an ALB : I always receive a 502 bad gateway error.
Changing statusCode from a string to a number solve my issue.

This is my working monkey patching :

import apig_wsgi
class Response(apig_wsgi.Response):
    def as_apig_response(self):
        response = super().as_apig_response()
        response['statusCode'] = int(response['statusCode'])
        return response
apig_wsgi.Response = Response


Discussion:

Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 2073fb4..dda2242 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -6,6 +6,10 @@ Pending Release
 
 .. Insert new release notes below this line
 
+* Change ``statusCode`` returned to API Gateway / ALB to an integer. It seems
+  API Gateway always supported both strings and integers, whilst API Gateway
+  only supports integers.
+
 2.0.2 (2019-02-07)
 ------------------
 
diff --git a/apig_wsgi.py b/apig_wsgi.py
index 3a1045b..cb204c3 100644
--- a/apig_wsgi.py
+++ b/apig_wsgi.py
@@ -72,7 +72,7 @@ def get_environ(event, binary_support):
 
 class Response(object):
     def __init__(self, binary_support):
-        self.status_code = '500'
+        self.status_code = 500
         self.headers = []
         self.body = BytesIO()
         self.binary_support = binary_support
@@ -80,7 +80,7 @@ class Response(object):
     def start_response(self, status, response_headers, exc_info=None):
         if exc_info is not None:
             raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
-        self.status_code = status.split()[0]
+        self.status_code = int(status.split()[0])
         self.headers.extend(response_headers)
         return self.body.write
 


Test patch:
diff --git a/test_apig_wsgi.py b/test_apig_wsgi.py
index 6d61584..341bc6a 100644
--- a/test_apig_wsgi.py
+++ b/test_apig_wsgi.py
@@ -45,7 +45,7 @@ def test_get(simple_app):
     response = simple_app.handler(make_event(), None)
 
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {'Content-Type': 'text/plain'},
         'body': 'Hello World\n',
     }
@@ -57,7 +57,7 @@ def test_get_missing_content_type(simple_app):
     response = simple_app.handler(make_event(), None)
 
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {},
         'body': 'Hello World\n',
     }
@@ -69,7 +69,7 @@ def test_get_binary_support_text(simple_app):
     response = simple_app.handler(make_event(), None)
 
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {'Content-Type': 'text/plain'},
         'body': 'Hello World\n',
     }
@@ -83,7 +83,7 @@ def test_get_binary_support_binary(simple_app):
     response = simple_app.handler(make_event(), None)
 
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {'Content-Type': 'application/octet-stream'},
         'body': b64encode(b'\x13\x37').decode('utf-8'),
         'isBase64Encoded': True,
@@ -98,7 +98,7 @@ def test_get_binary_support_no_content_type(simple_app):
     response = simple_app.handler(make_event(), None)
 
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {},
         'body': b64encode(b'\x13\x37').decode('utf-8'),
         'isBase64Encoded': True,
@@ -113,7 +113,7 @@ def test_post(simple_app):
     assert simple_app.environ['wsgi.input'].read() == b'The World is Large'
     assert simple_app.environ['CONTENT_LENGTH'] == str(len(b'The World is Large'))
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {'Content-Type': 'text/plain'},
         'body': 'Hello World\n',
     }
@@ -128,7 +128,7 @@ def test_post_binary_support(simple_app):
     assert simple_app.environ['wsgi.input'].read() == b'dogfood'
     assert simple_app.environ['CONTENT_LENGTH'] == str(len(b'dogfood'))
     assert response == {
-        'statusCode': '200',
+        'statusCode': 200,
         'headers': {'Content-Type': 'text/plain'},
         'body': 'Hello World\n',
     }