# Path: autopush/exceptions.py
# class RouterException(AutopushException):
#     """Exception if routing has failed, may include a custom status_code and
#     body to write to the response.
# 
#     """
#     def __init__(self, message, status_code=500, response_body="",
#                  router_data=None, headers=None, log_exception=True,
#                  errno=None, logged_status=None, **kwargs):
#         """Create a new RouterException"""
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.headers = {} if headers is None else headers
#         self.log_exception = log_exception
#         self.response_body = response_body or message
#         self.errno = errno
#         self.logged_status = logged_status
#         self.extra = kwargs
# 
# Path: autopush/router/gcmclient.py
# class GCMAuthenticationError(Exception):
# class Result(object):
# class JSONMessage(object):
# class GCM(object):
#     def __init__(self, response, message):
#     def parse_response(self, content, code, message):
#     def __init__(self,
#                  registration_ids,
#                  collapse_key,
#                  time_to_live,
#                  dry_run,
#                  data):
#     def __init__(self,
#                  api_key=None,
#                  logger=None,
#                  metrics=None,
#                  endpoint="gcm-http.googleapis.com/gcm/send",
#                  **options):
#     def process(self, response, payload):
#     def error(self, failure):
#     def send(self, payload):


import json
import pytest
import treq
from mock import Mock
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.trial import unittest
from twisted.web.http_headers import Headers
from autopush.exceptions import RouterException
from autopush.router import gcmclient
    @inlineCallbacks
    def test_fail_bad_response(self):
        content = json.dumps({
            "multicast_id": 5174939174563864884,
            "success": 0,
            "failure": 1,
            "canonical_ids": 0,
        })
        self._m_resp_text.callback(content)
        self._m_request.callback(self._m_response)
        with pytest.raises(RouterException):
            yield self.gcm.send(self.m_payload)

    @inlineCallbacks
    def test_fail_400(self):
        self._m_response.code = 400
        content = msg = "Invalid JSON"
        self._m_resp_text.callback(content)
        self._m_request.callback(self._m_response)
        with pytest.raises(RouterException) as ex:
            yield self.gcm.send(self.m_payload)
        assert ex.value.status_code == 500
        assert str(ex.value) == "Server error: {}".format(msg)

    @inlineCallbacks
    def test_fail_404(self):
        self._m_response.code = 404
        content = msg = "Invalid URL"
        self._m_resp_text.callback(content)
        self._m_request.callback(self._m_response)

# Path: autopush/exceptions.py
# class RouterException(AutopushException):
#     """Exception if routing has failed, may include a custom status_code and
#     body to write to the response.
# 
#     """
#     def __init__(self, message, status_code=500, response_body="",
#                  router_data=None, headers=None, log_exception=True,
#                  errno=None, logged_status=None, **kwargs):
#         """Create a new RouterException"""
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.headers = {} if headers is None else headers
#         self.log_exception = log_exception
#         self.response_body = response_body or message
#         self.errno = errno
#         self.logged_status = logged_status
#         self.extra = kwargs
# 
# Path: autopush/router/gcmclient.py
# class GCMAuthenticationError(Exception):
# class Result(object):
# class JSONMessage(object):
# class GCM(object):
#     def __init__(self, response, message):
#     def parse_response(self, content, code, message):
#     def __init__(self,
#                  registration_ids,
#                  collapse_key,
#                  time_to_live,
#                  dry_run,
#                  data):
#     def __init__(self,
#                  api_key=None,
#                  logger=None,
#                  metrics=None,
#                  endpoint="gcm-http.googleapis.com/gcm/send",
#                  **options):
#     def process(self, response, payload):
#     def error(self, failure):
#     def send(self, payload):


# Path: autopush/tests/test_gcmclient.py
import json
import pytest
import treq
from mock import Mock
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.trial import unittest
from twisted.web.http_headers import Headers
from autopush.exceptions import RouterException
from autopush.router import gcmclient

    @inlineCallbacks
    def test_fail_bad_response(self):
        content = json.dumps({
            "multicast_id": 5174939174563864884,
            "success": 0,
            "failure": 1,
            "canonical_ids": 0,
        })
        self._m_resp_text.callback(content)
        self._m_request.callback(self._m_response)
        with pytest.raises(RouterException):
            yield self.gcm.send(self.m_payload)

    @inlineCallbacks
    def test_fail_400(self):
        self._m_response.code = 400
        content = msg = "Invalid JSON"
        self._m_resp_text.callback(content)
        self._m_request.callback(self._m_response)
        with pytest.raises(RouterException) as ex:
            yield self.gcm.send(self.m_payload)
        assert ex.value.status_code == 500
        assert str(ex.value) == "Server error: {}".format(msg)

    @inlineCallbacks
    def test_fail_404(self):
        self._m_response.code = 404
        content = msg = "Invalid URL"
        self._m_resp_text.callback(content)
        self._m_request.callback(self._m_response)

        with pytest.raises(RouterException) as ex: