Skip to content

Commit c5a37e7

Browse files
committed
test(api,cli): add tests for custom user agent
1 parent 4bb201b commit c5a37e7

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

gitlab/tests/test_config.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import mock
2222
import io
2323

24-
from gitlab import config
24+
from gitlab import config, USER_AGENT
2525
import pytest
2626

2727

28+
custom_user_agent = "my-package/1.0.0"
29+
2830
valid_config = u"""[global]
2931
default = one
3032
ssl_verify = true
@@ -51,6 +53,17 @@
5153
oauth_token = STUV
5254
"""
5355

56+
custom_user_agent_config = """[global]
57+
default = one
58+
user_agent = {}
59+
60+
[one]
61+
url = http://one.url
62+
private_token = ABCDEF
63+
""".format(
64+
custom_user_agent
65+
)
66+
5467
no_default_config = u"""[global]
5568
[there]
5669
url = http://there.url
@@ -178,3 +191,21 @@ def test_valid_data(m_open, path_exists):
178191
assert "STUV" == cp.oauth_token
179192
assert 2 == cp.timeout
180193
assert True == cp.ssl_verify
194+
195+
196+
@mock.patch("os.path.exists")
197+
@mock.patch("builtins.open")
198+
@pytest.mark.parametrize(
199+
"config_string,expected_agent",
200+
[
201+
(valid_config, USER_AGENT),
202+
(custom_user_agent_config, custom_user_agent),
203+
],
204+
)
205+
def test_config_user_agent(m_open, path_exists, config_string, expected_agent):
206+
fd = io.StringIO(config_string)
207+
fd.close = mock.Mock(return_value=None)
208+
m_open.return_value = fd
209+
210+
cp = config.GitlabConfigParser()
211+
assert cp.user_agent == expected_agent

gitlab/tests/test_gitlab.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
import pickle
2020

21+
import pytest
2122
from httmock import HTTMock, response, urlmatch, with_httmock # noqa
2223

23-
from gitlab import Gitlab, GitlabList
24+
from gitlab import Gitlab, GitlabList, USER_AGENT
2425
from gitlab.v4.objects import CurrentUser
2526

2627

@@ -139,3 +140,15 @@ class MyGitlab(Gitlab):
139140
config_path = default_config
140141
gl = MyGitlab.from_config("one", [config_path])
141142
assert isinstance(gl, MyGitlab)
143+
144+
145+
@pytest.mark.parametrize(
146+
"kwargs,expected_agent",
147+
[
148+
({}, USER_AGENT),
149+
({"user_agent": "my-package/1.0.0"}, "my-package/1.0.0"),
150+
],
151+
)
152+
def test_gitlab_user_agent(kwargs, expected_agent):
153+
gl = Gitlab("http://localhost", **kwargs)
154+
assert gl.headers["User-Agent"] == expected_agent

0 commit comments

Comments
 (0)