示例#1
0
    def BadResponse(body, request, status_code=None, headers=None):
        """
        Construct a Bad HTTP response (defined in DEFAULT_BAD_RESPONSE_CODE)

        :param body: The body of the response
        :type  body: ``str``

        :param request: The HTTP request
        :type  request: :class:`requests.Request`

        :param status_code: The return status code, defaults
            to DEFAULT_GOOD_STATUS_CODE if not specified
        :type  status_code: ``int``

        :param headers: Response headers, defaults to
            DEFAULT_RESPONSE_HEADERS if not specified
        :type  headers: ``dict``

        :rtype: :class:`requests.Response`
        :returns: a Response object
        """
        response = Response()
        response.url = request.url
        response.raw = BytesIO(body)
        if status_code:
            response.status_code = status_code
        else:
            response.status_code = DEFAULT_BAD_STATUS_CODE
        if headers:
            response.headers = headers
        else:
            response.headers = DEFAULT_RESPONSE_HEADERS
        response.request = request
        response._content = body
        return response
示例#2
0
    def send(self,
             request,
             stream=None,
             timeout=None,
             verify=None,
             cert=None,
             proxies=None):
        pathname = url_to_path(request.url)

        resp = Response()
        resp.status_code = 200
        resp.url = request.url

        try:
            stats = stat(pathname)
        except OSError as exc:
            resp.status_code = 404
            resp.raw = exc
        else:
            modified = formatdate(stats.st_mtime, usegmt=True)
            content_type = guess_type(pathname)[0] or "text/plain"
            resp.headers = CaseInsensitiveDict({
                "Content-Type": content_type,
                "Content-Length": stats.st_size,
                "Last-Modified": modified,
            })

            resp.raw = open(pathname, "rb")
            resp.close = resp.raw.close

        return resp
示例#3
0
 def test_request_method(self):
     client = self.ClientClass(self.config)
     url = '/accounts'
     with patch('requests.request') as mocked_request:
         data = {
             "MetaInformation": {
                 "@TotalResources": 1210,
                 "@TotalPages": 13,
                 "@CurrentPage": 1
             },
             "Accounts": [{
                 "@url":
                 "https://api.fortnox.se/3/accounts/1010?financialyear=1",
                 "Active": False,
                 "BalanceBroughtForward": 0,
                 "CostCenter": None,
                 "CostCenterSettings": "ALLOWED",
                 "Description": "Utvecklingsutgifter",
                 "Number": 1010,
                 "Project": "",
                 "ProjectSettings": "ALLOWED",
                 "SRU": 7201,
                 "Year": 2,
                 "VATCode": None
             }]
         }
         _response_client = Response()
         _response_client._content = json.dumps(data).encode('utf-8')
         _response_client.status_code = 200
         _response_client.headers = {"Content-Type": "application/json"}
         mocked_request.return_value = _response_client
         response = client.request('get', url, **{'raw': True})
         self.assertEqual(response[0], 200)
         self.assertEqual(response[1], {"Content-Type": "application/json"})
         self.assertEqual(response[2], data)
示例#4
0
    def to_requests_response(self):
        """Returns an instance of `requests.Response` based on this response.

        Returns:
            request.Response: the generated response.
        """

        # Make sure that body is at position 0 before returning
        self.body.seek(0)

        urllib3_response = URLLib3Rresponse(
            body=self.body,
            headers=self.headers,
            status=self.http_code,
            request_method=self.request.method,
            reason=self.reason,
            preload_content=False
        )

        response = RequestResponse()
        response.request = self.request
        response.raw = urllib3_response
        response.status_code = self.http_code
        response.reason = self.reason
        response.headers = CaseInsensitiveDict(response.raw.headers)
        response.encoding = get_encoding_from_headers(response.headers)

        extract_cookies_to_jar(response.cookies, self.request, urllib3_response)

        if isinstance(self.request.url, six.binary_type):
            response.url = self.request.url.decode("utf-8")
        else:
            response.url = self.request.url

        return response
示例#5
0
 def new_response(self, response):
     """Convert an tornado.HTTPResponse object to a requests.Response object"""
     new = Response()
     new._content = response.body
     new.status_code = response.code
     new.headers = dict(response.headers.get_all())
     return new
