Example #1
1
def test_summary(mock_request):

    # Given
    url = 'http://www.google.com'
    mock_response = Response()
    mock_response.headers.get = MagicMock(return_value = 'html')
    mock_response.url = url
    mock_response.encoding = 'UTF-8'
    mock_response.consumed = False
    mock_response.raw = MagicMock()
    mock_response.iter_content = lambda s: ['<html><head><title>Test Title</head><body></body></html>']
    mock_request.return_value = mock_response

    # When
    summ = Summary(url)
    # summ._html = '<html><head><title>Test Title</head><body></body></html>'
    summ.extract()

    # Then
    # mock_response.raw.close.assert_called_with()
    assert summ.title == 'Test Title'
Example #2
0
    def test_upload_app_poll_status(self):
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/'
        }

        response_not_completed = Response()
        response_not_completed.status_code = 200
        response_not_completed.encoding = 'utf-8'
        response_not_completed._content = json.dumps(
            {'completed': False, 'application_url': ''}).encode('utf-8')
        response_completed = Response()
        response_completed.status_code = 200
        response_completed.encoding = 'utf-8'
        response_completed._content = json.dumps(
            {'completed': True, 'revision': 14,
             'application_url': 'http://example.org'}).encode('utf-8')
        self.mock_get.side_effect = [
            response_not_completed,
            response_not_completed,
            response_completed,
        ]
        response = upload_app(self.package_name, self.data)
        self.assertEqual(response, {
            'success': True,
            'errors': [],
            'application_url': 'http://example.org',
            'revision': 14,
        })
        self.assertEqual(self.mock_get.call_count, 3)
Example #3
0
def request_mookup(*args, **kwargs):
    method = kwargs["method"]
    url = kwargs["url"]
    if not url.startswith(SERVER + "/"):
        raise ValueError("URL [{}] does not start with [{}/].".format(
            url, SERVER))
    parts = url[len(SERVER) + 1:].split("?")
    url = parts[0]
    params = parts[1] if len(parts) > 1 else None

    response = Response()
    response.url = kwargs["url"]

    response_file = os.path.join(RESPONSE_ROOT, url, method)
    try:
        with open(response_file) as f:
            data = {"responses": {}, "__builtins__": {}}
            exec(f.read(), data)
            data = data["responses"][params]
            if type(data) is dict:
                if "status_code" in data:
                    response.status_code = data.pop("status_code")
                else:
                    response.status_code = 200

                # Extend the links with the server
                for item in [None, "owner", "project", "workspace"]:
                    cur_dict = data if item is None else data.get(item, {})
                    if "links" in cur_dict:
                        for link in cur_dict["links"].values():
                            try:
                                link["href"] = "{}/{}".format(
                                    SERVER, link["href"])
                            except:
                                pass
                if "next" in data:
                    data["next"] = "{}/{}".format(SERVER, data["next"])

                response.encoding = "utf-8"
                response._content = bytes(json.dumps(data), response.encoding)
            else:
                response.status_code = 200
                response._content = data
    except FileNotFoundError:
        response.encoding = "utf-8"
        response._content = b"{}"
        response.status_code = 404  # Not found
        response.reason = "No stub defined [{}]".format(response_file)
    except KeyError:
        response.encoding = "utf-8"
        response._content = b"{}"
        response.status_code = 404  # Not found
        response.reason = "No stub defined for param [{}]".format(params)
    except Exception as e:
        raise e

    return response
