Example #1
0
def mock_interface(expected_call, status_code, response_data, reason=None):
    """Return a CTMSInterface with a mocked session and response"""
    call = expected_call.lower()
    assert call in set(("patch", "post", "put", "get"))
    session = Mock(spec_set=[call])
    caller = getattr(session, call)

    def set_request(path, **kwargs):
        url = f"https://ctms.example.com{path}"
        request = Request(expected_call, url, **kwargs)
        response.request = request.prepare()
        response.url = url
        return DEFAULT

    caller.side_effect = set_request

    response = Response()
    response.status_code = status_code
    if reason:
        response.reason = reason
    else:
        reasons = {200: "OK", 422: "Unprocessable Entity"}
        response.reason = reasons.get(status_code, "Unknown")
    response._content = json.dumps(response_data).encode("utf8")

    getattr(session, call).return_value = response

    return CTMSInterface(session)
Example #2
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 #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]
    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 #4
0
 def request(self, yang_class, parent, method, url, locals_dict, payload):
     response = Response()
     response.headers['Content-Type'] = 'application/json'
     if method == 'GET':
         url = self._remove_restconf_prefix(url)
         response = self._get_response_from_mock_storage(
             yang_class, parent, url)
     elif method == 'POST' and url.split(
             '/').pop() in yang_class.YANG_ACTIONS:
         method_name = self._make_python_name(url.split('/').pop())
         if 'Returns:' in getattr(yang_class, method_name).__doc__:
             output = getattr(yang_class, method_name).__doc__.replace(
                 '\n', '').replace('\t', '').replace(' ', '')
             start = output.find('Returns:') + len('Returns:')
             output = output[start:output.find('}', start) + 1]
             output = output.replace('string', '')
             response.status_code = 200
             response.reason = 'OK'
             response._content = json.dumps(
                 {'openhltest:output': json.loads(output)}, indent=4)
         else:
             response.status_code = 204
             response.reason = 'No Content'
     elif method == 'POST':  # POST config message
         url = self._remove_restconf_prefix(url)
         response.status_code = 201
         response.reason = 'Created'
         payload = json.loads(payload)
         key_value = locals_dict[self._make_python_name(
             yang_class.YANG_KEY)]
         if len(url) > 0:
             response.headers['location'] = 'openhltest:%s/%s=%s' % (
                 url, yang_class.YANG_NAME, key_value)
         else:
             response.headers['location'] = 'openhltest:%s=%s' % (
                 yang_class.YANG_NAME, key_value)
         self._add_to_mock_storage(yang_class, parent,
                                   response.headers['location'], payload)
     elif method == 'PATCH':
         url = self._remove_restconf_prefix(url)
         response.status_code = 204
         response.reason = 'No Content'
         self._update_mock_storage(yang_class, parent, url, payload)
     elif method == 'DELETE':
         url = self._remove_restconf_prefix(url)
         response.status_code = 204
         response.reason = 'No Content'
         self._delete_from_mock_storage(yang_class, parent, url)
     return response
Example #5
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
Example #6
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
        )
Example #7
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 #8
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
Example #9
0
 def make_response(self, status_code=200, reason='OK', data=None):
     data = data or {}
     response = Response()
     response.status_code = status_code
     response.reason = reason
     response._content = json.dumps(data).encode('utf-8')
     return response
def test_put_prediction_409_error(prediction_storage, prediction_data,
                                  tag_metadata):
    exception_response = Response()
    exception_response.status_code = 409
    exception_response.reason = "Conflict url"

    with patch.object(
            prediction_storage,
            "_get",
            new=MagicMock(return_value=exception_response)), patch.object(
                prediction_storage,
                "replace_cached_metadata_with_new",
                return_value=tag_metadata
            ) as replace_cached_mock, patch.object(
                prediction_storage,
                "_store_data_for_id",
                return_value=store_data_for_id_resp), patch.object(
                    prediction_storage,
                    "get_facility_by_tag_name",
                    return_value="1903"):
        res = prediction_storage.put_prediction(prediction_data)

    calls = [
        call(facility="1903",
             description="Gordo model-output - 1903.R1",
             tag_name="1903.R1|model|model-output"),
        call(facility="1903",
             description="Gordo model-output - 1903.R2",
             tag_name="1903.R2|model|model-output"),
        call(facility="1903",
             description="Gordo confidence - ",
             tag_name="1903.INDICATOR|model|confidence"),
    ]
    replace_cached_mock.assert_has_calls(calls)
    assert res
