Skip to content

Commit

Permalink
make core tests offline (#19986)
Browse files Browse the repository at this point in the history
* make core tests offline

* clean up

* use localhost instead of 127.0.0.1

* update

* update

* update
  • Loading branch information
xiangyan99 authored Aug 25, 2021
1 parent 56475b0 commit 0ab3cda
Show file tree
Hide file tree
Showing 49 changed files with 708 additions and 598 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ async def __aenter__(self):
return self

async def __aexit__(self, *exc_details) -> None:
await self._client.__aexit__(*exc_details)
await self._client.__aexit__(*exc_details)
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from six.moves.http_client import HTTPConnection
import time

try:
from unittest import mock
except ImportError:
import mock

from azure.core.pipeline.transport import HttpRequest, AsyncHttpResponse, AsyncHttpTransport, AioHttpTransport
from azure.core.pipeline.policies import HeadersPolicy
from azure.core.pipeline import AsyncPipeline
Expand Down Expand Up @@ -42,9 +34,9 @@ def body(self):


@pytest.mark.asyncio
async def test_basic_options_aiohttp():
async def test_basic_options_aiohttp(port):

request = HttpRequest("OPTIONS", "https://httpbin.org")
request = HttpRequest("OPTIONS", "http://localhost:{}/basic/string".format(port))
async with AsyncPipeline(AioHttpTransport(), policies=[]) as pipeline:
response = await pipeline.run(request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def emit(self, record):

policy = HttpLoggingPolicy(logger=logger)

universal_request = HttpRequest('GET', 'http://127.0.0.1/')
universal_request = HttpRequest('GET', 'http://localhost/')
http_response = HttpResponse(universal_request, None)
http_response.status_code = 202
request = PipelineRequest(universal_request, PipelineContext(None))
Expand All @@ -54,7 +54,7 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 6
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == 'Request headers:'
assert mock_handler.messages[3].message == 'No body was attached to the request'
Expand All @@ -75,13 +75,13 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 12
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == 'Request headers:'
assert mock_handler.messages[3].message == 'No body was attached to the request'
assert mock_handler.messages[4].message == 'Response status: 202'
assert mock_handler.messages[5].message == 'Response headers:'
assert mock_handler.messages[6].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[6].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[7].message == "Request method: 'GET'"
assert mock_handler.messages[8].message == 'Request headers:'
assert mock_handler.messages[9].message == 'No body was attached to the request'
Expand All @@ -102,15 +102,15 @@ def emit(self, record):
"Content-Type": "Caramel",
"HateToo": "Chocolat",
}
universal_request.url = "http://127.0.0.1/?country=france&city=aix"
universal_request.url = "http://localhost/?country=france&city=aix"

policy.on_request(request)
response = PipelineResponse(request, http_response, request.context)
policy.on_response(request, response)

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 10
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/?country=france&city=REDACTED'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/?country=france&city=REDACTED'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == "Request headers:"
# Dict not ordered in Python, exact logging order doesn't matter
Expand Down Expand Up @@ -156,7 +156,7 @@ def emit(self, record):
policy = HttpLoggingPolicy()
kwargs={'logger': logger}

universal_request = HttpRequest('GET', 'http://127.0.0.1/')
universal_request = HttpRequest('GET', 'http://localhost/')
http_response = HttpResponse(universal_request, None)
http_response.status_code = 202
request = PipelineRequest(universal_request, PipelineContext(None, **kwargs))
Expand All @@ -169,7 +169,7 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 6
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == 'Request headers:'
assert mock_handler.messages[3].message == 'No body was attached to the request'
Expand All @@ -192,13 +192,13 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 12
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == 'Request headers:'
assert mock_handler.messages[3].message == 'No body was attached to the request'
assert mock_handler.messages[4].message == 'Response status: 202'
assert mock_handler.messages[5].message == 'Response headers:'
assert mock_handler.messages[6].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[6].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[7].message == "Request method: 'GET'"
assert mock_handler.messages[8].message == 'Request headers:'
assert mock_handler.messages[9].message == 'No body was attached to the request'
Expand Down Expand Up @@ -226,7 +226,7 @@ def emit(self, record):

policy = HttpLoggingPolicy(logger=logger)

universal_request = HttpRequest('GET', 'http://127.0.0.1/')
universal_request = HttpRequest('GET', 'http://localhost/')
universal_request.body = "testbody"
http_response = HttpResponse(universal_request, None)
http_response.status_code = 202
Expand All @@ -238,7 +238,7 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 6
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == 'Request headers:'
assert mock_handler.messages[3].message == 'A body is sent with the request'
Expand Down Expand Up @@ -267,7 +267,7 @@ def emit(self, record):

policy = HttpLoggingPolicy(logger=logger)

universal_request = HttpRequest('GET', 'http://127.0.0.1/')
universal_request = HttpRequest('GET', 'http://localhost/')
mock = Mock()
mock.__class__ = types.AsyncGeneratorType
universal_request.body = mock
Expand All @@ -281,7 +281,7 @@ def emit(self, record):

assert all(m.levelname == 'INFO' for m in mock_handler.messages)
assert len(mock_handler.messages) == 6
assert mock_handler.messages[0].message == "Request URL: 'http://127.0.0.1/'"
assert mock_handler.messages[0].message == "Request URL: 'http://localhost/'"
assert mock_handler.messages[1].message == "Request method: 'GET'"
assert mock_handler.messages[2].message == 'Request headers:'
assert mock_handler.messages[3].message == 'File upload'
Expand Down
21 changes: 10 additions & 11 deletions sdk/core/azure-core/tests/async_tests/test_pipeline_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ def on_exception(self, requests, **kwargs):
with pytest.raises(NotImplementedError):
await pipeline.run(req)


@pytest.mark.asyncio
async def test_basic_aiohttp():
async def test_basic_aiohttp(port):

request = HttpRequest("GET", "https://bing.com")
request = HttpRequest("GET", "http://localhost:{}/basic/string".format(port))
policies = [
UserAgentPolicy("myusergant"),
AsyncRedirectPolicy()
Expand All @@ -105,10 +104,10 @@ async def test_basic_aiohttp():
assert isinstance(response.http_response.status_code, int)

@pytest.mark.asyncio
async def test_basic_aiohttp_separate_session():
async def test_basic_aiohttp_separate_session(port):

session = aiohttp.ClientSession()
request = HttpRequest("GET", "https://bing.com")
request = HttpRequest("GET", "http://localhost:{}/basic/string".format(port))
policies = [
UserAgentPolicy("myusergant"),
AsyncRedirectPolicy()
Expand All @@ -124,9 +123,9 @@ async def test_basic_aiohttp_separate_session():
await transport.session.close()

@pytest.mark.asyncio
async def test_basic_async_requests():
async def test_basic_async_requests(port):

request = HttpRequest("GET", "https://bing.com")
request = HttpRequest("GET", "http://localhost:{}/basic/string".format(port))
policies = [
UserAgentPolicy("myusergant"),
AsyncRedirectPolicy()
Expand Down Expand Up @@ -186,9 +185,9 @@ def test_pass_in_http_logging_policy():
assert http_logging_policy.allowed_header_names == HttpLoggingPolicy.DEFAULT_HEADERS_WHITELIST.union({"x-ms-added-header"})

@pytest.mark.asyncio
async def test_conf_async_requests():
async def test_conf_async_requests(port):

request = HttpRequest("GET", "https://bing.com/")
request = HttpRequest("GET", "http://localhost:{}/basic/string".format(port))
policies = [
UserAgentPolicy("myusergant"),
AsyncRedirectPolicy()
Expand All @@ -198,10 +197,10 @@ async def test_conf_async_requests():

assert isinstance(response.http_response.status_code, int)

def test_conf_async_trio_requests():
def test_conf_async_trio_requests(port):

async def do():
request = HttpRequest("GET", "https://bing.com/")
request = HttpRequest("GET", "http://localhost:{}/basic/string".format(port))
policies = [
UserAgentPolicy("myusergant"),
AsyncRedirectPolicy()
Expand Down
11 changes: 5 additions & 6 deletions sdk/core/azure-core/tests/async_tests/test_request_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@pytest.mark.asyncio
async def test_async_gen_data():
async def test_async_gen_data(port):
class AsyncGen:
def __init__(self):
self._range = iter([b"azerty"])
Expand All @@ -26,14 +26,13 @@ async def __anext__(self):
raise StopAsyncIteration

async with AsyncioRequestsTransport() as transport:
req = HttpRequest('GET', 'http://httpbin.org/anything', data=AsyncGen())
req = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), data=AsyncGen())
response = await transport.send(req)
assert json.loads(response.text())['data'] == "azerty"

@pytest.mark.asyncio
async def test_send_data():
async def test_send_data(port):
async with AsyncioRequestsTransport() as transport:
req = HttpRequest('PUT', 'http://httpbin.org/anything', data=b"azerty")
req = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), data=b"azerty")
response = await transport.send(req)

assert json.loads(response.text())['data'] == "azerty"
assert json.loads(response.text())['data'] == "azerty"
8 changes: 4 additions & 4 deletions sdk/core/azure-core/tests/async_tests/test_request_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@pytest.mark.trio
async def test_async_gen_data():
async def test_async_gen_data(port):
class AsyncGen:
def __init__(self):
self._range = iter([b"azerty"])
Expand All @@ -26,14 +26,14 @@ async def __anext__(self):
raise StopAsyncIteration

async with TrioRequestsTransport() as transport:
req = HttpRequest('GET', 'http://httpbin.org/anything', data=AsyncGen())
req = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), data=AsyncGen())
response = await transport.send(req)
assert json.loads(response.text())['data'] == "azerty"