Example #4
0
def request_mookup(*args, **kwargs):
    method = kwargs["method"]
    url = kwargs["url"]
    if not url.startswith(SERVER + "/"):
        raise ValueError("URL [{}] does not start with [{}/].".format(url, SERVER))
    parts = url[len(SERVER) + 1 :].split("?")
    url = parts[0]
    response_key = parts[1] if len(parts) > 1 else None
    if kwargs["data"] is not None:
        response_key = str(kwargs["data"])

    response = Response()
    response.url = kwargs["url"]

    response_file = os.path.join(RESPONSE_ROOT, url, method)
    try:
        with open(response_file) as f:
            data = {"responses": {}, "__builtins__": {}, "true": True, "false": False}
            exec(f.read(), data)
            data = data["responses"][response_key]
            if type(data) is dict:
                if "status_code" in data:
                    response.status_code = data.pop("status_code")
                else:
                    response.status_code = 200

                # Extend the links with the server
                for item in [None, "owner", "project", "workspace"]:
                    # Use values of paged response
                    for elem in data["values"] if "values" in data else [data]:
                        cur_dict = elem if item is None else elem.get(item, {})
                        if "links" in cur_dict:
                            for link in cur_dict["links"].values():
                                for ld in link if type(link) is list else [link]:
                                    ld["href"] = "{}/{}".format(SERVER, ld["href"])
                if "next" in data:
                    data["next"] = "{}/{}".format(SERVER, data["next"])

                response.encoding = "utf-8"
                response._content = bytes(json.dumps(data), response.encoding)
            else:
                response.status_code = 200
                response._content = data
    except FileNotFoundError:
        response.encoding = "utf-8"
        response._content = b"{}"
        response.status_code = 404  # Not found
        response.reason = "No stub defined [{}]".format(response_file)
    except KeyError:
        response.encoding = "utf-8"
        response._content = b"{}"
        response.status_code = 404  # Not found
        response.reason = "No stub defined for key [{}] in [{}]".format(response_key, response_file)

    return response
Example #5
0
 def test_extract_response(self) -> None:
     fake_response = Response()
     fake_response.encoding = "UTF-8"
     fake_response._content = bytes(  # type: ignore
         '{"a": 1}', fake_response.encoding)
     self.assertEqual(self.instance.extract_response(fake_response),
                      {"a": 1})
Example #6
0
 def article2(self):
     resp = Response()
     resp.url = 'https://www.youtube.com/watch?v=scbrjaqM3Oc'
     resp.encoding = 'utf8'
     with open('tests/fixtures/article-2.html', 'rb') as fd:
         resp._content = fd.read()
     return resp
Example #7
0
 def side_effect():
     response = Response()
     response.status_code = 200
     response._content = bytes(
         '{\"metadata\": {\"total\": 1}, \"data\": [] }', 'utf-8')
     response.encoding = 'utf-8'
     return response
Example #8
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
Example #9
0
    def test_upload_app_ignore_non_ok_responses(self):
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/',
        }

        ok_response = Response()
        ok_response.status_code = 200
        ok_response.encoding = 'utf-8'
        ok_response._content = json.dumps(
            {'completed': True, 'revision': 14}).encode('utf-8')
        nok_response = Response()
        nok_response.status_code = 503

        self.mock_get.side_effect = [nok_response, nok_response, ok_response]
        response = upload_app(self.package_name, self.data)
        self.assertEqual(response, {
            'success': True,
            'errors': [],
            'application_url': '',
            'revision': 14,
        })
        self.assertEqual(self.mock_get.call_count, 3)
    def test_store(self):
        self.rp.decrypt_survey = MagicMock(return_value=valid_json)
        self.rp.validate_survey = MagicMock()
        # <store>
        self.rp.send_receipt = MagicMock()
        self.rp.send_notification = MagicMock()

        r = Response()
        with mock.patch('app.response_processor.ResponseProcessor.remote_call') as call_mock:
            call_mock.return_value = r

            # 500 - retry
            r.status_code = 500
            with self.assertRaises(RetryableError):
                self._process()

            # 400 - bad
            r.status_code = 400
            with self.assertRaises(QuarantinableError):
                self._process()

            # 200 - ok
            r.status_code = 200
            r.encoding = 'utf-8'
            self._process()
    def test_upload_app_ok(self):
        # fake upload response
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/',
        }
        # fake poll status response
        application_url = 'http://example.com/app/1'
        revision = '1'
        ok_response = Response()
        ok_response.status_code = 200
        ok_response.encoding = 'utf-8'
        ok_response._content = json.dumps({
            'completed': True,
            'revision': revision,
            'application_url': application_url
        }).encode('utf-8')
        self.mock_get.return_value = ok_response

        success = upload(self.binary_file.name, 'foo')

        self.assertTrue(success)
        self.assertIn(
            call('Application uploaded successfully (as revision {})'.format(
                revision)), self.mock_logger.info.call_args_list)
        self.assertIn(
            call('Please check out the application at: %s\n', application_url),
            self.mock_logger.info.call_args_list)
