Repository: ucfopen/canvasapi

Issue:
Properly strip out trailing slash from BASE_URL
A user may include a trailing slash at the end of their `BASE_URL`. When we manually tack on `/api/v1` we are including a slash before `api`, which can end up forming strange URLs like this: `some.site//api/v1`.

While this works for most endpoints, our PaginatedList implementation chokes when trying to parse the `next` link header because Canvas returns back a URL with multiple slashes.

Discussion:

Patch:
diff --git a/canvasapi/util.py b/canvasapi/util.py
index 86d6ee4..88741ab 100644
--- a/canvasapi/util.py
+++ b/canvasapi/util.py
@@ -126,12 +126,13 @@ def obj_or_id(parameter, param_name, object_types):
 
 def get_institution_url(base_url):
     """
-    Trim '/api/v1' from a given root URL.
+    Clean up a given base URL.
 
     :param base_url: The base URL of the API.
     :type base_url: str
     :rtype: str
     """
+    base_url = base_url.rstrip('/')
     index = base_url.find('/api/v1')
 
     if index != -1:


Test patch:
diff --git a/tests/test_util.py b/tests/test_util.py
index 472b4f1..15fe7ad 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -413,5 +413,26 @@ class TestUtil(unittest.TestCase):
 
     # get_institution_url()
     def test_get_institution_url(self, m):
-        base_url = 'https://my.canvas.edu/api/v1'
-        self.assertEqual(get_institution_url(base_url), 'https://my.canvas.edu')
+        correct_url = 'https://my.canvas.edu'
+
+        self.assertEqual(
+            get_institution_url('https://my.canvas.edu/'), correct_url
+        )
+        self.assertEqual(
+            get_institution_url('https://my.canvas.edu/api/v1'), correct_url
+        )
+        self.assertEqual(
+            get_institution_url('https://my.canvas.edu/api/v1/'), correct_url
+        )
+        self.assertEqual(
+            get_institution_url('https://my.canvas.edu/test/2/'),
+            correct_url + '/test/2'
+        )
+        self.assertEqual(
+            get_institution_url('https://my.canvas.edu/test/2/api/v1'),
+            correct_url + '/test/2'
+        )
+        self.assertEqual(
+            get_institution_url('https://my.canvas.edu/test/2/api/v1/'),
+            correct_url + '/test/2'
+        )