예제 #1
0
    def fetch(self, url, body=None, headers=None):
        """Perform an HTTP request

        @raises Exception: Any exception that can be raised by Django

        @see: C{L{HTTPFetcher.fetch}}
        """
        if body:
            # method = 'POST'
            # undo the URL encoding of the POST arguments
            data = parse_qs(body)
            response = self.client.post(url, data)
        else:
            # method = 'GET'
            data = {}
            if headers and 'Accept' in headers:
                data['CONTENT_TYPE'] = headers['Accept']
            response = self.client.get(url, data)

        # Translate the test client response to the fetcher's HTTP response abstraction
        content = response.content
        final_url = url
        response_headers = {}
        if 'Content-Type' in response:
            response_headers['content-type'] = response['Content-Type']
        if 'X-XRDS-Location' in response:
            response_headers['x-xrds-location'] = response['X-XRDS-Location']
        status = response.status_code

        return HTTPResponse(
            body=content,
            final_url=final_url,
            headers=response_headers,
            status=status,
        )
예제 #2
0
    def fetch(self, url, body=None, headers=None):
        self.fetchlog.append((url, body, headers))

        u = urlsplit(url)
        proxy_host = u[1]
        xri = u[2]
        query = u[3]

        if not headers and not query:
            raise ValueError("No headers or query; you probably didn't "
                             "mean to do that.")

        if xri.startswith('/'):
            xri = xri[1:]

        try:
            ctype, body = self.documents[xri]
        except KeyError:
            status = 404
            ctype = 'text/plain'
            body = ''
        else:
            status = 200

        return HTTPResponse(url, status, {'content-type': ctype}, body)
예제 #3
0
    def test_post(self):
        self.fetcher.response = HTTPResponse(
            'http://example.com/', 200, {'content-type': YADIS_CONTENT_TYPE},
            EXAMPLE_XRDS)

        response = self.client.post(
            '/consumer/', {'openid_identifier': 'http://example.com/'})

        # Renders a POST form
        self.assertContains(response, 'http://example.com/')
        self.assertContains(response, 'openid.identity')
예제 #4
0
파일: __init__.py 프로젝트: girder/openid
    def fetch(self, url, body=None, headers=None):
        headers = headers or {}
        if body:
            method = 'POST'
            headers['Content-Type'] = 'application/x-www-form-urlencoded'
        else:
            method = 'GET'

        with requests.session() as s:
            response = s.request(method, url, data=body, headers=headers, verify=self._verify)

        return HTTPResponse(response.url, response.status_code, response.headers, response.content)
예제 #5
0
class TestDiscoveryFailure(datadriven.DataDrivenTestCase):
    cases = [
        [HTTPResponse('http://network.error/', None)],
        [HTTPResponse('http://not.found/', 404)],
        [HTTPResponse('http://bad.request/', 400)],
        [HTTPResponse('http://server.error/', 500)],
        [
            HTTPResponse('http://header.found/',
                         200,
                         headers={'x-xrds-location': 'http://xrds.missing/'}),
            HTTPResponse('http://xrds.missing/', 404)
        ],
    ]

    def __init__(self, responses):
        self.url = responses[0].final_url
        datadriven.DataDrivenTestCase.__init__(self, self.url)
        self.responses = responses

    def setUp(self):
        fetcher = SimpleMockFetcher(self.responses)
        fetchers.setDefaultFetcher(fetcher)

    def tearDown(self):
        fetchers.setDefaultFetcher(None)

    def runOneTest(self):
        expected_status = self.responses[-1].status
        try:
            discover.discover(self.url)
        except DiscoveryFailure as why:
            self.assertEqual(why.http_response.status, expected_status)
        else:
            self.fail('Did not raise DiscoveryFailure')
예제 #6
0
class TestDiscoveryFailure(unittest.TestCase):
    cases = [
        [HTTPResponse('http://network.error/', None)],
        [HTTPResponse('http://not.found/', 404)],
        [HTTPResponse('http://bad.request/', 400)],
        [HTTPResponse('http://server.error/', 500)],
        [
            HTTPResponse('http://header.found/',
                         200,
                         headers={'x-xrds-location': 'http://xrds.missing/'}),
            HTTPResponse('http://xrds.missing/', 404)
        ],
    ]

    def runOneTest(self, url, expected_status):
        with self.assertRaises(DiscoveryFailure) as catch:
            discover.discover(url)
        self.assertEqual(catch.exception.http_response.status, expected_status)

    def test(self):
        for responses in self.cases:
            url = responses[0].final_url
            status = responses[-1].status

            fetcher = SimpleMockFetcher(responses)
            fetchers.setDefaultFetcher(fetcher)
            self.runOneTest(url, status)
            fetchers.setDefaultFetcher(None)
예제 #7
0
    def fetch(self, url, body=None, headers=None):
        if url == self.identity_url:
            # Serve an XRDS document directly, pointing at our endpoint.
            type_uris = ['<Type>%s</Type>' % uri for uri in self.type_uris]
            return HTTPResponse(
                url, 200, {'content-type': 'application/xrds+xml'}, """\
<?xml version="1.0"?>
<xrds:XRDS
    xmlns="xri://$xrd*($v*2.0)"
    xmlns:xrds="xri://$xrds">
  <XRD>
    <Service priority="0">
      %s
      <URI>%s</URI>
      <LocalID>%s</LocalID>
    </Service>
  </XRD>
</xrds:XRDS>
""" % ('\n'.join(type_uris), self.endpoint_url, self.localid_url))
        elif url.startswith(self.endpoint_url):
            # Gather query parameters
            query = {}
            if '?' in url:
                query.update(cgi.parse_qsl(url.split('?', 1)[1]))
            if body is not None:
                query.update(cgi.parse_qsl(body))
            self.last_request = self.server.decodeRequest(query)

            # The browser based requests should not be handled through
            # the fetcher interface.
            assert self.last_request.mode not in BROWSER_REQUEST_MODES

            response = self.server.handleRequest(self.last_request)
            webresponse = self.server.encodeResponse(response)
            return HTTPResponse(url, webresponse.code, webresponse.headers,
                                webresponse.body)
        else:
            raise HTTPFetchingError('unknown URL %s' % url)
예제 #8
0
    def fetch(self, url, body=None, headers=None):
        self.fetchlog.append((url, body, headers))
        if self.redirect:
            final_url = self.redirect
        else:
            final_url = url

        try:
            ctype, body = self.documents[url]
        except KeyError:
            status = 404
            ctype = 'text/plain'
            body = ''
        else:
            status = 200

        return HTTPResponse(final_url, status, {'content-type': ctype}, body)