def parse_stream(response: requests.Response) -> Iterator[Mapping[str, Any]]:
    """Parses a stream response containing one detection batch.

  The requests library provides utilities for iterating over the HTTP stream
  response, so we do not have to worry about chunked transfer encoding. The
  response is a stream of bytes that represent a json array.
  Each top-level element of the json array is a detection batch. The array is
  "never ending"; the server can send a batch at any time, thus
  adding to the json array.

  Args:
    response: The requests.Response Object returned from post().

  Yields:
    Dictionary representations of each detection batch that was sent over the
    stream.
  """
    try:
        if response.encoding is None:
            response.encoding = "utf-8"

        for line in response.iter_lines(decode_unicode=True, delimiter="\r\n"):
            if not line:
                continue

            # Trim all characters before first opening brace, and after last closing
            # brace. Example:
            #   Input:  "  {'key1': 'value1'},  "
            #   Output: "{'key1': 'value1'}"
            json_string = "{" + line.split("{", 1)[1].rsplit("}", 1)[0] + "}"
            yield json.loads(json_string)

    except Exception as e:  # pylint: disable=broad-except
        # Chronicle's servers will generally send a {"error": ...} dict over the
        # stream to indicate retryable failures (e.g. due to periodic internal
        # server maintenance), which will not cause this except block to fire.
        #
        # In rarer cases, the streaming connection may silently fail; the
        # connection will close without an error dict, which manifests as a
        # requests.exceptions.ChunkedEncodingError; see
        # https://github.com/urllib3/urllib3/issues/1516 for details from the
        # `requests` and `urllib3` community.
        #
        # Instead of allowing streaming clients to crash (for ChunkedEncodingErrors,
        # and for other Exceptions that may occur while reading from the stream),
        # we will catch exceptions, then yield a dict containing the error,
        # so the client may report the error, then retry connection (with backoff,
        # and retry limit).
        yield {
            "error": {
                "code":
                500,
                "status":
                "UNAVAILBLE",
                "message":
                "exception caught while reading stream response (your "
                "streaming client should retry connection): {}".format(
                    repr(e)),
            }
        }
