diff --git a/autopush/router/apnsrouter.py b/autopush/router/apnsrouter.py index 297f4697..0a01d2eb 100644 --- a/autopush/router/apnsrouter.py +++ b/autopush/router/apnsrouter.py @@ -115,13 +115,13 @@ def _route(self, notification, router_data): } if notification.data: payload["body"] = notification.data - payload["con"] = notification.headers["content-encoding"] + payload["con"] = notification.headers["encoding"] payload["enc"] = notification.headers["encryption"] - if "crypto-key" in notification.headers: - payload["cryptokey"] = notification.headers["crypto-key"] - elif "encryption-key" in notification.headers: - payload["enckey"] = notification.headers["encryption-key"] + if "crypto_key" in notification.headers: + payload["cryptokey"] = notification.headers["crypto_key"] + elif "encryption_key" in notification.headers: + payload["enckey"] = notification.headers["encryption_key"] payload['aps'] = dict( alert=router_data.get("title", config.get('default_title', 'Mozilla Push')), diff --git a/autopush/router/fcm.py b/autopush/router/fcm.py index 571e041a..a98c004e 100644 --- a/autopush/router/fcm.py +++ b/autopush/router/fcm.py @@ -170,13 +170,13 @@ def _route(self, notification, router_data): 413, errno=104, log_exception=False) data['body'] = notification.data - data['con'] = notification.headers['content-encoding'] + data['con'] = notification.headers['encoding'] data['enc'] = notification.headers['encryption'] - if 'crypto-key' in notification.headers: - data['cryptokey'] = notification.headers['crypto-key'] - elif 'encryption-key' in notification.headers: - data['enckey'] = notification.headers['encryption-key'] + if 'crypto_key' in notification.headers: + data['cryptokey'] = notification.headers['crypto_key'] + elif 'encryption_key' in notification.headers: + data['enckey'] = notification.headers['encryption_key'] # registration_ids are the FCM instance tokens (specified during # registration. diff --git a/autopush/router/gcm.py b/autopush/router/gcm.py index deb54845..8ab8e298 100644 --- a/autopush/router/gcm.py +++ b/autopush/router/gcm.py @@ -89,13 +89,13 @@ def _route(self, notification, uaid_data): 413, errno=104, log_exception=False) data['body'] = notification.data - data['con'] = notification.headers['content-encoding'] + data['con'] = notification.headers['encoding'] data['enc'] = notification.headers['encryption'] - if 'crypto-key' in notification.headers: - data['cryptokey'] = notification.headers['crypto-key'] - elif 'encryption-key' in notification.headers: - data['enckey'] = notification.headers['encryption-key'] + if 'crypto_key' in notification.headers: + data['cryptokey'] = notification.headers['crypto_key'] + elif 'encryption_key' in notification.headers: + data['enckey'] = notification.headers['encryption_key'] # registration_ids are the GCM instance tokens (specified during # registration. diff --git a/autopush/tests/test_router.py b/autopush/tests/test_router.py index 35bdfb9b..79c41c0e 100644 --- a/autopush/tests/test_router.py +++ b/autopush/tests/test_router.py @@ -122,6 +122,7 @@ def setUp(self, mt, mc): ttl=200, message_id=10, ) + self.notif.cleanup_headers() self.router_data = dict(router_data=dict(token="connect_data", rel_channel="firefox")) @@ -240,6 +241,7 @@ def test_route_crypto_key(self): ttl=200, message_id=10, ) + self.notif.cleanup_headers() d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -280,6 +282,7 @@ def setUp(self, fgcm): ttl=200, message_id=10, ) + self.notif.cleanup_headers() self.router_data = dict( router_data=dict( token="connect_data", @@ -364,6 +367,7 @@ def test_ttl_none(self): headers=self.headers, ttl=None ) + self.notif.cleanup_headers() d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -394,6 +398,7 @@ def test_ttl_high(self): headers=self.headers, ttl=5184000 ) + self.notif.cleanup_headers() d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -421,6 +426,7 @@ def test_long_data(self): headers=self.headers, ttl=200 ) + d = self.router.route_notification(bad_notif, self.router_data) def check_results(result): @@ -433,8 +439,8 @@ def check_results(result): def test_route_crypto_notification(self): self.router.gcm['test123'] = self.gcm - del(self.notif.headers['encryption-key']) - self.notif.headers['crypto-key'] = 'crypto' + del(self.notif.headers['encryption_key']) + self.notif.headers['crypto_key'] = 'crypto' d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -564,6 +570,7 @@ def setUp(self, ffcm): headers=self.headers, ttl=200 ) + self.notif.cleanup_headers() self.router_data = dict( router_data=dict( token="connect_data", @@ -641,6 +648,7 @@ def test_ttl_none(self): headers=self.headers, ttl=None ) + self.notif.cleanup_headers() d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -668,6 +676,7 @@ def test_ttl_high(self): headers=self.headers, ttl=5184000 ) + self.notif.cleanup_headers() d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -696,6 +705,7 @@ def test_long_data(self): ttl=200, message_id=10, ) + self.notif.cleanup_headers() d = self.router.route_notification(bad_notif, self.router_data) def check_results(result): @@ -708,8 +718,8 @@ def check_results(result): def test_route_crypto_notification(self): self.router.fcm = self.fcm - del(self.notif.headers['encryption-key']) - self.notif.headers['crypto-key'] = 'crypto' + del(self.notif.headers['encryption_key']) + self.notif.headers['crypto_key'] = 'crypto' d = self.router.route_notification(self.notif, self.router_data) def check_results(result): @@ -1119,6 +1129,7 @@ def test_route_to_busy_node_with_ttl_zero(self): ttl=0, message_id=uuid.uuid4().hex, ) + self.notif.cleanup_headers() self.agent_mock.request.return_value = response_mock = Mock() response_mock.addCallback.return_value = response_mock type(response_mock).code = PropertyMock( diff --git a/autopush/tests/test_websocket.py b/autopush/tests/test_websocket.py index 11059ad0..7de6f252 100644 --- a/autopush/tests/test_websocket.py +++ b/autopush/tests/test_websocket.py @@ -1517,13 +1517,17 @@ def test_notification_with_webpush(self): "timestamp": 0} self.proto.send_notifications(payload) + fixed_headers = dict() + for header in dummy_headers: + fixed_headers[header.replace("-", "_")] = dummy_headers[header] + # Check the call result args = json.loads(self.send_mock.call_args[0][0]) eq_(args, {"messageType": "notification", "channelID": chid, "data": dummy_data, "version": "10", - "headers": dummy_headers}) + "headers": fixed_headers}) def test_notification_avoid_newer_delivery(self): self._connect() diff --git a/autopush/utils.py b/autopush/utils.py index 8527b6c0..a7c82c23 100644 --- a/autopush/utils.py +++ b/autopush/utils.py @@ -326,13 +326,14 @@ def cleanup_headers(self): head = headers[hdr].replace('"', '') headers[hdr] = STRIP_PADDING.sub("", head) - data = dict( - encoding=headers["content-encoding"], - encryption=headers["encryption"], - ) + # content-encoding header may already be stored as "encoding", + # this is a failover to ensure that the proper value is pulled in. + # Webpush expects the value of "encoding". + data = dict(encoding=headers.get("encoding", + headers.get("content-encoding"))) # AWS cannot store empty strings, so we only add these keys if # they're present to avoid empty strings. - for name in ["encryption-key", "crypto-key"]: + for name in ["encryption", "encryption-key", "crypto-key"]: if name in headers: # NOTE: The client code expects all header keys to be lower # case and s/-/_/. @@ -519,7 +520,9 @@ def websocket_format(self): ) if self.data: payload["data"] = self.data - payload["headers"] = self.headers + payload["headers"] = { + k.replace("-", "_"): v for k, v in self.headers.items() + } return payload