예제 #1
0
    def _mk_response(self, code=200, phrase='OK', headers={},
                     delivered_body='{}'):
        response = Response(('HTTP', 1, 1), code, phrase,
                            mkheaders(headers), None)

        response.delivered_body = delivered_body
        return response
예제 #2
0
 def test_proxy_readbodyprotocol(self):
     original = Response(('HTTP', 1, 1), 200, 'OK', Headers(),
                         AbortableStringTransport())
     original._bodyDataReceived(LOREM_IPSUM)
     original._bodyDataFinished()
     proxy = RecordingResponse(original)
     body = yield readBody(proxy)
     self.assertEqual(body, LOREM_IPSUM)
     self.assertEqual(proxy.value(), LOREM_IPSUM)
예제 #3
0
    def _mk_response(self,
                     code=200,
                     phrase='OK',
                     headers={},
                     delivered_body='{}'):
        response = Response(('HTTP', 1, 1), code, phrase, mkheaders(headers),
                            None)

        response.delivered_body = delivered_body
        return response
예제 #4
0
 def test_simple(self):
     data = [{'foo': 'bar', 'index': i} for i in xrange(10000)]
     json_str = json.dumps(data)
     response = Response(('HTTP', 1, 1), 200, 'OK', Headers(),
                         StringTransport())
     finished = read_json_body(response)
     finished.addCallback(self.assertEqual, data)
     chunk_length = 1000
     for start in xrange(0, len(json_str), chunk_length):
         response._bodyDataReceived(json_str[start:start + chunk_length])
     response._bodyDataFinished()
     return finished
예제 #5
0
    def test_inboxReplyFailedDelete(self):
        """
        When an inbox item that contains a reply is seen by the client, it
        deletes it immediately.  If the delete fails, the appropriate response
        code is returned.
        """
        userNumber = 1
        clock = Clock()
        inboxURL = '/some/inbox/'
        vevent = Component.fromString(INBOX_REPLY)
        inbox = Calendar(caldavxml.schedule_inbox, set(), u'the inbox',
                         inboxURL, None)
        client = StubClient(userNumber, self.mktemp())
        client._calendars[inboxURL] = inbox

        inboxEvent = Event(client.serializeLocation(), inboxURL + u'4321.ics',
                           None, vevent)
        client._setEvent(inboxEvent.url, inboxEvent)
        client._failDeleteWithObject(
            inboxEvent.url,
            IncorrectResponseCode(
                NO_CONTENT,
                Response(('HTTP', 1, 1), PRECONDITION_FAILED,
                         'Precondition Failed', None, None)))
        accepter = Accepter(clock, self.sim, client, userNumber)
        accepter.eventChanged(inboxEvent.url)
        clock.advance(3)
        self.assertNotIn(inboxEvent.url, client._events)
        self.assertNotIn('4321.ics', inbox.events)
예제 #6
0
        def request(method, uri, headers, body):
            """
            Processes a request sent to the mocked agent.

            :param method: The method of the request.
            :type method: bytes
            :param uri: The URI of the request.
            :type uri: bytes
            :param headers: The headers of the request.
            :type headers: twisted.web.http_headers.Headers
            :param body: The body of the request.
            :type body: twisted.web.client.FileBodyProducer[io.BytesIO]

            :return: A deferred that resolves into a 200 OK response.
            :rtype: twisted.internet.defer.Deferred[Response]
            """
            # Check the method and the URI.
            assert method == b"POST"
            assert uri == b"https://fake.server:1234/_matrix/identity/replicate/v1/push"

            # postJson calls the agent with a BytesIO within a FileBodyProducer, so we
            # need to unpack the payload correctly.
            payload = json.loads(body._inputFile.read().decode("utf8"))
            for assoc_id, assoc in payload["sgAssocs"].items():
                sent_assocs[assoc_id] = assoc

            # Return with a fake response wrapped in a Deferred.
            d = defer.Deferred()
            d.callback(Response((b"HTTP", 1, 1), 200, b"OK", None, None))
            return d
예제 #7
0
 def test_redirect_to_same_host(self):
     finished = self.fetcher.fetch_title('http://foo.test/',
                                         hostname_tag=True)
     request, result = self.protocol.requests.pop()
     redirect_headers = Headers()
     redirect_headers.addRawHeader('Location', 'http://foo.test/bar')
     response = Response._construct(('HTTP', 1, 1), 301,
                                    'Moved Permanently', redirect_headers,
                                    AbortableStringTransport(), request)
     result.callback(response)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual, u'[foo.test] Unknown document')
     return finished