Example #13
0
    def test_make_request(self, mocked_resp):
        resp = Response()
        resp.status_code = 200
        resp._content = b'{"file_name": "test.yml", "env": "staging", "hash": "\xe5\xad\x97"}'
        resp.encoding = 'UTF-8'
        mocked_resp.return_value = resp

        headers = {}
        params = {
            'method': 'GET',
            'uri': 'https://*****:*****@host:123/path/?a=b&c=d',
            'headers': headers,
            'data':
            b'C\xcfI\x91\xc1\xd0\tw<\xa8\x13\x06{=\x9b\xb3\x1c\xfcl\xfe\xb9\xb18zS\xf4%i*Q\xc9v',
            'access_key': '',
            'private_key': 'Mzjg58S93/qdg0HuVP6PsLSRDTe+fQZ5++v/mkUUx4k=',
            'data_binary': True
        }
        r = make_request(**params)

        expected = u'хнЧ'

        ### assert that the unicode character is in the response.text output
        self.assertTrue(expected in r.text)

        ### assert that the unicode character is _not_ in the response.text which has been converted to bytes
        self.assertFalse(expected in str(r.text.encode('utf-8')))

        pass
    def test_upload_app_ok_without_revision(self):
        # fake upload response
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/',
        }
        # fake poll status response
        ok_response = Response()
        ok_response.status_code = 200
        ok_response.encoding = 'utf-8'
        ok_response._content = json.dumps(
            {'completed': True}).encode('utf-8')
        self.mock_get.return_value = ok_response

        success = upload(self.binary_file.name)

        self.assertTrue(success)
        self.assertIn(
            call('Application uploaded successfully.'),
            self.mock_logger.info.call_args_list)
        self.assertNotIn(
            call('Uploaded as revision %s.', ANY),
            self.mock_logger.info.call_args_list)
    def test_upload_app_ignore_non_ok_responses(self):
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/',
        }

        ok_response = Response()
        ok_response.status_code = 200
        ok_response.encoding = 'utf-8'
        ok_response._content = json.dumps({
            'completed': True,
            'revision': 14
        }).encode('utf-8')
        nok_response = Response()
        nok_response.status_code = 503

        self.mock_get.side_effect = [nok_response, nok_response, ok_response]
        response = upload_app(self.package_name, self.data)
        self.assertEqual(response, {
            'success': True,
            'errors': [],
            'application_url': '',
            'revision': 14,
        })
        self.assertEqual(self.mock_get.call_count, 3)
Example #16
0
    def test_upload_app_ok(self):
        # fake upload response
        mock_response = self.mock_post.return_value
        mock_response.ok = True
        mock_response.return_value = {
            'success': True,
            'status_url': 'http://example.com/status/',
        }
        # fake poll status response
        application_url = 'http://example.com/app/1'
        revision = '1'
        ok_response = Response()
        ok_response.status_code = 200
        ok_response.encoding = 'utf-8'
        ok_response._content = json.dumps(
            {'completed': True, 'revision': revision,
             'application_url': application_url}).encode('utf-8')
        self.mock_get.return_value = ok_response

        success = upload(self.binary_file.name, 'foo')

        self.assertTrue(success)
        self.assertIn(
            call('Application uploaded successfully (as revision {})'.format(
                revision)),
            self.mock_logger.info.call_args_list)
        self.assertIn(call('Please check out the application at: %s\n',
                           application_url),
                      self.mock_logger.info.call_args_list)
    def test_store(self):
        self.rp.decrypt_survey = MagicMock(return_value=valid_json)
        self.rp.validate_survey = MagicMock()
        # <store>
        self.rp.send_receipt = MagicMock()
        self.rp.send_notification = MagicMock()

        r = Response()
        with mock.patch('app.response_processor.ResponseProcessor.remote_call'
                        ) as call_mock:
            call_mock.return_value = r

            # 500 - retry
            r.status_code = 500
            # Set content to keep r.json() function happy
            r._content = b'{}'  # pylint:disable=protected-access
            with self.assertRaises(RetryableError):
                self._process()

            # 400 - bad
            r.status_code = 400
            # Set content to keep r.json() function happy
            r._content = b'{}'  # pylint:disable=protected-access
            with self.assertRaises(QuarantinableError):
                self._process()

            # 200 - ok
            r.status_code = 200
            r.encoding = 'utf-8'
            self._process()
Example #18
0
 def from_json(cls, raw, connection=None):
     resp = Response()
     data = json.dumps(raw)
     resp._content = bytes(data, 'utf-8')
     resp.encoding = 'utf-8'
     resp.status_code = 200
     return cls(r=resp, connection=connection)
Example #19
0
def fake_response_handler(url=None, **kwargs):
    r = Response()
    r.status_code = 200
    r.encoding = "utf-8"
    assert url in fake_responses, "unknown URL: " + url
    r._content = fake_responses[url]
    return r