Example #11
0
 def make_response(self, status_code=200, reason='OK', data=None):
     data = data or {}
     response = Response()
     response.status_code = status_code
     response.reason = reason
     response._content = json.dumps(data).encode('utf-8')
     return response
Example #12
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 #13
0
 def test_reschedule_300(self, mask_frontend_request, post_req):
     response = Response()
     response.status_code = 302
     response.reason = 'whatever'
     post_req.side_effect = response
     with pytest.raises(FrontendClientException) as ex:
         self.fc.reschedule_all_running()
     assert 'Failed to reschedule builds' in str(ex)
Example #14
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
Example #15
0
 def ctms_error(self, status_code, detail, reason):
     """Return a CTMS error response"""
     response = Response()
     response.status_code = status_code
     response._content = json.dumps({"detail": detail})
     if reason:
         response.reason = reason
     error = HTTPError()
     error.response = response
     return error
    def test_trade_error_with_api_response(self):

        response = Response()
        response.reason = "Server Error"
        response.status_code = 500

        trade_error = TradeError("Some Error", None, response)

        self.assertEqual(str(trade_error),
                         "Trade Error (500) [Server Error]: Some Error")
Example #17
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
Example #18
0
def make_response(content: dict = None,
                  dumped_data: str = None,
                  status_code: int = 200,
                  reason: str = "OK") -> Response:
    response_obj = Response()
    response_obj.status_code = status_code
    response_obj._content = dumped_data if dumped_data else dump_any_dict(
        content).encode()
    response_obj.reason = reason
    return response_obj
def map_splash_response(data: dict) -> Response:
    response = Response()
    response.url = data['url']
    response.status_code = data['status']
    response.reason = data['statusText']
    response.headers = {
        header['name']: header['value']
        for header in data['headers']
    }

    return response
Example #20
0
 def make_response(url='', content='ok', status_code=200, history=(),
                   encoding='utf8', reason='', cookies=None):
     r = Response()
     r.url = url
     r._content = content
     r.status_code = status_code
     r.history = history
     r.encoding = encoding
     r.reason = reason
     r.cookies = cookiejar_from_dict(cookies or {})
     return r
Example #21
0
 def check_output_run_http(self, method, url, data, **kwargs):
     # pylint: disable=protected-access, unused-argument
     self.assertEqual(method, "post")
     with tmp_file_context() as file:
         file.write(json.dumps(data['payload']))
         self.check_output_run([url, data['type'], file.name])
     the_response = Response()
     the_response.reason = "ok"
     the_response.status_code = 200
     the_response._content = b'{ "result" : "ok" }'
     return the_response
Example #22
0
def _dummy_resp():
    """ Response with dummy data so that a dummy file will always be downloaded """

    dummy_resp = Response()
    dummy_resp.raw = BytesIO(b'This File could not be downloaded because '
                             b'the server returned an error response!')
    dummy_resp.encoding = 'utf-8'  # plain encoding
    dummy_resp.status_code = 200  # fake the status
    dummy_resp.is_dummy = True  # but leave a mark
    dummy_resp.reason = 'Failed to access'  # fail reason
    return dummy_resp
    def test_delete_operation_on_cloud_any_error_throws_exception(self):
        # Arrange
        response = Response()
        response.status_code = 0
        response.reason = "Bla bla error"
        error = CloudError(response)
        self.vm_service.delete_vm = Mock(side_effect=error)

        # Act
        self.assertRaises(Exception, self.delete_operation.delete, Mock(),
                          Mock(), "AzureTestGroup", "AzureTestVM")
 def request(method, url, **kwargs):
     response = Response()
     if method == "POST" and "/upload" in url:
         url = test.generate_docservice_url()
         response.status_code = 200
         response.encoding = "application/json"
         data = '{{"url":"{url}","hash":"md5:{md5}","format":"{format}","title":"{title}"}}'.format(
             url=url, md5="0" * 32, title="name.doc", format="application/msword"
         )
         response._content = '{{"data": {data},"get_url":"{url}"}}'.format(url=url, data=data)
         response.reason = "200 OK"
     return response
