Skip to content

Commit

Permalink
tests: ensure consistent behavior around url format (#1600)
Browse files Browse the repository at this point in the history
* tests: add tests that aim to ensure consistent behavior around url formats

* Fix naming

* make the tests work

* Clean up imports

* Improve based on rebase

* Clean up PR

* clean up PR
  • Loading branch information
WilliamBergamin authored Nov 27, 2024
1 parent 860bf9b commit 5464f89
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/slack_sdk/web/test_legacy_web_client_url_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from unittest import TestCase

from slack_sdk.web.legacy_client import LegacyWebClient
from tests.slack_sdk.web.mock_web_api_handler import MockHandler
from tests.mock_web_api_server import setup_mock_web_api_server, cleanup_mock_web_api_server, assert_received_request_count


class TestLegacyWebClientUrlFormat(TestCase):
def setUp(self):
setup_mock_web_api_server(self, MockHandler)
self.client = LegacyWebClient(token="xoxb-api_test", base_url="http://localhost:8888")
self.client_base_url_slash = LegacyWebClient(token="xoxb-api_test", base_url="http://localhost:8888/")

def tearDown(self):
cleanup_mock_web_api_server(self)

def test_base_url_without_slash_api_method_without_slash(self):
self.client.api_call("chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_without_slash_api_method_with_slash(self):
self.client.api_call("/chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_with_slash_api_method_without_slash(self):
self.client_base_url_slash.api_call("chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_with_slash_api_method_with_slash(self):
self.client_base_url_slash.api_call("/chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
self.client.api_call("/chat.postMessage/")
assert_received_request_count(self, "/chat.postMessage/", 1)
35 changes: 35 additions & 0 deletions tests/slack_sdk/web/test_web_client_url_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from unittest import TestCase

from slack_sdk.web import WebClient
from tests.slack_sdk.web.mock_web_api_handler import MockHandler
from tests.mock_web_api_server import setup_mock_web_api_server, cleanup_mock_web_api_server, assert_received_request_count


class TestWebClientUrlFormat(TestCase):
def setUp(self):
setup_mock_web_api_server(self, MockHandler)
self.client = WebClient(token="xoxb-api_test", base_url="http://localhost:8888")
self.client_base_url_slash = WebClient(token="xoxb-api_test", base_url="http://localhost:8888/")

def tearDown(self):
cleanup_mock_web_api_server(self)

def test_base_url_without_slash_api_method_without_slash(self):
self.client.api_call("chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_without_slash_api_method_with_slash(self):
self.client.api_call("/chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_with_slash_api_method_without_slash(self):
self.client_base_url_slash.api_call("chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_with_slash_api_method_with_slash(self):
self.client_base_url_slash.api_call("/chat.postMessage")
assert_received_request_count(self, "/chat.postMessage", 1)

def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
self.client.api_call("/chat.postMessage/")
assert_received_request_count(self, "/chat.postMessage/", 1)
45 changes: 45 additions & 0 deletions tests/slack_sdk_async/web/test_web_client_url_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest

from slack_sdk.web.async_client import AsyncWebClient
from tests.slack_sdk_async.helpers import async_test
from tests.slack_sdk.web.mock_web_api_handler import MockHandler
from tests.mock_web_api_server import (
setup_mock_web_api_server_async,
cleanup_mock_web_api_server_async,
assert_received_request_count_async,
)


class TestAsyncWebClientUrlFormat(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server_async(self, MockHandler)
self.client = AsyncWebClient(token="xoxb-api_test", base_url="http://localhost:8888")
self.client_base_url_slash = AsyncWebClient(token="xoxb-api_test", base_url="http://localhost:8888/")

def tearDown(self):
cleanup_mock_web_api_server_async(self)

@async_test
async def test_base_url_without_slash_api_method_without_slash(self):
await self.client.api_call("chat.postMessage")
await assert_received_request_count_async(self, "/chat.postMessage", 1)

@async_test
async def test_base_url_without_slash_api_method_with_slash(self):
await self.client.api_call("/chat.postMessage")
await assert_received_request_count_async(self, "/chat.postMessage", 1)

@async_test
async def test_base_url_with_slash_api_method_without_slash(self):
await self.client_base_url_slash.api_call("chat.postMessage")
await assert_received_request_count_async(self, "/chat.postMessage", 1)

@async_test
async def test_base_url_with_slash_api_method_with_slash(self):
await self.client_base_url_slash.api_call("/chat.postMessage")
await assert_received_request_count_async(self, "/chat.postMessage", 1)

@async_test
async def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
await self.client.api_call("/chat.postMessage/")
await assert_received_request_count_async(self, "/chat.postMessage/", 1)

0 comments on commit 5464f89

Please sign in to comment.