Example #20
0
    def test_authorized(self, mocked_class):
        test_token_info = {
            'https://auth.data.humancellatlas.org/email':
            '*****@*****.**'
        }
        resp = Response()
        resp.encoding = 'utf-8'
        resp.status_code = 200
        mocked_class.return_value = resp

        @authz.authorize(self.gcp_creds, ['test:Action'],
                         ['aws:test:resource:place:21351345134'])
        def _test(token_info: dict):
            pass

        resp._content = json.dumps({'result': True}).encode('utf-8')
        with self.subTest("result = True"):
            _test(token_info=test_token_info)

        resp._content = json.dumps({'result': False}).encode('utf-8')
        with self.subTest("result = False"):
            with self.assertRaises(AuthorizationException):
                _test(token_info=test_token_info)

        resp.status_code = 500
        with self.subTest("Request failed"):
            with self.assertRaises(AuthorizationException):
                _test(token_info=test_token_info)
Example #21
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
Example #22
0
def fake_response_handler(url=None, **kwargs):
    r = Response()
    r.status_code = 200
    r.encoding = "utf-8"
    assert url in fake_responses, "unknown URL: " + url
    r._content = fake_responses[url]
    return r
Example #23
0
 def article(self):
     resp = Response()
     resp.url = 'http://www.pariszigzag.fr/paris-insolite-secret/'\
                'les-plus-belles-boulangeries-de-paris'
     resp.encoding = 'utf8'
     with open('tests/fixtures/article.html', 'rb') as fd:
         resp._content = fd.read()
     return resp
Example #24
0
def test_extract_http_response_content_check_for_json_key():
    response = Response()
    response.status_code = 200
    response.encoding = 'UTF-8'
    response._content = dumps({'content': True}).encode()
    response_content = core.extract_http_response_content(response)

    assert 'data' in response_content
 def _query_anitya_url(host_url, api_path):
     assert api_path.endswith(expected_suffix)
     result = Response()
     result.status_code = 200
     result.encoding = 'utf-8'
     dummy_data = {'api_path': api_path, 'packages': dummy_packages}
     result._content = json.dumps(dummy_data).encode(result.encoding)
     return result
def make_response(filename, status_code = 200):
    res = Response()
    res.status_code = status_code
    res.header = None
    res.encoding = 'utf-8'
    json_loaded = load_json(filename)
    res._content = json.dumps(json_loaded).encode('utf-8')
    return res
Example #27
0
def build_response(request, data, code, reason, headers={}, encoding=None):
    """Builds a response object from the data"""
    response = Response()
    response.status_code = code

    for k, v in headers.items():
        response.headers[k] = v
    response.encoding = encoding
    if not encoding:
        response.encoding = get_encoding_from_headers(response.headers)
    response.raw = data
    response._content = data
    response.url = request.url
    response.request = request
    response.reason = reason

    return response
 def _end_request(res: Response, encoding: str) -> Response:
     """
     request之后的处理,可根据逻辑重载
     :param res: request之后的Response对象
     :return: 处理之后的Response对象
     """
     res.encoding = encoding
     return res
Example #29
0
 def get_response(self, scheme='http:'):
     resp = Response()
     resp.url = scheme + self.response_url
     resp.status_code = 200
     resp.encoding = 'utf8'
     resp.headers['content-type'] = 'text/html'
     with open('tests/fixtures/article.html') as fd:
         setattr(resp, '_content', fd.read())
     return resp
Example #30
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
Example #31
0
def _get_dummy_response(body, status_code=200) -> Response:
    body_encoding = "utf-8"
    body_string = body if isinstance(body, str) else json.dumps(body)
    body_bytes = bytes(body_string, body_encoding)
    response = Response()
    response.status_code = status_code
    response.encoding = body_encoding
    response._content = body_bytes
    return response
Example #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
Example #33
0
def _response_as_json(response: requests.Response) -> Dict[Text, Any]:
    """Convert a HTTP response to json, raise exception if response failed."""

    response.raise_for_status()

    if response.encoding is None:
        response.encoding = 'utf-8'

    return response.json()
