def test_resource_post_json(mock_mesos_http_request): mock_mesos_http_request.return_value = mock.Mock( status_code=200, text=ujson.dumps({'hello': 'world'}), ) mock_auth = mock.Mock() resource = http.Resource('http://some.domain/some/prefix', default_timeout=100, default_auth=mock_auth, default_max_attempts=1, default_use_gzip_encoding=True) ret = resource.post_json(payload={'somekey': 'someval'}) assert mock_mesos_http_request.mock_calls == [ mock.call(json={'somekey': 'someval'}, method='POST', url='http://some.domain/some/prefix', auth=mock_auth, headers={ 'Accept-Encoding': 'gzip', 'Accept': 'application/json' }, params=None, timeout=100) ] assert ret == {'hello': 'world'}
def test_resource_request_json(mock_ujson_loads, mock_mesos_http_request, default_tos, override_tos, json_payload, obj_decoder, request_exception, resp_status, json_exception, expected_exception, expected_additional_kwargs): call_kwargs = dict(method=http.METHOD_POST, json={'some': 'payload'}, timeout=None, url='http://some.domain/some/prefix', auth=None, headers={ 'Accept': 'application/json', 'Accept-Encoding': 'gzip' }, params=None) mock_calls = [] for kwargs in expected_additional_kwargs: call_kwargs.update(kwargs) mock_calls.append(mock.call(**call_kwargs)) def json_side_effect(_): if json_exception is None: return {'some': 'return_value'} else: raise json_exception def request_side_effect(*_, **__): if request_exception is None: return mock.Mock(status_code=resp_status) else: raise request_exception mock_mesos_http_request.side_effect = request_side_effect mock_ujson_loads.side_effect = json_side_effect resource = http.Resource('http://some.domain/some/prefix', default_timeout=default_tos, default_auth=None, default_max_attempts=1, default_use_gzip_encoding=True) if expected_exception is None: ret = resource.request_json(http.METHOD_POST, timeout=override_tos, payload=json_payload, decoder=obj_decoder) expected_ret = {'some': 'return_value'} if obj_decoder is None: assert ret == expected_ret else: assert ret == SomeModel(**expected_ret) else: with pytest.raises(expected_exception): resource.request_json(http.METHOD_POST, timeout=override_tos, payload=json_payload) assert mock_mesos_http_request.mock_calls == mock_calls
def test_resource(): # test initialization resource = http.Resource('http://some.domain/some/prefix') assert resource.url.geturl() == 'http://some.domain/some/prefix' # test subresource subresource = resource.subresource('some/subpath') assert subresource.url.geturl() == \ 'http://some.domain/some/prefix/some/subpath'
def test_resource_request(mock_mesos_http_request, default_tos, override_tos, expected_request_calls): # test request mock_response = mock.Mock(status_code=200) mock_mesos_http_request.return_value = mock_response resource = http.Resource( 'http://some.domain/some/prefix', default_timeout=default_tos, default_headers={'Content-Type': 'applicatioin/json'}) ret = resource.request( http.METHOD_GET, additional_headers={'some-header-key': 'some-header-val'}, timeout=override_tos, auth=None, use_gzip_encoding=False, max_attempts=1, params=None, somekey='someval') assert ret == mock_response assert mock_mesos_http_request.mock_calls == expected_request_calls