|
17 | 17 |
|
18 | 18 | import os
|
19 | 19 | import datetime
|
20 |
| - |
21 | 20 | from typing import Optional
|
| 21 | +import pytest |
| 22 | + |
22 | 23 | from ibm_cloud_sdk_core import string_to_datetime, datetime_to_string, get_authenticator_from_environment
|
23 | 24 | from ibm_cloud_sdk_core import string_to_date, date_to_string
|
24 | 25 | from ibm_cloud_sdk_core import convert_model, convert_list
|
| 26 | +from ibm_cloud_sdk_core import get_query_param |
25 | 27 | from ibm_cloud_sdk_core import read_external_sources
|
26 | 28 | from ibm_cloud_sdk_core.authenticators import BasicAuthenticator, IAMAuthenticator
|
27 | 29 |
|
@@ -122,6 +124,46 @@ def test_date_conversion():
|
122 | 124 | assert res == '2017-03-06'
|
123 | 125 | assert date_to_string(None) is None
|
124 | 126 |
|
| 127 | +def test_get_query_param(): |
| 128 | + # Relative URL |
| 129 | + next_url = '/api/v1/offerings?start=foo&limit=10' |
| 130 | + page_token = get_query_param(next_url, 'start') |
| 131 | + assert page_token == 'foo' |
| 132 | + # Absolute URL |
| 133 | + next_url = 'https://acme.com/api/v1/offerings?start=bar&limit=10' |
| 134 | + page_token = get_query_param(next_url, 'start') |
| 135 | + assert page_token == 'bar' |
| 136 | + # Missing param |
| 137 | + next_url = 'https://acme.com/api/v1/offerings?start=bar&limit=10' |
| 138 | + page_token = get_query_param(next_url, 'token') |
| 139 | + assert page_token is None |
| 140 | + # No URL |
| 141 | + page_token = get_query_param(None, 'start') |
| 142 | + assert page_token is None |
| 143 | + # Empty URL |
| 144 | + page_token = get_query_param('', 'start') |
| 145 | + assert page_token is None |
| 146 | + # No query string |
| 147 | + next_url = '/api/v1/offerings' |
| 148 | + page_token = get_query_param(next_url, 'start') |
| 149 | + assert page_token is None |
| 150 | + # Bad query string |
| 151 | + next_url = '/api/v1/offerings?start%XXfoo' |
| 152 | + with pytest.raises(ValueError): |
| 153 | + page_token = get_query_param(next_url, 'start') |
| 154 | + # Duplicate param |
| 155 | + next_url = '/api/v1/offerings?start=foo&start=bar&limit=10' |
| 156 | + page_token = get_query_param(next_url, 'start') |
| 157 | + assert page_token == 'foo' |
| 158 | + # Bad URL - since the behavior for this case varies based on the version of Python |
| 159 | + # we allow _either_ a ValueError or that the illegal chars are just ignored |
| 160 | + next_url = 'https://foo.bar\u2100/api/v1/offerings?start=foo' |
| 161 | + try: |
| 162 | + page_token = get_query_param(next_url, 'start') |
| 163 | + assert page_token == 'foo' |
| 164 | + except ValueError: |
| 165 | + # This is okay. |
| 166 | + pass |
125 | 167 |
|
126 | 168 | def test_convert_model():
|
127 | 169 | class MockModel:
|
|
0 commit comments