Example #34
0
 def response2(self):
     resp = Response()
     resp.status_code = 200
     resp.url = 'https://www.youtube.com/watch?v=scbrjaqM3Oc'
     resp.encoding = 'utf8'
     resp.headers['content-type'] = 'text/html'
     with open('tests/fixtures/article-2.html') as fd:
         setattr(resp, '_content', fd.read())
     return resp
Example #35
0
def create_response(body, status, headers=None):
    res = Response()
    res.headers = headers
    if is_third():
        res._content = bytes(body, 'utf8')
        res.encoding = 'utf8'
    else:
        res._content = body
    res.status_code = status
    return res
Example #36
0
def test_send_message_json_OK(mock_send):
    svc = OnapService()
    mocked_response = Response()
    mocked_response._content = b'{"yolo": "yala"}'
    mocked_response.encoding = "UTF-8"
    mocked_response.status_code = 200
    mock_send.return_value = mocked_response
    response = svc.send_message_json("GET", 'test get', 'http://my.url/')
    mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/')
    assert response['yolo'] == 'yala'
Example #37
0
 def get(self, method, url, *a, **kw):
     assert method == "GET"
     assert url == collection_url
     r = Response()
     r.status_code = 200
     assert responses
     r._content = responses.pop().encode("utf-8")
     r.headers["Content-Type"] = "text/calendar"
     r.encoding = "ISO-8859-1"
     return r
Example #38
0
 def get(self, method, url, *a, **kw):
     assert method == 'GET'
     assert url == collection_url
     r = Response()
     r.status_code = 200
     assert responses
     r._content = responses.pop().encode('utf-8')
     r.headers['Content-Type'] = 'text/icalendar'
     r.encoding = 'ISO-8859-1'
     return r
Example #39
0
 def get(self, method, url, *a, **kw):
     assert method == 'GET'
     assert url == collection_url
     r = Response()
     r.status_code = 200
     assert responses
     r._content = responses.pop().encode('utf-8')
     r.headers['Content-Type'] = 'text/icalendar'
     r.encoding = 'ISO-8859-1'
     return r
Example #40
0
def test_send_message_json_bad_json_no_exception(mock_send):
    svc = OnapService()
    mocked_response = Response()
    mocked_response._content = b'yolo'
    mocked_response.encoding = "UTF-8"
    mocked_response.status_code = 200
    mock_send.return_value = mocked_response
    response = svc.send_message_json("GET", 'test get', 'http://my.url/')
    mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/')
    assert response == {}
Example #41
0
    def mock_post_response():
        r = Response()
        r.status_code = 200
        r.encoding = "UTF-8"
        r._content = str(json.dumps({
            "access_token": "access_token",
            "token_type": "Bearer",
            "expires_in": 3599
        })).encode("UTF-8")

        return r
        def _request(method, url, *args, **kwargs):
            assert method == 'GET'
            assert url == 'http://localhost:123/collection.txt'
            r = Response()
            r.status_code = 200
            try:
                with open(self.tmpfile, 'rb') as f:
                    r._content = f.read()
            except IOError:
                r._content = b''

            r.headers['Content-Type'] = 'text/icalendar'
            r.encoding = 'ISO-8859-1'
            return r
Example #43
0
    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
Example #44
0
        def _request(method, url, *args, **kwargs):
            assert method == 'GET'
            assert url == 'http://localhost:123/collection.txt'
            assert 'vdirsyncer' in kwargs['headers']['User-Agent']
            r = Response()
            r.status_code = 200
            try:
                with open(self.tmpfile, 'rb') as f:
                    r._content = f.read()
            except IOError:
                r._content = b''

            r.headers['Content-Type'] = 'text/calendar'
            r.encoding = 'utf-8'
            return r