Example #25
0
def make_response(code, message):
    """
    Create a ``requests.Response`` with the given response code and message.

    :param int code: The HTTP response code to include in the fake response.
    :param unicode message: The HTTP response message to include in the fake
        response.  The message will be encoded using ASCII.
    """
    response = Response()
    response.status_code = code
    response.reason = message
    return response
Example #26
0
def client_err_response_type2():
    client_err_response_type2 = Response()
    client_err_response_type2.status_code = 400
    client_err_response_type2.reason = "mock client error response"
    client_err_response_type2.url = ""
    client_err_response_type2._content = b"request-client-error-002"
    client_err_response_type2.headers = {"X-Request-Id": "request-client-error-002"}
    client_err_response_type2.request = Request()
    client_err_response_type2.request.method = "GET"
    client_err_response_type2.request.url = ""

    yield client_err_response_type2
Example #27
0
 def check_output_run_http(self, method, url, data, **kwargs):
     # pylint: disable=protected-access, unused-argument
     self.assertEqual(method, "post")
     self.check_output_run([url, data['type']],
                           cwd='',
                           input=json.dumps(data['payload']),
                           universal_newlines=True)
     the_response = Response()
     the_response.reason = "ok"
     the_response.status_code = 200
     the_response._content = b'{ "result" : "ok" }'
     return the_response
Example #28
0
def make_response(code, message):
    """
    Create a ``requests.Response`` with the given response code and message.

    :param int code: The HTTP response code to include in the fake response.
    :param unicode message: The HTTP response message to include in the fake
        response.  The message will be encoded using ASCII.
    """
    response = Response()
    response.status_code = code
    response.reason = message
    return response
Example #29
0
 def _mock_response(url="", status=200, json_data=None, text=""):
     response = Response()
     response.status_code = status
     response.reason = 'Testing'
     response.url = url
     if text:
         type(response).text = mocker.PropertyMock(return_value=text)
         type(response).content = mocker.PropertyMock(return_value=text)
     if json_data:
         response.json = mocker.Mock(return_value=json_data)
         type(response).content = mocker.PropertyMock(
             return_value=json_data)
     return response
Example #30
0
    def mock_update(url, headers=None):
        response = Response()
        response._content_consumed = True
        response.request = mock.create_autospec(Request)
        response.request.method = 'GET'
        response.headers = headers or {}
        response.headers.update(
            {"content-type": "application/json; charset=utf8"})
        response.reason = "OK"

        if url == ASYNC_URL:
            response.request.url = url
            response.status_code = POLLING_STATUS
            response._content = ASYNC_BODY.encode('ascii')
            response.randomFieldFromPollAsyncOpHeader = None

        elif url == LOCATION_URL:
            response.request.url = url
            response.status_code = POLLING_STATUS
            response._content = LOCATION_BODY.encode('ascii')
            response.randomFieldFromPollLocationHeader = None

        elif url == ERROR:
            raise BadEndpointError("boom")

        elif url == RESOURCE_URL:
            response.request.url = url
            response.status_code = POLLING_STATUS
            response._content = RESOURCE_BODY.encode('ascii')

        else:
            raise Exception('URL does not match')

        request = CLIENT._request(
            response.request.method,
            response.request.url,
            None,  # params
            {},  # request has no headers
            None,  # Request has no body
            None,  # form_content
            None  # stream_content
        )

        return PipelineResponse(
            request,
            AsyncioRequestsTransportResponse(
                request,
                response,
            ),
            None  # context
        )
def mock_http_calls(status_code, content=None, reason=None):
    http_response = Response()
    http_response.status_code = status_code
    http_response.headers = {}
    if content is not None:
        if isinstance(content, (bytes, bytearray)):
            http_response._content = content
        else:
            http_response._content = content.encode("ascii")
    if reason is not None:
        http_response.reason = reason
    mocked_response = Mock()
    mocked_response.send.return_value = http_response
    return mocked_response
