# Path: autopush/protocol.py
# class IgnoreBody(Protocol):
#     """A protocol that discards any data it receives
# 
#     This is necessary to support persistent HTTP connections. If the
#     response body is never read using ``Response.deliverBody``, or
#     ``stopProducing()`` is called, the connection will not be reused.
# 
#     """
#     def __init__(self, response, deferred):
#         self.response = response
#         self.deferred = deferred
# 
#     @classmethod
#     def ignore(cls, response):
#         """Class method helper for ignoring the response"""
#         d = Deferred()
#         if response is not None:
#             response.deliverBody(cls(response, d))
#         return d
# 
#     def dataReceived(self, data):
#         """Ignore received data"""
#         pass
# 
#     def connectionLost(self, reason):
#         """Relay back the loss of connection to the deferred"""
#         if reason.check(ResponseDone):
#             self.deferred.callback(self.response)
#         else:
#             self.deferred.errback(reason)
#         del self.response
#         del self.deferred


from mock import Mock
from nose.tools import eq_
from twisted.trial import unittest
from twisted.web.client import Response
from autopush.protocol import IgnoreBody


class ProtocolTestCase(unittest.TestCase):
    def test_ignore(self):
        mock_reason = Mock()
        mock_reason.check.return_value = True

        def deliverBody(proto):
            proto.dataReceived("some data to ignore")
            proto.connectionLost(mock_reason)
        mock_response = Mock(spec=Response)
        mock_response.deliverBody.side_effect = deliverBody
        d = IgnoreBody.ignore(mock_response)

        def verifyResponse(result):
            eq_(result, mock_response)
            eq_(len(mock_reason.mock_calls), 1)

        d.addCallback(verifyResponse)
        return d

    def test_ignore_check_false(self):
        mock_reason = Mock()
        mock_reason.check.return_value = False

        def deliverBody(proto):
            proto.dataReceived("some data to ignore")
            proto.connectionLost(mock_reason)
        mock_response = Mock(spec=Response)
        mock_response.deliverBody.side_effect = deliverBody

# Path: autopush/protocol.py
# class IgnoreBody(Protocol):
#     """A protocol that discards any data it receives
# 
#     This is necessary to support persistent HTTP connections. If the
#     response body is never read using ``Response.deliverBody``, or
#     ``stopProducing()`` is called, the connection will not be reused.
# 
#     """
#     def __init__(self, response, deferred):
#         self.response = response
#         self.deferred = deferred
# 
#     @classmethod
#     def ignore(cls, response):
#         """Class method helper for ignoring the response"""
#         d = Deferred()
#         if response is not None:
#             response.deliverBody(cls(response, d))
#         return d
# 
#     def dataReceived(self, data):
#         """Ignore received data"""
#         pass
# 
#     def connectionLost(self, reason):
#         """Relay back the loss of connection to the deferred"""
#         if reason.check(ResponseDone):
#             self.deferred.callback(self.response)
#         else:
#             self.deferred.errback(reason)
#         del self.response
#         del self.deferred


# Path: autopush/tests/test_protocol.py
from mock import Mock
from nose.tools import eq_
from twisted.trial import unittest
from twisted.web.client import Response
from autopush.protocol import IgnoreBody



class ProtocolTestCase(unittest.TestCase):
    def test_ignore(self):
        mock_reason = Mock()
        mock_reason.check.return_value = True

        def deliverBody(proto):
            proto.dataReceived("some data to ignore")
            proto.connectionLost(mock_reason)
        mock_response = Mock(spec=Response)
        mock_response.deliverBody.side_effect = deliverBody
        d = IgnoreBody.ignore(mock_response)

        def verifyResponse(result):
            eq_(result, mock_response)
            eq_(len(mock_reason.mock_calls), 1)

        d.addCallback(verifyResponse)
        return d

    def test_ignore_check_false(self):
        mock_reason = Mock()
        mock_reason.check.return_value = False

        def deliverBody(proto):
            proto.dataReceived("some data to ignore")
            proto.connectionLost(mock_reason)
        mock_response = Mock(spec=Response)
        mock_response.deliverBody.side_effect = deliverBody

        d = IgnoreBody.ignore(mock_response)