# Path: mozsvc/exceptions.py
# class BackendError(Exception):
#     """Raised when the backend is down or fails"""
#     def __init__(self, msg='', server='', retry_after=None,
#                  backend=None, request=None):
#         """
#         - msg, server will be dumped in str() if provided
#         - retry_after, if set to a positive integer, will be used to send
#           back a Retry-After header value. If not set, a default value is
#           returned. If set to 0, the header is explicitely skipped.
#         - backend: if provided, the backend from where originated the error.
#           it will be used to render more details, It can be any kind of object
#           as __str__() will be called on it.
# 
#         """
#         self.msg = msg
#         self.server = server
#         self.retry_after = retry_after
#         self.backend = backend
#         self.request = request
# 
#     def __str__(self):
#         res = self.__class__.__name__
#         if self.server != '':
#             res += ' on %s' % self.server
#         if self.backend is not None:
#             res += '\n%s' % str(self.backend)
#         if self.msg != '':
#             res += '\n\n%s' % self.msg
#         if self.request:
#             res = '%s %s\n%s' % (self.request.method,
#                                  self.request.path_info,
#                                  res)
#         return res
# 
# Path: mozsvc/tests/support.py
# def make_request(config, path="/", environ=None, factory=None):
#     my_environ = {}
#     my_environ["wsgi.version"] = (1, 0)
#     my_environ["wsgi.multithread"] = True
#     my_environ["wsgi.multiprocess"] = True
#     my_environ["wsgi.run_once"] = False
#     my_environ["wsgi.url_scheme"] = "http"
#     my_environ["REQUEST_METHOD"] = "GET"
#     my_environ["SCRIPT_NAME"] = ""
#     my_environ["PATH_INFO"] = path
#     my_environ["SERVER_NAME"] = "localhost"
#     my_environ["SERVER_PORT"] = "5000"
#     if environ is not None:
#         my_environ.update(environ)
#     if factory is None:
#         factory = config.registry.queryUtility(IRequestFactory)
#         if factory is None:
#             factory = Request
#     request = factory(my_environ)
#     request.registry = config.registry
#     return request


import unittest
import pyramid.testing
from pyramid.response import Response
from mozsvc.exceptions import BackendError
from mozsvc.tests.support import make_request
        def backend_error(request):
            raise BackendError(retry_after=42)

        r = self._do_request("/backend_error")
        self.assertEqual(r.status_int, 503)
        retry_after = int(r.headers["Retry-After"])
        self.assertTrue(42 <= retry_after <= 47)

    def test_that_retry_after_doesnt_get_set_to_zero(self):
        @self._set_backend_error_view
        def backend_error(request):
            raise BackendError(retry_after=0)

        r = self._do_request("/backend_error")
        self.assertEqual(r.status_int, 503)
        self.assertEqual(r.headers.get("Retry-After"), None)


class TestBackoffResponseTween(unittest.TestCase):

    def setUp(self):
        self.app = None
        self.config = pyramid.testing.setUp()
        self.config.add_route("root", "/")
        self.config.add_view(lambda r: Response("ok"), route_name="root")

    def tearDown(self):
        pyramid.testing.tearDown()

    def _make_request(self, *args, **kwds):

# Path: mozsvc/exceptions.py
# class BackendError(Exception):
#     """Raised when the backend is down or fails"""
#     def __init__(self, msg='', server='', retry_after=None,
#                  backend=None, request=None):
#         """
#         - msg, server will be dumped in str() if provided
#         - retry_after, if set to a positive integer, will be used to send
#           back a Retry-After header value. If not set, a default value is
#           returned. If set to 0, the header is explicitely skipped.
#         - backend: if provided, the backend from where originated the error.
#           it will be used to render more details, It can be any kind of object
#           as __str__() will be called on it.
# 
#         """
#         self.msg = msg
#         self.server = server
#         self.retry_after = retry_after
#         self.backend = backend
#         self.request = request
# 
#     def __str__(self):
#         res = self.__class__.__name__
#         if self.server != '':
#             res += ' on %s' % self.server
#         if self.backend is not None:
#             res += '\n%s' % str(self.backend)
#         if self.msg != '':
#             res += '\n\n%s' % self.msg
#         if self.request:
#             res = '%s %s\n%s' % (self.request.method,
#                                  self.request.path_info,
#                                  res)
#         return res
# 
# Path: mozsvc/tests/support.py
# def make_request(config, path="/", environ=None, factory=None):
#     my_environ = {}
#     my_environ["wsgi.version"] = (1, 0)
#     my_environ["wsgi.multithread"] = True
#     my_environ["wsgi.multiprocess"] = True
#     my_environ["wsgi.run_once"] = False
#     my_environ["wsgi.url_scheme"] = "http"
#     my_environ["REQUEST_METHOD"] = "GET"
#     my_environ["SCRIPT_NAME"] = ""
#     my_environ["PATH_INFO"] = path
#     my_environ["SERVER_NAME"] = "localhost"
#     my_environ["SERVER_PORT"] = "5000"
#     if environ is not None:
#         my_environ.update(environ)
#     if factory is None:
#         factory = config.registry.queryUtility(IRequestFactory)
#         if factory is None:
#             factory = Request
#     request = factory(my_environ)
#     request.registry = config.registry
#     return request


# Path: mozsvc/tests/test_tweens.py
import unittest
import pyramid.testing
from pyramid.response import Response
from mozsvc.exceptions import BackendError
from mozsvc.tests.support import make_request

        def backend_error(request):
            raise BackendError(retry_after=42)

        r = self._do_request("/backend_error")
        self.assertEqual(r.status_int, 503)
        retry_after = int(r.headers["Retry-After"])
        self.assertTrue(42 <= retry_after <= 47)

    def test_that_retry_after_doesnt_get_set_to_zero(self):
        @self._set_backend_error_view
        def backend_error(request):
            raise BackendError(retry_after=0)

        r = self._do_request("/backend_error")
        self.assertEqual(r.status_int, 503)
        self.assertEqual(r.headers.get("Retry-After"), None)


class TestBackoffResponseTween(unittest.TestCase):

    def setUp(self):
        self.app = None
        self.config = pyramid.testing.setUp()
        self.config.add_route("root", "/")
        self.config.add_view(lambda r: Response("ok"), route_name="root")

    def tearDown(self):
        pyramid.testing.tearDown()

    def _make_request(self, *args, **kwds):

        return make_request(self.config, *args, **kwds)