def build_response(data) -> Response:
    """Return a requests.Response object with the data set as the content."""
    response = Response()
    response.status_code = 200
    response.headers = {
        "Connection": "keep-alive",
        "Content-Encoding": "gzip",
        "Content-Type": "application/json; charset=utf-8",
    }
    response.encoding = "utf-8"
    response.raw = BytesIO(bytes(data, encoding="utf-8"))
    response.reason = "OK"
    response.url = "https://jsonplaceholder.typicode.com/todos"
    return response
    def test_delete_operation_on_cloud_any_error_throws_exception(self):
        # Arrange
        response = Response()
        response.status_code = 0
        response.reason = "Bla bla error"
        error = CloudError(response)
        self.vm_service.delete_vm = Mock(side_effect=error)

        # Act
        self.assertRaises(Exception,
                          self.delete_operation.delete,
                          Mock(),
                          Mock(),
                          "AzureTestGroup",
                          "AzureTestVM")
Example #34
0
    def test_post_to_frontend_err_400(self, post_req, mc_time):
        response = Response()
        response.status_code = 404
        response.reason = 'NOT FOUND'

        post_req.side_effect = [
            FrontendClientRetryError(),
            response,
        ]

        mc_time.time.return_value = 0
        with pytest.raises(FrontendClientException):
            assert self.fc._post_to_frontend_repeatedly(
                self.data, self.url_path) == response
        assert mc_time.sleep.called
Example #35
0
    def encode(self, onem2m_primitive):
        """
        Encodes OneM2M JSON primitive object to Rx specific HTTP message
        with JSON content type
        """

        # This is Rx encoder so we use Response
        msg = Response()

        params = onem2m_primitive.get_parameters()
        proto_params = onem2m_primitive.get_protocol_specific_parameters()

        # set result code and reason
        if http_result_code not in proto_params:
            raise IoTDataEncodeError("Result code not passed for HTTP response")

        result_code = proto_params[http_result_code]
        try:
            reason = status_codes._codes[result_code][0]
        except KeyError:
            raise IoTDataEncodeError("Invalid result code passed: {}", result_code)

        msg.status_code = result_code
        msg.reason = reason

        # Headers from protocol specific parameters
        if proto_params:
            for key, value in proto_params.items():
                encoded = http_headers.encode_default_ci(key, None)
                if None is not encoded:
                    msg.headers[encoded] = str(value)

        # onem2m parameters
        for key, value in params.items():
            encoded = http_headers.encode_default_ci(key, None)
            if None is not encoded:
                msg.headers[encoded] = str(value)

        # Body (content)
        content = onem2m_primitive.get_content()
        if content:
            msg._content = content

        return msg
Example #36
0
        def fake_send(self, request):
            g = parser.match(request.url).groups()

            response = Response()
            response.url = g[0]
            response.reason = g[1]
            response.status_code = int(g[2])
            if g[3] is not None:
                wait = float(g[3])
            else:
                wait = defaultSendTime - 0.001 # Epsilon, since sleep is defined as "will wait at *least* as long as..."

            sleep(wait)

            if response.status_code >= 600:
                # Special case for testing exception handling
                raise Exception('[%d] %s' % ( response.status_code, response.reason ))

            return response
    def test_delete_operation_on_cloud_error_not_found_no_exception(self):
        # Arrange
        response = Response()
        response.status_code = 0
        response.reason = "Not Found"
        error = CloudError(response)
        self.vm_service.delete_vm = Mock(side_effect=error)
        self.delete_operation.security_group_service.delete_security_rules = Mock()

        # Act
        self.delete_operation.delete(
                Mock(),
                Mock(),
                Mock(),
                "group_name",
                "vm_name",
                self.logger)

        # Verify
        self.logger.info.assert_called()
Example #38
0
    def test_negative_retry(self):
        retry_max = 100
        retry_wait = 0
        retry_range = 10

        client = Client(TEST_ENDPOINT, retry_max=retry_max, retry_wait=retry_wait, retry_range=retry_range)
        err_response = Response()
        err_response.status_code = 500
        err_response.reason = "INTERNAL SERVER ERROR"

        adapter = RepeatingTestAdapter(err_response)
        client._session.mount(TEST_ENDPOINT, adapter)

        mock_sleep = MockTimeSleep()

        with mock_sleep:
            self.assertRaises(APIError, client.measurements.list)

        for wait_time in mock_sleep.calls:
            self.assertGreaterEqual(wait_time, 0)