示例#6
0
    def get_response_from_request(self, request):
        """
        You may override this method to implement your own response logic based
        on given request. You can even modify the ``self.responses`` based on
        some POST, PUT or DELETE request.

        This is the only method that looks at ``self.responses``. Therefore,
        overriding this method also allows you to create a custom format for
        this container variable or even mutate the ``responses`` variable based
        on the request.
        """
        response = Response()
        # Get mock response for URI (look for full URI and path URI).
        response_methods = self.responses.get(request.uri) or self.responses.get(
            request.uri[len(self.root_uri):])

        # If the URI is not found, return a 404.
        if response_methods is None:
            response.status_code = 404
            response._content = 'Page not found'
        # If the URI is found, but not the method, return a 405.
        elif request.method not in response_methods:
            response.status_code = 405
            response._content = 'Method not allowed'
        # Otherwise, return the headers and content from the responses.
        else:
            response_headers, response_content = response_methods[request.method]
            response.status_code = 200
            response._content = response_content
            response.headers = response_headers

        return response
示例#7
0
    def request(self, uri, method='GET', body=None, headers=None):
        if self._response_index >= len(self.responses):
            raise ValueError('Ran out of responses when requesting: %s' % uri)

        if not uri.startswith(self.root_uri):
            urlparse.urljoin(self.root_uri, uri)

        request = self.create_request(uri, method, body, headers)

        # Get current queued mock response.
        response_headers, response_content = self.responses[self._response_index]
        response = Response()
        response.status_code = response_headers.pop('Status')
        response.headers = response_headers
        response._content = response_content
        self._response_index += 1

        # Set minimal response headers.
        response_headers = {
            'Server': 'Mock Client',
            'Status': 0,
        }
        response.headers.update(response_headers)

        return self.create_response(response, request)
示例#8
0
    def mock_send(method, status, headers=None, body=RESPONSE_BODY):
        if headers is None:
            headers = {}
        response = Response()
        response._content_consumed = True
        response._content = json.dumps(body).encode('ascii') if body is not None else None
        response.request = mock.create_autospec(Request)
        response.request.method = method
        response.request.url = RESOURCE_URL
        response.request.headers = {
            'x-ms-client-request-id': '67f4dd4e-6262-45e1-8bed-5c45cf23b6d9'
        }
        response.status_code = status
        response.headers = headers
        response.headers.update({"content-type": "application/json; charset=utf8"})
        response.reason = "OK"

        request = CLIENT._request(
            response.request.method,
            response.request.url,
            None,  # params
            response.request.headers,
            body,
            None,  # form_content
            None  # stream_content
        )

        return PipelineResponse(
            request,
            RequestsTransportResponse(
                request,
                response,
            ),
            None  # context
        )
示例#9
0
    def test_submitJob2Launcher(self, ua):
        from requests import Response
        create_url = 'http://localhost:9998/job/'
        create_url += '11111111-1111-1111-1111-111111111111'
        uresp = Response()
        uresp.status_code = 204
        uresp.headers = {'Location': create_url}
        ua.return_value = uresp
        body = {'foo': 'bar'}

        resp = self.factory.submitJob2Launcher(body)

        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
        }
        ua.assert_called_with(
            'http://localhost:9998/job',
            data='{"foo": "bar"}',
            headers=headers,
        )

        launcher_url = 'http://localhost:9998/job/'
        launcher_url += '11111111-1111-1111-1111-111111111111'
        self.assertEqual(resp, launcher_url)
示例#10
0
文件: __init__.py 项目: feedhq/feedhq
def responses(code, path=None, redirection=None, data=None,
              url=None,
              headers=None):
    if headers is None:
        headers = {'Content-Type': 'text/xml'}
    response = Response()
    response.status_code = code
    if path is not None and redirection is None:
        with open(data_file(path), 'rb') as f:
            response.raw = BytesIO(f.read())
    elif data is not None:
        response._content = data.encode('utf-8')
    if redirection is not None:
        temp = Response()
        temp.status_code = 301 if 'permanent' in redirection else 302
        temp.url = path
        response.history.append(temp)
        response.url = redirection
        headers['location'] = path
    if url is None:
        if redirection is not None:
            url = redirection
        else:
            url = 'https://example.com/{}'.format(str(uuid4()))
    response.url = url
    response.headers = headers
    return response
示例#11
0
def responses(code,
              path=None,
              redirection=None,
              data=None,
              url=None,
              headers={'Content-Type': 'text/xml'}):
    response = Response()
    response.status_code = code
    if path is not None and redirection is None:
        with open(data_file(path), 'rb') as f:
            response.raw = BytesIO(f.read())
    elif data is not None:
        response._content = data.encode('utf-8')
    if redirection is not None:
        temp = Response()
        temp.status_code = 301 if 'permanent' in redirection else 302
        temp.url = path
        response.history.append(temp)
        response.url = redirection
        headers['location'] = path
    if url is None:
        if redirection is not None:
            url = redirection
        else:
            url = 'https://example.com/{}'.format(str(uuid4()))
    response.url = url
    response.headers = headers
    return response