@pytest.mark.trio
async def test_send_data():
async def test_send_data(port):
async with TrioRequestsTransport() as transport:
req = HttpRequest('PUT', 'http://httpbin.org/anything', data=b"azerty")
req = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), data=b"azerty")
response = await transport.send(req)

assert json.loads(response.text())['data'] == "azerty"
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import json

from azure.core.pipeline.transport import AsyncioRequestsTransport
from azure.core.rest import HttpRequest
from rest_client_async import AsyncTestRestClient
Expand All @@ -29,15 +27,15 @@ async def __anext__(self):

async with AsyncioRequestsTransport() as transport:
client = AsyncTestRestClient(port, transport=transport)
request = HttpRequest('GET', 'http://httpbin.org/anything', content=AsyncGen())
request = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), content=AsyncGen())
response = await client.send_request(request)
assert response.json()['data'] == "azerty"

@pytest.mark.asyncio
async def test_send_data(port):
async with AsyncioRequestsTransport() as transport:
client = AsyncTestRestClient(port, transport=transport)
request = HttpRequest('PUT', 'http://httpbin.org/anything', content=b"azerty")
request = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), content=b"azerty")
response = await client.send_request(request)

assert response.json()['data'] == "azerty"
assert response.json()['data'] == "azerty"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from azure.core.exceptions import HttpResponseError, ResponseNotReadError
from azure.core.exceptions import ResponseNotReadError
import pytest
from azure.core.rest import HttpRequest
from rest_client_async import AsyncTestRestClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ async def test_response_headers_case_insensitive(client):
response.headers["cAMeLCaSE-hEadER"] ==
"camelCase"
)
return response
return response
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# Thank you httpx for your wonderful tests!
import pytest
from azure.core.rest import HttpRequest
from typing import AsyncGenerator
import collections.abc

