Skip to content

Commit

Permalink
feat(transport): add fix address function
Browse files Browse the repository at this point in the history
Refs: #10
  • Loading branch information
piraz committed Mar 30, 2024
1 parent 0ad5f5a commit d708cc4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
8 changes: 8 additions & 0 deletions peasant/client/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@

import logging
import typing
from urllib.parse import urlparse

if typing.TYPE_CHECKING:
from peasant.client.protocol import Peasant

logger = logging.getLogger(__name__)


def fix_address(address):
parsed_address = urlparse(address)
if parsed_address.path.endswith("/"):
parsed_address = parsed_address._replace(path=parsed_address.path[:-1])
return parsed_address.geturl()


class Transport:

_peasant: Peasant
Expand Down
3 changes: 2 additions & 1 deletion tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
# limitations under the License.

import unittest
from tests import tornado_test
from tests import tornado_test, transport_test


def suite():
testLoader = unittest.TestLoader()
alltests = unittest.TestSuite()
alltests.addTests(testLoader.loadTestsFromModule(tornado_test))
alltests.addTests(testLoader.loadTestsFromModule(transport_test))
return alltests


Expand Down
35 changes: 35 additions & 0 deletions tests/transport_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2020-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from peasant.client.transport import fix_address
from unittest import TestCase


class TransportTestCase(TestCase):

def test_fix_address(self):
address = "http://localhost"
expected_address = "http://localhost"
fixed_address = fix_address(address)
self.assertEqual(expected_address, fixed_address)

address = "https://localhost/"
expected_address = "https://localhost"
fixed_address = fix_address(address)
self.assertEqual(expected_address, fixed_address)

address = "http://localhost/a/path/"
expected_address = "http://localhost/a/path"
fixed_address = fix_address(address)
self.assertEqual(expected_address, fixed_address)

0 comments on commit d708cc4

Please sign in to comment.