示例#12
0
    def test_validate_headers_wrong(self):
        r = Response()
        r.status_code = 200
        r.headers = {"fake": "header"}

        with pytest.raises(test_utils.HeadersMismatchError):
            test_utils.validate_response(r, expected_headers={"fake": "wrong"})
示例#13
0
def test_delay_extraction():
    polling = LROBasePolling()
    headers = {}

    response = Response()
    response.headers = headers

    polling._pipeline_response = PipelineResponse(
        None,
        RequestsTransportResponse(
            None,
            response,
        ),
        None  # context
    )

    headers['retry-after'] = "10"
    assert polling._extract_delay() == 10

    # Test that I need to retry exactly one hour after, by mocking "now"
    headers['retry-after'] = "Mon, 20 Nov 1995 19:12:08 -0500"

    from datetime import datetime as basedatetime
    now_mock_datetime = datetime.datetime(1995, 11, 20, 18, 12, 8, tzinfo=_FixedOffset(-5*60))
    with mock.patch('datetime.datetime') as mock_datetime:
        mock_datetime.now.return_value = now_mock_datetime
        mock_datetime.side_effect = lambda *args, **kw: basedatetime(*args, **kw)

        assert polling._extract_delay() == 60*60  # one hour in seconds
        assert str(mock_datetime.now.call_args[0][0]) == "<FixedOffset -5.0>"
示例#14
0
 def new_response(self, response):
     """Convert an tornado.HTTPResponse object to a requests.Response object"""
     new = Response()
     new._content = response.body
     new.status_code = response.code
     new.headers = dict(response.headers.get_all())
     return new
示例#15
0
def build_response(request,
                   status_code=200,
                   headers={},
                   content='(none)'):
    """
    Build a :class:`requests.Response` object on the basis of the passed
    parameters.
    """

    response = Response()

    response.status_code = status_code
    response.reason = responses[status_code]
    response.headers = CaseInsensitiveDict(headers)
    # Pretend that we've already read from the socket
    response._content = content

    response.encoding = get_encoding_from_headers(response.headers)
    response.url = request.url
    response.raw = MockRawResponse()

    # Give the Response some context.
    response.request = request
    response.connection = MockConnection()

    return response