Example #39
0
    def test_error_wrapping(self):
        """
        Test that HTTP errors are wrapped into API Errors
        """
        client = Client(TEST_ENDPOINT)
        response = Response()
        response.status_code = 500
        response.reason = "INTERNAL SERVER ERROR"
        adapter = RepeatingTestAdapter(response)

        client._session.mount(TEST_ENDPOINT, adapter)

        # We want a bit more control than just assertRaises
        try:
            client.measurements.get()
        except APIError as e:
            self.assertIsNotNone(e.response)
            self.assertEqual(500, e.response.status_code)
            self.assertEqual("INTERNAL SERVER ERROR", e.response.reason)
        else:
            self.fail("No APIError was raised")
Example #40
0
    def test_retry(self):
        retry_max = 2
        retry_wait = 7

        client = Client(TEST_ENDPOINT, retry_max=retry_max, retry_wait=retry_wait)

        err_response = Response()
        err_response.status_code = 500
        err_response.reason = "INTERNAL SERVER ERROR"

        response_data = dict(BASE_RESPONSE)
        response_data["objects"] = [OBJ_1_PARTIAL]
        response_data["total_count"] = 1
        success_response = make_json_response(response_data)

        adapter = PredictableTestAdapter([err_response, err_response, success_response])
        client._session.mount(TEST_ENDPOINT, adapter)

        mock_sleep = MockTimeSleep()

        with mock_sleep:
            try:
                 response = client.measurements.list()
            except APIError as e:
                self.fail("API call wasn't retried: %s" % e)

        self.assertEqual([OBJ_1_PARTIAL], response)
        self.assertEqual([retry_wait, retry_wait], mock_sleep.calls)


        adapter = PredictableTestAdapter([err_response, err_response, err_response, success_response])
        client._session.mount(TEST_ENDPOINT, adapter)

        mock_sleep = MockTimeSleep()

        with mock_sleep:
            self.assertRaises(APIError, client.measurements.list)

        self.assertEqual([retry_wait, retry_wait], mock_sleep.calls)
Example #41
0
    def test_race_condition(self):
        self.client = Client(TEST_ENDPOINT)  # We'll never hit that anyway

        response_data = dict(BASE_RESPONSE)

        absent = make_json_response(response_data)

        exists = Response()
        exists.status_code = 400
        exists.reason = "BAD REQUEST"
        exists._content = six.b('{"configuration": {"__all__": ["Configuration with this I/O Mode, Block Size and I/O Depth already exists."]}}')

        response_data = dict(BASE_RESPONSE)
        response_data["objects"] = [OBJ_1_PARTIAL]
        response_data["total_count"] = 1
        success_response = make_json_response(response_data)

        self.adapter = PredictableTestAdapter([absent, exists, success_response, make_json_response(OBJ_RESPONSE)])
        self.client._session.mount(TEST_ENDPOINT, self.adapter)

        ret = self.client.configurations.get_or_create()
        self.assertDictEqual(OBJ_RESPONSE, ret)
        self.assertEqual(4, len(self.adapter.requests))
Example #42
0
    def test_random_retry(self):
        retry_max = 50  # We want a large sample
        retry_wait = 100
        retry_range = 5

        client = Client(TEST_ENDPOINT, retry_max=retry_max, retry_wait=retry_wait, retry_range=retry_range)
        err_response = Response()
        err_response.status_code = 500
        err_response.reason = "INTERNAL SERVER ERROR"

        adapter = RepeatingTestAdapter(err_response)
        client._session.mount(TEST_ENDPOINT, adapter)

        mock_sleep = MockTimeSleep()

        with mock_sleep:
            self.assertRaises(APIError, client.measurements.list)

        for wait_time in mock_sleep.calls:
            self.assertGreaterEqual(wait_time, retry_wait - retry_range)
            self.assertLessEqual(wait_time, retry_wait + retry_range)

        self.assertFalse(all([wait_time == retry_wait for wait_time in mock_sleep.calls]))
Example #43
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 #44
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
Example #45
0
 def raise_http_error():
     x = Response()
     x.status_code = 404
     x.reason = err
     raise HTTPError(err, response=x)
Example #46
0
 def connection_error():
     x = Response()
     x.status_code = 500
     x.reason = err
     raise ConnectionError(err, response=x)