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_with_body(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-body' % (tests_util.get_host_ip(), remotehost_port) json_file_content = tests_util.create_random_json(5) # test body specified in input http_response = rest_client.post(uri, data=json.dumps(json_file_content)) assert http_response.status_code == 200 assert http_response.json() == json_file_content # test body from local file # 1. error raised when local file does not exist with pytest.raises(exception.RestClientError) as exc_info: rest_client.post(uri, local_file='missing_file') assert 'Invalid file path given' in str(exc_info.value) # 2. create file locally with tempfile.NamedTemporaryFile() as tmp_local_file: tmp_local_file.write(json.dumps(json_file_content).encode('utf-8')) tmp_local_file.seek(0) http_response = rest_client.post(uri, local_file=tmp_local_file.name) assert http_response.status_code == 200 assert http_response.json() == json_file_content # check file copied on remote host has been properly removed assert not gateway_session.exists(path=tmp_local_file.name) # test body from remote file remote_file = 'remote_file.json' # 1. error raised when remote file does not exist with pytest.raises(exception.RestClientError) as exc_info: rest_client.post(uri, remote_file=remote_file) assert 'Invalid remote file path given' in str(exc_info.value) # 2. create file remotely gateway_session.file(remote_path=remote_file, content=json.dumps(json_file_content)) http_response = rest_client.post(uri, remote_file=remote_file) assert http_response.status_code == 200 assert http_response.json() == json_file_content