Skip to content

Commit

Permalink
Updates tests
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kolevska <elena@kolevska.com>
  • Loading branch information
elena-kolevska committed Oct 18, 2023
1 parent bf7af70 commit 4b85bef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 44 deletions.
2 changes: 1 addition & 1 deletion dapr/clients/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_api_url(self) -> str:
return '{}/{}'.format(settings.DAPR_HTTP_ENDPOINT, settings.DAPR_API_VERSION)

return 'http://{}:{}/{}'.format(settings.DAPR_RUNTIME_HOST,
settings.DAPR_HTTP_PORT, settings.DAPR_API_VERSION)
settings.DAPR_HTTP_PORT, settings.DAPR_API_VERSION)

async def send_bytes(
self, method: str, url: str,
Expand Down
5 changes: 3 additions & 2 deletions tests/clients/test_http_service_invocation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ def test_get_api_url_default(self):
settings.DAPR_API_VERSION),
client.invocation_client._client.get_api_url())

def test_get_api_url_endpoint_as_argument(self):
@patch.object(settings, "DAPR_HTTP_ENDPOINT", "https://domain1.com:5000")
def test_dont_get_api_url_endpoint_as_argument(self):
client = DaprClient("http://localhost:5000")
self.assertEqual('http://localhost:5000/{}'.format(settings.DAPR_API_VERSION),
self.assertEqual('https://domain1.com:5000/{}'.format(settings.DAPR_API_VERSION),
client.invocation_client._client.get_api_url())

@patch.object(settings, "DAPR_HTTP_ENDPOINT", "https://domain1.com:5000")
Expand Down
51 changes: 10 additions & 41 deletions tests/clients/test_secure_http_service_invocation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
limitations under the License.
"""
import ssl
from unittest.mock import patch

from dapr.clients.http.client import DaprHttpClient
from .certs import CERTIFICATE_CHAIN_PATH
from .fake_http_server import FakeHttpServer
from dapr.conf import settings
from dapr.clients import DaprClient
from dapr.proto import common_v1
from asyncio import TimeoutError


from opencensus.trace.tracer import Tracer # type: ignore
from opencensus.trace import print_exporter, samplers

from .test_http_service_invocation_client import DaprInvocationHttpClientTests

Expand All @@ -45,7 +40,8 @@ def setUp(self):
self.server.start()
settings.DAPR_HTTP_PORT = self.server_port
settings.DAPR_API_METHOD_INVOCATION_PROTOCOL = 'http'
self.client = DaprClient("https://localhost:{}".format(self.server_port))
settings.DAPR_HTTP_ENDPOINT = "https://localhost:{}".format(self.server_port)
self.client = DaprClient()
self.app_id = 'fakeapp'
self.method_name = 'fakemethod'
self.invoke_url = f'/v1.0/invoke/{self.app_id}/method/{self.method_name}'
Expand All @@ -55,37 +51,10 @@ def tearDown(self):
settings.DAPR_API_TOKEN = None
settings.DAPR_API_METHOD_INVOCATION_PROTOCOL = 'http'

def test_global_timeout_setting_is_honored(self):
previous_timeout = settings.DAPR_HTTP_TIMEOUT_SECONDS
settings.DAPR_HTTP_TIMEOUT_SECONDS = 1
new_client = DaprClient("https://localhost:{}".format(self.server_port))
self.server.set_server_delay(1.5)
with self.assertRaises(TimeoutError):
new_client.invoke_method(self.app_id, self.method_name, "")

settings.DAPR_HTTP_TIMEOUT_SECONDS = previous_timeout

def test_invoke_method_with_tracer(self):
tracer = Tracer(sampler=samplers.AlwaysOnSampler(), exporter=print_exporter.PrintExporter())

self.client = DaprClient("https://localhost:{}".format(self.server_port),
headers_callback=lambda: tracer.propagator.to_headers(
tracer.span_context))
self.server.set_response(b"FOO")

with tracer.span(name="test"):
req = common_v1.StateItem(key='test')
resp = self.client.invoke_method(self.app_id, self.method_name, http_verb='PUT',
data=req, )

request_headers = self.server.get_request_headers()

self.assertIn('Traceparent', request_headers)
self.assertEqual(b'FOO', resp.data)

def test_timeout_exception_thrown_when_timeout_reached(self):
new_client = DaprClient("https://localhost:{}".format(self.server_port),
http_timeout_seconds=1)
self.server.set_server_delay(1.5)
with self.assertRaises(TimeoutError):
new_client.invoke_method(self.app_id, self.method_name, "")
@patch.object(settings, "DAPR_HTTP_ENDPOINT", None)
def test_get_api_url_default(self):
client = DaprClient()
self.assertEqual(
'http://{}:{}/{}'.format(settings.DAPR_RUNTIME_HOST, settings.DAPR_HTTP_PORT,
settings.DAPR_API_VERSION),
client.invocation_client._client.get_api_url())

0 comments on commit 4b85bef

Please sign in to comment.