def test_download_response(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['response', 'download']) assert data.basename == 'download.png' assert data.read() == b'some file content'
def test_text_response(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['response', 'text']) expected = '123' assert data == expected
def test_query_params_with_multiple_values(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['location', 'query'], params={'example': [1, 2, 3]}) expected = { 'method': 'GET', 'query_params': {'example': ['1', '2', '3']} } assert data == expected
def test_query_params(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['location', 'query'], params={'example': 123}) expected = { 'method': 'GET', 'query_params': {'example': '123'} } assert data == expected
def test_urlencoded_encoding_in_body(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['encoding', 'urlencoded-body'], params={'example': {'foo': 123, 'bar': True}}) expected = { 'method': 'POST', 'content_type': 'application/x-www-form-urlencoded', 'query_params': {}, 'data': {'foo': '123', 'bar': 'true'}, 'files': {} } assert data == expected
def test_urlencoded_encoding_multiple_values(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['encoding', 'urlencoded'], params={'example': [1, 2, 3]}) expected = { 'method': 'POST', 'content_type': 'application/x-www-form-urlencoded', 'query_params': {}, 'data': {'example': ['1', '2', '3']}, 'files': {} } assert data == expected
def test_body_params(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['location', 'body'], params={'example': 123}) expected = { 'method': 'POST', 'content_type': 'application/json', 'query_params': {}, 'data': 123, 'files': {} } assert data == expected
def test_api_client(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') assert schema.title == 'Example API' assert schema.url == 'https://api.example.com/' assert schema['simple_link'].description == 'example link' assert schema['location']['query'].fields[0].schema.description == 'example field' data = client.action(schema, ['simple_link']) expected = { 'method': 'GET', 'query_params': {} } assert data == expected
def test_raw_upload_explicit_content_type(self): from coreapi.utils import File client = CoreAPIClient() schema = client.get('http://api.example.com/') example = File('example.txt', '123', 'text/html') data = client.action(schema, ['encoding', 'raw_upload'], params={'example': example}) expected = { 'method': 'POST', 'files': {'file': {'name': 'example.txt', 'content': '123'}}, 'content_type': 'text/html' } assert data == expected
def test_multipart_encoding_no_file(self): # When no file is included, multipart encoding should still be used. client = CoreAPIClient() schema = client.get('http://api.example.com/') data = client.action(schema, ['encoding', 'multipart'], params={'example': 123}) expected = { 'method': 'POST', 'content_type': 'multipart/form-data', 'query_params': {}, 'data': {'example': '123'}, 'files': {} } assert data == expected
def test_multipart_encoding_in_body(self): from coreapi.utils import File client = CoreAPIClient() schema = client.get('http://api.example.com/') example = {'foo': File(name='example.txt', content='123'), 'bar': 'abc'} data = client.action(schema, ['encoding', 'multipart-body'], params={'example': example}) expected = { 'method': 'POST', 'content_type': 'multipart/form-data', 'query_params': {}, 'data': {'bar': 'abc'}, 'files': {'foo': {'name': 'example.txt', 'content': '123'}} } assert data == expected
def test_raw_upload(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') with tempfile.NamedTemporaryFile(delete=False) as temp: temp.write(b'example file content') temp.flush() temp.seek(0) name = os.path.basename(temp.name) data = client.action(schema, ['encoding', 'raw_upload'], params={'example': temp}) expected = { 'method': 'POST', 'files': {'file': {'name': name, 'content': 'example file content'}}, 'content_type': 'application/octet-stream' } assert data == expected
def test_multipart_encoding_string_file_content(self): # Test for `coreapi.utils.File` support. from coreapi.utils import File client = CoreAPIClient() schema = client.get('http://api.example.com/') example = File(name='example.txt', content='123') data = client.action(schema, ['encoding', 'multipart'], params={'example': example}) expected = { 'method': 'POST', 'content_type': 'multipart/form-data', 'query_params': {}, 'data': {}, 'files': {'example': {'name': 'example.txt', 'content': '123'}} } assert data == expected
def test_multipart_encoding(self): client = CoreAPIClient() schema = client.get('http://api.example.com/') with tempfile.NamedTemporaryFile() as temp: temp.write(b'example file content') temp.flush() temp.seek(0) name = os.path.basename(temp.name) data = client.action(schema, ['encoding', 'multipart'], params={'example': temp}) expected = { 'method': 'POST', 'content_type': 'multipart/form-data', 'query_params': {}, 'data': {}, 'files': {'example': {'name': name, 'content': 'example file content'}} } assert data == expected
def test_session_headers(self): client = CoreAPIClient() client.session.headers.update({'X-Custom-Header': 'foo'}) schema = client.get('http://api.example.com/') data = client.action(schema, ['headers']) assert data['headers']['X-CUSTOM-HEADER'] == 'foo'