예제 #8
0
 def test_redirect_to_same_host(self):
     finished = self.fetcher.fetch_title(
         'http://foo.test/', hostname_tag=True)
     request, result = self.protocol.requests.pop()
     redirect_headers = Headers()
     redirect_headers.addRawHeader('Location', 'http://foo.test/bar')
     response = Response._construct(
         ('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers,
         AbortableStringTransport(), request)
     result.callback(response)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual, u'[foo.test] Unknown document')
     return finished
예제 #9
0
 def test_no_redirect(self):
     finished = self.fetcher.fetch_title('http://foo.test/',
                                         hostname_tag=True)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual, u'[foo.test] Unknown document')
     return finished
예제 #10
0
 def test_no_redirect(self):
     finished = self.fetcher.fetch_title(
         'http://foo.test/', hostname_tag=True)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual, u'[foo.test] Unknown document')
     return finished
예제 #11
0
 def test_soft_redirect(self):
     self.fetcher.extractors = {
         'text/html': SoftRedirectExtractor(u'http://bar.test/')}
     finished = self.fetcher.fetch_title(
         'http://foo.test/', hostname_tag=True)
     request, result = self.protocol.requests.pop()
     redirect_headers = Headers()
     redirect_headers.addRawHeader('Content-Type', 'text/html')
     response = Response._construct(
         ('HTTP', 1, 1), 200, 'OK', redirect_headers,
         AbortableStringTransport(), request)
     result.callback(response)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[foo.test → bar.test] Unknown document')
     return finished
예제 #12
0
 def test_soft_redirect(self):
     self.fetcher.extractors = {
         'text/html': SoftRedirectExtractor(u'http://bar.test/')
     }
     finished = self.fetcher.fetch_title('http://foo.test/',
                                         hostname_tag=True)
     request, result = self.protocol.requests.pop()
     redirect_headers = Headers()
     redirect_headers.addRawHeader('Content-Type', 'text/html')
     response = Response._construct(('HTTP', 1, 1), 200, 'OK',
                                    redirect_headers,
                                    AbortableStringTransport(), request)
     result.callback(response)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[foo.test → bar.test] Unknown document')
     return finished
예제 #13
0
 def test_tag_iri_to_uri(self):
     finished = self.fetcher.fetch_title(u'http://ドメイン名例.test/',
                                         hostname_tag=True)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(
         self.assertEqual, u'[ドメイン名例.test → xn--eckwd4c7cu47r2wf.test] '
         u'Unknown document')
     return finished
예제 #14
0
 def assert_delivery(self, data, expected):
     finished = Deferred()
     finished.addCallback(self.assertEqual, expected)
     response = Response(('HTTP', 1, 1), 200, 'OK', Headers(),
                         AbortableStringTransport())
     protocol = TruncatingReadBodyProtocol(response.code,
                                           response.phrase,
                                           finished,
                                           max_bytes=8)
     response.deliverBody(protocol)
     response._bodyDataReceived(data)
     response._bodyDataFinished()
     return finished
예제 #15
0
 def test_tag_iri_to_uri(self):
     finished = self.fetcher.fetch_title(
         u'http://ドメイン名例.test/', hostname_tag=True)
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[ドメイン名例.test → xn--eckwd4c7cu47r2wf.test] '
                          u'Unknown document')
     return finished
예제 #16
0
    def changeEventAttendee(self, href, old, new):
        if href in self.rescheduled:
            return fail(
                IncorrectResponseCode(
                    NO_CONTENT,
                    Response(('HTTP', 1, 1), PRECONDITION_FAILED,
                             'Precondition Failed', None, None)))

        vevent = self._events[href].component
        vevent.mainComponent().removeProperty(old)
        vevent.mainComponent().addProperty(new)
        self._events[href].component = vevent
        return succeed(None)
예제 #17
0
 def assert_delivery(self, data, expected):
     finished = Deferred()
     finished.addCallback(self.assertEqual, expected)
     response = Response(('HTTP', 1, 1), 200, 'OK', Headers(),
                         AbortableStringTransport())
     protocol = TruncatingReadBodyProtocol(
         response.code, response.phrase, finished, max_bytes=8)
     response.deliverBody(protocol)
     response._bodyDataReceived(data)
     response._bodyDataFinished()
     return finished