@pytest.fixture
Expand Down Expand Up @@ -87,4 +86,4 @@ async def content():
request = HttpRequest("POST", "http://example.org", content=content())
await assert_aiterator_body(request, b"test 123")
# in this case, request._data is what we end up passing to the requests transport
assert isinstance(request._data, collections.abc.AsyncIterable)
assert isinstance(request._data, collections.abc.AsyncIterable)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
# license information.
# -------------------------------------------------------------------------
from azure.core.exceptions import HttpResponseError, ServiceRequestError
import functools
import os
import json
import pytest
from azure.core.rest import HttpRequest
from azure.core.exceptions import StreamClosedError, StreamConsumedError, ResponseNotReadError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ async def __anext__(self):

async with TrioRequestsTransport() as transport:
client = AsyncTestRestClient(port, transport=transport)
request = HttpRequest('GET', 'http://httpbin.org/anything', content=AsyncGen())
request = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), content=AsyncGen())
response = await client.send_request(request)
assert response.json()['data'] == "azerty"

@pytest.mark.trio
async def test_send_data(port):
async with TrioRequestsTransport() as transport:
request = HttpRequest('PUT', 'http://httpbin.org/anything', content=b"azerty")
request = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), content=b"azerty")
client = AsyncTestRestClient(port, transport=transport)
response = await client.send_request(request)

assert response.json()['data'] == "azerty"
assert response.json()['data'] == "azerty"
Loading

0 comments on commit 0ab3cda

Please sign in to comment.