示例#16
0
    def test_get_user_command__bad_response(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email of a non-existing user in the application
        When:
            - Calling function get_user_command
            - A bad response (500) is returned from the application API
        Then:
            - Ensure the resulted User Profile object holds information about the bad response.
        """
        import demistomock as demisto

        client = mock_client(mocker)
        args = {'user-profile': {'email': '*****@*****.**'}}

        bad_response = Response()
        bad_response.headers = {
            'x-redlock-status': json.dumps([{
                'i18nKey': 'bad request'
            }])
        }
        bad_response.status_code = 500

        mocker.patch.object(demisto, 'error')
        mocker.patch.object(Session, 'request', return_value=bad_response)

        user_profile = IAMCommand().get_user(client, args)
        outputs = get_outputs_from_user_profile(user_profile)

        assert outputs.get('action') == IAMActions.GET_USER
        assert outputs.get('success') is False
        assert outputs.get('errorCode') == 500
        assert 'Error in API call [500] - bad request' in outputs.get(
            'errorMessage')
    def test__get_file_content_as_attachment(self):
        mock_response = Response()
        mock_response.status_code = 200
        mock_response.raw = SimpleUploadedFile('sample.txt', b'abc')
        mock_response.headers = {'Content-Type': 'testing'}

        with mock.patch('aether.sdk.utils.request',
                        return_value=mock_response) as mock_get:
            response = utils.get_file_content(
                '/a/b/sample.txt',
                'http://any-server/sample.txt',
                True,
            )
            self.assertTrue(isinstance(response, FileResponse))
            mock_get.assert_called_once_with(
                url='http://any-server/sample.txt',
                method='get',
                stream=True,
            )

            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.getvalue(), b'abc')
            self.assertEqual(response['Content-Type'], 'testing')

            self.assertEqual(response['Content-Disposition'],
                             'attachment; filename="sample.txt"')
            self.assertEqual(response['Access-Control-Expose-Headers'],
                             'Content-Disposition')
示例#18
0
def successful_github_response(url, *_args, **_kwargs):
    r = Response()
    r.url = url
    r.status_code = 200
    r.reason = 'OK'
    r.headers = {
        'Content-Type': 'application/json; charset=utf-8',
    }
    r.raw = BytesIO(b'''[
        {
            "html_url": "https://github.com/netbox-community/netbox/releases/tag/v2.7.8",
            "tag_name": "v2.7.8",
            "prerelease": false
        },
        {
            "html_url": "https://github.com/netbox-community/netbox/releases/tag/v2.6-beta1",
            "tag_name": "v2.6-beta1",
            "prerelease": true
        },
        {
            "html_url": "https://github.com/netbox-community/netbox/releases/tag/v2.5.9",
            "tag_name": "v2.5.9",
            "prerelease": false
        }
    ]
    ''')
    return r
示例#19
0
def list_vpc_response():
    list_vpc_response = Response()
    list_vpc_response.status_code = 200
    list_vpc_response._content = b"""
        {
            "vpcs": [
                {
                    "id": "0378f905-2ae8-4c75-a9fe-575ec11fddc9",
                    "name": "Hello123556WorldVpc",
                    "description": "",
                    "cidr": "192.168.1.0/24",
                    "status": "OK",
                    "routes": [],
                    "enterprise_project_id": "0"
                },
                {
                    "id": "27349a49-a1b8-48fc-aed5-30d183c6844b",
                    "name": "HelloWorld123Vpc",
                    "description": "6Pjkx7B0Yze81L",
                    "cidr": "192.168.1.0/24",
                    "status": "OK",
                    "routes": [],
                    "enterprise_project_id": "0"
                }
            ]
        }
        """
    list_vpc_response.headers = {"X-Request-Id": "list-vpcs-request"}
    list_vpc_response.request = Request()
    list_vpc_response.request.method = "GET"
    list_vpc_response.request.url = ""

    yield list_vpc_response
示例#20
0
文件: localfs.py 项目: Korijn/conda
    def send(self, request, stream=None, timeout=None, verify=None, cert=None, proxies=None):
        pathname = url_to_path(request.url)

        resp = Response()
        resp.status_code = 200
        resp.url = request.url

        try:
            stats = stat(pathname)
        except OSError as exc:
            resp.status_code = 404
            resp.raw = exc
        else:
            modified = formatdate(stats.st_mtime, usegmt=True)
            content_type = guess_type(pathname)[0] or "text/plain"
            resp.headers = CaseInsensitiveDict({
                "Content-Type": content_type,
                "Content-Length": stats.st_size,
                "Last-Modified": modified,
            })

            resp.raw = open(pathname, "rb")
            resp.close = resp.raw.close

        return resp
示例#21
0
def test_extract_http_response_content():
    response = Response()
    response.status_code = 200
    response.headers = {'Content-Type': 'application/json'}
    response_content = core.extract_http_response_content(response)

    assert all(key in response_content for key in ('status_code', ))
示例#22
0
def successful_github_response(url, *_args, **_kwargs):
    r = Response()
    r.url = url
    r.status_code = 200
    r.reason = "OK"
    r.headers = {
        "Content-Type": "application/json; charset=utf-8",
    }
    r.raw = BytesIO(b"""[
        {
            "html_url": "https://github.com/nautobot/nautobot/releases/tag/v2.7.8",
            "tag_name": "v2.7.8",
            "prerelease": false
        },
        {
            "html_url": "https://github.com/nautobot/nautobot/releases/tag/v2.6-beta1",
            "tag_name": "v2.6-beta1",
            "prerelease": true
        },
        {
            "html_url": "https://github.com/nautobot/nautobot/releases/tag/v2.5.9",
            "tag_name": "v2.5.9",
            "prerelease": false
        }
    ]
    """)
    return r
示例#23
0
    def test_validate_error_no_error(self):
        r = Response()
        r.status_code = 200
        r.headers = dict()

        with pytest.raises(test_utils.NoErrorReturnedError):
            test_utils.validate_error(r, errors_api.InvalidMethodError)
示例#24
0
    def test_validate_paging_no_urls_raises(self):
        r = Response()
        r.status_code = 200
        r.headers = {
            "paging-next": "www.url.com/next",
            "paging-previous": "www.url.com/previous",
            "paging-offset": 5,
            "paging-limit": 5,
            "paging-current-page": 2,
            "paging-total-pages": 10,
            "paging-total-items": 50,
        }

        expected_paging = PagingResp(
            next=None,
            previous=None,
            offset=5,
            limit=5,
            current_page=2,
            total_pages=10,
            total_items=50,
        )

        with pytest.raises(test_utils.PagingMismatchError):
            test_utils.validate_response(r, expected_paging=expected_paging)
def test_report_chunked_response(mocker):
    """
    Given:
     - hash of file.

    When:
     - Running report command.

    Then:
     - outputs is valid.
    """
    mocker.patch.object(demisto, 'results')
    get_sample_response = Response()
    get_sample_response.status_code = 200
    get_sample_response.headers = {
        'Server': 'nginx',
        'Date': 'Thu, 28 May 2020 15:03:35 GMT',
        'Transfer-Encoding': 'chunked',
        'Connection': 'keep-alive',
        'x-envoy-upstream-service-time': '258'
    }
    get_sample_response._content = b'<?xml version="1.0" encoding="UTF-8"?><wildfire><version>2.0</version><file_info>' \
                                   b'<file_signer>None</file_signer><malware>no</malware><sha1></sha1><filetype>PDF' \
                                   b'</filetype><sha256>' \
                                   b'8decc8571946d4cd70a024949e033a2a2a54377fe9f1c1b944c20f9ee11a9e51</sha256><md5>' \
                                   b'4b41a3475132bd861b30a878e30aa56a</md5><size>3028</size></file_info><task_info>' \
                                   b'<report><version>2.0</version><platform>100</platform><software>' \
                                   b'PDF Static Analyzer</software><sha256>' \
                                   b'8decc8571946d4cd70a024949e033a2a2a54377fe9f1c1b944c20f9ee11a9e51</sha256>' \
                                   b'<md5>4b41a3475132bd861b30a878e30aa56a</md5><malware>no</malware><summary/>' \
                                   b'</report></task_info></wildfire>'
    mocker.patch('requests.request', return_value=get_sample_response)
    mocker.patch.object(
        demisto,
        "args",
        return_value={
            'hash':
            '8decc8571946d4cd70a024949e033a2a2a54377fe9f1c1b944c20f9ee11a9e51',
            'format': 'xml'
        })
    mocker.patch("Palo_Alto_Networks_WildFire_v2.URL",
                 "https://wildfire.paloaltonetworks.com/publicapi")
    command_results, status = wildfire_get_report_command({
        'hash':
        '8decc8571946d4cd70a024949e033a2a2a54377fe9f1c1b944c20f9ee11a9e51',
        'format': 'xml'
    })
    hr = '### WildFire File Report\n|FileType|MD5|SHA256|Size|Status|\n|---|---|---|---|---|\n|' \
         ' PDF | 4b41a3475132bd861b30a878e30aa56a | 8decc8571946d4cd70a024949e033a2a2a54377fe9f1c1b944c20f9ee11a9e51 |' \
         ' 3028 | Completed |\n'
    context = {
        'Status':
        'Success',
        'SHA256':
        '8decc8571946d4cd70a024949e033a2a2a54377fe9f1c1b944c20f9ee11a9e51'
    }

    assert command_results[0].outputs == context
    assert command_results[0].readable_output == hr
示例#26
0
def mock_response(content=None, return_headers=None, status_code=200):
    response = Response()
    response.status_code = status_code
    response.headers = return_headers
    if content:
        response._content = json.dumps(content).encode()

    return response
示例#27
0
    def response(self, requests):
        response = Response()
        response.raw = BytesIO(b"")
        response.headers = {"Content-Type": "dummy"}
        response.status_code = 200
        requests.get.return_value = response

        return response
示例#28
0
    def response(self, http_service):
        response = Response()
        response.raw = BytesIO(b"")
        response.headers = {"Content-Type": "dummy"}
        response.status_code = 200
        http_service.get.return_value = response

        return response
示例#29
0
    def test_data_schema_bson(self):
        r = Response()
        r.status_code = 200
        r._content = encode_bson({"first": "Harry", "last": "Potter"})
        r.headers = {"Content-Type": "application/bson"}

        name: Name = test_utils.validate_response(r, data_schema=NameSchema())
        assert name == Name("Harry", "Potter")
示例#30
0
def make_wrapper(json=None,
                 headers: Optional[Union[dict[str],
                                         CaseInsensitiveDict[str]]] = None):
    response = Response()
    response.raw = io.BytesIO(_json.dumps(json).encode() if json else b'')
    if headers:
        response.headers = CaseInsensitiveDict(headers)
    return ResponseWrapper(response)
示例#31
0
    def send(self,
             request,
             stream=None,
             timeout=None,
             verify=None,
             cert=None,
             proxies=None):

        resp = Response()
        resp.status_code = 200
        resp.url = request.url

        try:
            import boto
        except ImportError:
            stderrlog.info('\nError: boto is required for S3 channels. '
                           'Please install it with `conda install boto`\n'
                           'Make sure to run `source deactivate` if you '
                           'are in a conda environment.\n')
            resp.status_code = 404
            return resp

        conn = boto.connect_s3()

        bucket_name, key_string = url_to_s3_info(request.url)

        # Get the bucket without validation that it exists and that we have
        # permissions to list its contents.
        bucket = conn.get_bucket(bucket_name, validate=False)

        try:
            key = bucket.get_key(key_string)
        except boto.exception.S3ResponseError as exc:
            # This exception will occur if the bucket does not exist or if the
            # user does not have permission to list its contents.
            resp.status_code = 404
            resp.raw = exc
            return resp

        if key and key.exists:
            modified = key.last_modified
            content_type = key.content_type or "text/plain"
            resp.headers = CaseInsensitiveDict({
                "Content-Type": content_type,
                "Content-Length": key.size,
                "Last-Modified": modified,
            })

            _, self._temp_file = mkstemp()
            key.get_contents_to_filename(self._temp_file)
            f = open(self._temp_file, 'rb')
            resp.raw = f
            resp.close = resp.raw.close
        else:
            resp.status_code = 404

        return resp
示例#32
0
 async def new_response(self, response):
     """Convert an aiohttp.Response object to a requests.Response object"""
     new = Response()
     new._content = await response.read()
     new.status_code = response.status
     new.headers = response.headers
     new.cookies = response.cookies
     new.encoding = response.charset
     return new
示例#33
0
 async def new_response(self, response):
     """Convert an aiohttp.Response object to a requests.Response object"""
     new = Response()
     new._content = await response.read()
     new.status_code = response.status
     new.headers = response.headers
     new.cookies = response.cookies
     new.encoding = response.charset
     return new
示例#34
0
def unsuccessful_github_response(url, *_args, **_kwargs):
    r = Response()
    r.url = url
    r.status_code = 404
    r.reason = "Not Found"
    r.headers = {"Content-Type": "application/json; charset=utf-8"}
    r.raw = BytesIO(
        b'{"message": "Not Found","documentation_url": "https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository"}'
    )
    return r
示例#35
0
def successful_github_response(url, *_args, **_kwargs):
    r = Response()
    r.url = url
    r.status_code = 200
    r.reason = "OK"
    r.headers = {"Content-Type": "application/json; charset=utf-8"}
    r.raw = BytesIO(
        b'[{"html_url": "https://github.com/peering-manager/peering-manager/releases/tag/v1.1.0","tag_name": "v1.1.0","prerelease": false},{"html_url": "https://github.com/peering-manager/peering-manager/releases/tag/v1.1-beta1","tag_name": "v1.1-beta1","prerelease": true},{"html_url": "https://github.com/peering-manager/peering-manager/releases/tag/v1.0.0","tag_name": "v1.0.0","prerelease": false}]'
    )
    return r
示例#36
0
文件: s3.py 项目: ESSS/conda
    def send(self, request, stream=None, timeout=None, verify=None, cert=None, proxies=None):

        resp = Response()
        resp.status_code = 200
        resp.url = request.url

        try:
            import boto
        except ImportError:
            stderrlog.info('\nError: boto is required for S3 channels. '
                           'Please install it with `conda install boto`\n'
                           'Make sure to run `source deactivate` if you '
                           'are in a conda environment.\n')
            resp.status_code = 404
            return resp

        conn = boto.connect_s3()

        bucket_name, key_string = url_to_s3_info(request.url)

        # Get the bucket without validation that it exists and that we have
        # permissions to list its contents.
        bucket = conn.get_bucket(bucket_name, validate=False)

        try:
            key = bucket.get_key(key_string)
        except boto.exception.S3ResponseError as exc:
            # This exception will occur if the bucket does not exist or if the
            # user does not have permission to list its contents.
            resp.status_code = 404
            resp.raw = exc
            return resp

        if key and key.exists:
            modified = key.last_modified
            content_type = key.content_type or "text/plain"
            resp.headers = CaseInsensitiveDict({
                "Content-Type": content_type,
                "Content-Length": key.size,
                "Last-Modified": modified,
            })

            _, self._temp_file = mkstemp()
            key.get_contents_to_filename(self._temp_file)
            f = open(self._temp_file, 'rb')
            resp.raw = f
            resp.close = resp.raw.close
        else:
            resp.status_code = 404

        return resp
示例#37
0
文件: plugIt.py 项目: barroco/PlugIt
    def doQuery(self, url, method='GET', getParmeters=None, postParameters=None, files=None):
        """Send a request to the server and return the result"""

        if method == 'POST':
            if not files:
                r = requests.post(self.baseURI + '/' + url, params=getParmeters, data=postParameters, stream=True)
            else:  
                # Special way, for big files
                # Requests is not usable: https://github.com/shazow/urllib3/issues/51
                
                from poster.encode import multipart_encode, MultipartParam
                from poster.streaminghttp import register_openers
                import urllib2

                # Register the streaming http handlers with urllib2
                register_openers()

                # headers contains the necessary Content-Type and Content-Length
                # datagen is a generator object that yields the encoded parameters
                data = {}
                for x in postParameters:
                    data[x] = postParameters[x]

                for f in files:
                    data[f] = MultipartParam(f, fileobj=open(files[f].temporary_file_path(), 'rb'), filename=files[f].name)

                datagen, headers = multipart_encode(data)

                # Create the Request object
                request = urllib2.Request(self.baseURI + '/' + url, datagen, headers)

                re = urllib2.urlopen(request)

                from requests import Response

                r = Response()
                r.status_code = re.getcode()
                r.headers = re.info()
                r.encoding = "application/json"
                r.raw = re.read()
                r._content = r.raw

                return r

        else:
            # Call the function based on the method.
            r = getattr(requests, method.lower())(self.baseURI + '/' + url, params=getParmeters, stream=True)

        return r
示例#38
0
文件: __init__.py 项目: twidi/feedhq
def responses(code, path=None, redirection=None,
              headers={'Content-Type': 'text/xml'}):
    response = Response()
    response.status_code = code
    if path is not None:
        with open(test_file(path), 'r') as f:
            response.raw = BytesIO(f.read())
    if redirection is not None:
        temp = Response()
        temp.status_code = 301 if 'permanent' in redirection else 302
        temp.url = path
        response.history.append(temp)
        response.url = redirection
    response.headers = headers
    return response
    def setUpResponse(self, headers, body, status_code=200):
        if not headers:
            headers = {'Content-Type': 'application/json'}

        def decode_content():
            pass

        response = Response()
        response.status_code = status_code
        response.headers = headers
        stream = Mock()
        stream.read = Mock()
        stream.read.side_effect = [body, None]
        response.raw = stream

        return response
示例#40
0
    def test_response_headers(self):
        """
        Make sure that whatever headers come back on the response get added
        to the report.
        """
        URL = 'http://fakeurl/robots.txt'
        req = DownloadRequest(URL, StringIO(), headers={'pulp_header': 'awesome!'})
        response = Response()
        response.status_code = httplib.OK
        response.headers = {'content-length': '1024'}
        response.raw = StringIO('abc')
        self.session.get.return_value = response

        report = self.downloader._fetch(req)

        self.assertEqual(report.headers['content-length'], '1024')
示例#41
0
def make_fake_response(status, doc):
    """
    Helper function to conveniently build a fake Response instance from
    a status code and a dictionary, as if this is a response coming from the
    YesGraph API.
    """
    text = json.dumps(doc)
    body = text.encode('utf-8')  # body must be bytes

    response = Response()
    response.status_code = status
    response.headers = CaseInsensitiveDict({
        'Content-Type': 'application/json',
    })
    response.raw = io.BytesIO(body)
    return response
    def test_response_headers(self):
        """
        Make sure that whatever headers come back on the response get added
        to the report.
        """
        URL = 'http://pulpproject.org/robots.txt'
        req = DownloadRequest(URL, StringIO(), headers={'pulp_header': 'awesome!'})
        response = Response()
        response.status_code = httplib.OK
        response.headers = {'content-length': '1024'}
        response.raw = StringIO('abc')
        session = threaded.build_session(self.config)
        session.get = mock.MagicMock(return_value=response, spec_set=session.get)

        report = self.downloader._fetch(req, session)

        self.assertEqual(report.headers['content-length'], '1024')
示例#43
0
def fake_request(*args, **kwargs):
    r = Response()
    r.status_code = 200

    try:
        _token = kwargs['headers']['Authorization']
    except KeyError:
        r._content = b'{"shoe_size": 10}'
    else:
        _token = _token[7:]
        if _token == 'abcdef':
            r._content = b'{"shoe_size": 11}'
        else:
            r._content = b'{"shoe_size": 12}'

    r.headers = {'content-type': 'application/json'}
    return r
示例#44
0
def responses(code, path=None, redirection=None, data=None,
              headers={'Content-Type': 'text/xml'}):
    response = Response()
    response.status_code = code
    if path is not None and redirection is None:
        with open(data_file(path), 'rb') as f:
            response.raw = BytesIO(f.read())
    elif data is not None:
        response._content = data.encode('utf-8')
    if redirection is not None:
        temp = Response()
        temp.status_code = 301 if 'permanent' in redirection else 302
        temp.url = path
        response.history.append(temp)
        response.url = redirection
        headers['location'] = path
    response.headers = headers
    return response
示例#45
0
def make_json_response(data, status_code=200, headers=None):
    """
    :type data: dict
    :type status_code: int
    :type headers: dict

    :returns: A Response object with the corresponding JSON body
    :rtype: requests.models.Response
    """
    if headers is None:
        headers = {}

    response = Response()

    response.status_code = status_code
    response._content = json.dumps(data).encode("utf-8")

    headers["content-type"] = "application/json"
    response.headers = headers

    return response
示例#46
0
文件: test_job.py 项目: NLeSC/MAGMa
    def test_submitJob2Launcher(self, ua):
        from requests import Response
        create_url = 'http://localhost:9998/job/'
        create_url += '11111111-1111-1111-1111-111111111111'
        uresp = Response()
        uresp.status_code = 204
        uresp.headers = {'Location': create_url}
        ua.return_value = uresp
        body = {'foo': 'bar'}

        resp = self.factory.submitJob2Launcher(body)

        headers = {'Content-Type': 'application/json',
                   'Accept': 'application/json',
                   }
        ua.assert_called_with('http://localhost:9998/job',
                              data='{"foo": "bar"}',
                              headers=headers,
                              )

        launcher_url = 'http://localhost:9998/job/'
        launcher_url += '11111111-1111-1111-1111-111111111111'
        self.assertEquals(resp, launcher_url)
示例#47
0
    def build_response(self, req, resp):
        response = Response()

        response.status_code = resp.status_code
        response.headers = CaseInsensitiveDict((k, v) for k, v in resp.items())

        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = StringIO(resp.content)
        response.reason = None

        if isinstance(req.url, bytes):
            response.url = req.url.decode('utf-8')
        else:
            response.url = req.url

        # Convert from django's SimpleCookie to request's CookieJar
        cookiejar_from_dict(resp.cookies, response.cookies)

        # context
        response.request = req
        response.connection = self

        response = dispatch_hook('response', req.hooks, response)
        return response
示例#48
0
def response(status_code=200, content='', headers={}):
    response = Response()
    response.status_code = status_code
    response.raw = BytesIO(content.encode('utf-8'))
    response.headers = headers
    return response
示例#49
0
    def doQuery(self, url, method='GET', getParmeters=None, postParameters=None, files=None, extraHeaders={}, session={}):
        """Send a request to the server and return the result"""

        # Build headers
        headers = {}

        for key, value in extraHeaders.iteritems():
            headers['X-Plugit-' + key] = value

        for key, value in session.iteritems():
            headers['X-Plugitsession-' + key] = value

        if method == 'POST':
            if not files:
                r = requests.post(self.baseURI + '/' + url, params=getParmeters, data=postParameters, stream=True, headers=headers)
            else:
                # Special way, for big files
                # Requests is not usable: https://github.com/shazow/urllib3/issues/51

                from poster.encode import multipart_encode, MultipartParam
                from poster.streaminghttp import register_openers
                import urllib2

                # Register the streaming http handlers with urllib2
                register_openers()

                # headers contains the necessary Content-Type and Content-Length
                # datagen is a generator object that yields the encoded parameters
                data = []
                for x in postParameters:
                    if isinstance(postParameters[x], list):
                        for elem in postParameters[x]:
                            data.append((x, elem))
                    else:
                        data.append((x, postParameters[x]))

                for f in files:
                    data.append((f, MultipartParam(f, fileobj=open(files[f].temporary_file_path(), 'rb'), filename=files[f].name)))

                datagen, headers_multi = multipart_encode(data)

                headers.update(headers_multi)

                # Create the Request object
                request = urllib2.Request(self.baseURI + '/' + url, datagen, headers)

                re = urllib2.urlopen(request)

                from requests import Response

                r = Response()
                r.status_code = re.getcode()
                r.headers = dict(re.info())
                r.encoding = "application/json"
                r.raw = re.read()
                r._content = r.raw

                return r

        else:
            # Call the function based on the method.
            r = requests.request(method.upper(), self.baseURI + '/' + url, params=getParmeters, stream=True, headers=headers, allow_redirects=True)

        return r
def create_response(body, status, headers=None):
    res = Response()
    res.headers = headers
    res._content = body
    res.status_code = status
    return res
示例#51
0
 def fake_post(self, *args, **kwargs):
     fake_response = Response()
     fake_response.status_code = 200
     fake_response.headers = {'location': 'http://deploy/url/123'}
     return fake_response
示例#52
0
def build_response(reason=None, status_code=200, headers={}):
  response = Response()
  response.status_code = status_code
  response.headers = headers
  response.reason = reason
  return response
 def _():
     r = Response()
     r.status_code = 429
     r.headers = {'Retry-After': 3}
     raise LambdaCrawlerException(r)