예제 #18
0
 def test_infinite_hard_redirection(self):
     finished = self.fetcher.fetch_title(
         u'http://foo.test/', hostname_tag=True, friendly_errors=True)
     while self.protocol.requests:
         request, result = self.protocol.requests.pop()
         redirect_headers = Headers()
         redirect_headers.addRawHeader('Location', 'http://foo.test/')
         response = Response._construct(
             ('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers,
             AbortableStringTransport(), request)
         result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[foo.test] Encountered too many redirects.')
     return finished
예제 #19
0
 def test_infinite_hard_redirection(self):
     finished = self.fetcher.fetch_title(u'http://foo.test/',
                                         hostname_tag=True,
                                         friendly_errors=True)
     while self.protocol.requests:
         request, result = self.protocol.requests.pop()
         redirect_headers = Headers()
         redirect_headers.addRawHeader('Location', 'http://foo.test/')
         response = Response._construct(
             ('HTTP', 1, 1), 301, 'Moved Permanently', redirect_headers,
             AbortableStringTransport(), request)
         result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[foo.test] Encountered too many redirects.')
     return finished
예제 #20
0
 def test_content_length_unknown(self):
     agent = CassetteAgent(self.agent, '')
     finished = agent.request('GET', 'http://foo.test/')
     request, result = self.protocol.requests.pop()
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', Headers(),
                                    AbortableStringTransport(), request)
     response._bodyDataFinished()
     result.callback(response)
     finished.addCallback(readBody)
     def assert_length_absent(deferred_result):
         interaction = agent.cassette.as_dict()['http_interactions'][0]
         self.assertFalse(
             interaction['response']['headers'].get('Content-Length'))
         return deferred_result
     finished.addCallback(assert_length_absent)
     return finished
예제 #21
0
 def test_infinite_soft_redirection(self):
     self.fetcher.extractors = {
         'text/html': SoftRedirectExtractor(u'http://foo.test/')}
     finished = self.fetcher.fetch_title(
         u'http://foo.test/', hostname_tag=True, friendly_errors=True)
     while self.protocol.requests:
         request, result = self.protocol.requests.pop()
         redirect_headers = Headers()
         redirect_headers.addRawHeader('Content-Type', 'text/html')
         response = Response._construct(
             ('HTTP', 1, 1), 200, 'OK', redirect_headers,
             AbortableStringTransport(), request)
         result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[foo.test] Encountered too many redirects.')
     return finished
예제 #22
0
 def test_infinite_soft_redirection(self):
     self.fetcher.extractors = {
         'text/html': SoftRedirectExtractor(u'http://foo.test/')
     }
     finished = self.fetcher.fetch_title(u'http://foo.test/',
                                         hostname_tag=True,
                                         friendly_errors=True)
     while self.protocol.requests:
         request, result = self.protocol.requests.pop()
         redirect_headers = Headers()
         redirect_headers.addRawHeader('Content-Type', 'text/html')
         response = Response._construct(('HTTP', 1, 1), 200, 'OK',
                                        redirect_headers,
                                        AbortableStringTransport(), request)
         result.callback(response)
     finished.addCallback(self.assertEqual,
                          u'[foo.test] Encountered too many redirects.')
     return finished
예제 #23
0
 def test_header_isolation(self):
     agent = CassetteAgent(self.agent, '')
     finished = agent.request('GET', 'http://foo.test/')
     request, result = self.protocol.requests.pop()
     headers = Headers()
     headers.addRawHeader('Content-Encoding', 'gzip')
     response = Response._construct(('HTTP', 1, 1), 200, 'OK', headers,
                                    AbortableStringTransport(), request)
     response._bodyDataFinished()
     result.callback(response)
     @inlineCallbacks
     def assert_intact_headers(agent_response):
         yield readBody(agent_response)
         agent_response.headers.removeHeader('Content-Encoding')
         interaction = agent.cassette.as_dict()['http_interactions'][0]
         self.assertEqual(
             ['gzip'],
             interaction['response']['headers']['Content-Encoding'])
         returnValue(agent_response)
     finished.addCallback(assert_intact_headers)
     return finished
예제 #24
0
 def __init__(self, version, code, phrase, headers, body):
     headers.addRawHeader('Content-Length', [str(len(body))])
     Response.__init__(self, version, code, phrase, headers, None)
     self.body = body
예제 #25
0
 async def post_json_get_nothing(uri, post_json, opts):
     return Response((b"HTTP", 1, 1), 200, b"OK", None, None)
예제 #26
0
 def post_json_get_nothing(uri, post_json, opts):
     return Response((b'HTTP', 1, 1), 200, b'OK', None, None)