def test_methods(docker_env): gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway') gateway_session = SSHSession(host=gateway_ip, port=gateway_port, username='******', password='******').open() remotehost_ip, remotehost_port = docker_env.get_host_ip_port( 'remotehost', private_port=5000) rest_client = RestSshClient(gateway_session) uri = 'http://%s:%s/echo-method' % (tests_util.get_host_ip(), remotehost_port) # check proper http method is used for each function header_name = 'Request-Method' if sys.version_info[0] == 2: # HTTP header names in the response are all lowercase in Python 2.x. header_name = 'request-method' assert rest_client.get(uri).headers[header_name] == 'GET' assert rest_client.options(uri).headers[header_name] == 'OPTIONS' assert rest_client.post(uri).headers[header_name] == 'POST' assert rest_client.put(uri).headers[header_name] == 'PUT' assert rest_client.patch(uri).headers[header_name] == 'PATCH' assert rest_client.delete(uri).headers[header_name] == 'DELETE' assert rest_client.head(uri).headers[header_name] == 'HEAD'
def test_request(docker_env): gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway') gateway_session = SSHSession(host=gateway_ip, port=gateway_port, username='******', password='******').open() remotehost_ip, remotehost_port = docker_env.get_host_ip_port( 'remotehost', private_port=5000) rest_client = RestSshClient(gateway_session) # check not properly formatted uri raise exception with pytest.raises(exception.RestClientError) as exc_info: rest_client.get('invalid_uri') assert 'returned exit status' in str(exc_info.value) # test header only http_response = rest_client.request( 'GET', 'http://%s:%s/' % (tests_util.get_host_ip(), remotehost_port), document_info_only=True) assert http_response.status_code == 200 assert len(http_response.text) == 0 # test uri parameters only parameters = {'param1': 'value1', 'param2': 'value2'} http_response = rest_client.request( 'GET', 'http://%s:%s/echo-parameters' % (tests_util.get_host_ip(), remotehost_port), params=parameters) assert http_response.status_code == 200 # value is a list as each parameter can be specified multiple times with different values expected_body = {} for key, value in parameters.items(): expected_body[key] = [value] assert http_response.json() == expected_body # test headers are properly handled http_response = rest_client.request( 'GET', 'http://%s:%s/echo-headers' % (tests_util.get_host_ip(), remotehost_port), headers={'My-Header': 'My-Value'}) assert http_response.status_code == 200 assert 'My-Header' in http_response.json() assert http_response.json()['My-Header'] == 'My-Value'
def test_response_methods(docker_env): gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway') gateway_session = SSHSession(host=gateway_ip, port=gateway_port, username='******', password='******').open() remotehost_ip, remotehost_port = docker_env.get_host_ip_port( 'remotehost', private_port=5000) rest_client = RestSshClient(gateway_session) endpoint = 'http://%s:%s' % (tests_util.get_host_ip(), remotehost_port) # check_for_success with pytest.raises(exception.RestClientError): rest_client.get(endpoint + '/dummy-uri').check_for_success() # is_valid_json_body assert rest_client.get(endpoint + '/json').is_valid_json_body() is True assert rest_client.get(endpoint + '/').is_valid_json_body() is False # json with pytest.raises(exception.RestClientError) as exc_info: rest_client.get(endpoint + '/').json() assert 'http response body is not in a valid json format' in str( exc_info.value)