Example #45
0
    def test_repo_helper(self):
        helper = RepoHelper(self.repo1.working_dir)
        self.assertEqual(helper.active_branch_name(), 'master')
        self.assertEqual(
            helper.default_index_prefix(),
            os.path.basename(self.repo1.working_dir))

        helper = RepoHelper('http://localhost/repos/repo1.json')
        with patch.object(helper.rsm, 'mk_request') as mock:
            response = Response()
            response.encoding = 'utf-8'
            response._content = json.dumps({'branch': 'foo'})
            mock.return_value = response
            self.assertEqual(helper.active_branch_name(), 'foo')
            self.assertEqual(helper.default_index_prefix(), 'repo1')
Example #46
0
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.
    response.raw = data
    response.url = request.url
    response.request = request
    response.status_code = code.split()[0]

    # Make sure to seek the file-like raw object back to the start.
    response.raw.seek(0)

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
def build_response(request, data, code, encoding):
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.

    raw = StringIO()
    raw.write(data)
    raw.seek(0)

    response.raw = raw
    response.url = request.url
    response.request = request
    response.status_code = code

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
Example #48
0
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.
    response.raw = data
    response.url = request.url
    response.request = request
    response.status_code = int(code.split()[0])
    if hasattr(data, "content_len"):
        response.headers['Content-Length'] = str(data.content_len)

    # Make sure to seek the file-like raw object back to the start.
    response.raw.seek(0)

    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
def build_response(request, data, code, encoding):
    '''Builds a response object from the data returned by ftplib, using the
    specified encoding.'''
    response = Response()

    response.encoding = encoding

    # Fill in some useful fields.

    raw = StringIO()
    raw.write(data)
    raw.seek(0)

    response.raw = raw
    response.url = request.url
    response.request = request
    response.status_code = code


    # Run the response hook.
    response = dispatch_hook('response', request.hooks, response)
    return response
Example #50
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
Example #51
0
 def get_response(self, content=b'', status_code=200):
     response = Response()
     response.status_code = status_code
     response._content = content
     response.encoding = 'utf-8'
     return response
Example #52
0
    def send(self, request, stream=False, verify=None, cert=None, proxies=None,
            timeout=None):
        """issue request"""

        data = url_unquote(request.url[len('data:'):])

        if ',' not in data:
            raise InvalidURL('data URL missing comma')

        mime, content = data.split(',', 1)
        content = content.strip()

        base64 = False
        charset = None

        while ';' in mime:
            mime, encoding_spec = mime.rsplit(';', 1)
            encoding_spec = encoding_spec.strip()
            if encoding_spec == 'base64':
                base64 = True
            elif not encoding_spec.startswith('charset='):
                raise InvalidURL(
                    'unrecognized encoding parameter: %r' % encoding_spec
                )
            else:
                charset = encoding_spec[len('charset='):]

        try:
            if base64:
                content = a2b_base64(content)

            content_type = mime.strip()
            if charset:
                content_type += "; charset=" + charset

            response = Response()
            response.url = request.url
            response.headers['Date'] = formatdate(timeval=None, localtime=True)

            if request.method in ('GET', 'HEAD'):
                response.status_code = 200
                response.headers['Content-Length'] = len(content)
                response.headers['Last-Modified'] = formatdate()
                response.headers['Content-Type'] = content_type
                if charset:
                    response.encoding = charset
                response.raw = StringIO(str(content))
            else:
                response.status_code = 405
                response.headers['Status'] = '405 Method Not Allowed'
        except Exception:
            response.status_code = 500
            response.headers['Status'] = '500 Internal Server Error'
            response.raw = StringIO(format_exc())

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

        # hooks
        response = dispatch_hook('response', request.hooks, response)

        # streaming
        if not stream:
            response.content

        return response
    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 json_respnse(data):
    result = Response()
    result.encoding = "application/json"
    result.raw = BytesIO(json.dumps(data))
    result.status_code = 200
    return result