File: test_user_agent.py

package info (click to toggle)
python-requests-toolbelt 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 876 kB
  • sloc: python: 3,653; makefile: 166; sh: 7
file content (108 lines) | stat: -rw-r--r-- 3,824 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
import unittest
import sys

try:
    from unittest.mock import patch
except ImportError:
    from mock import patch
import pytest

from requests_toolbelt.utils import user_agent as ua


class Object(object):
    """
    A simple mock object that can have attributes added to it.
    """
    pass


class TestUserAgentBuilder(unittest.TestCase):
    def test_only_user_agent_name(self):
        assert 'fake/1.0.0' == ua.UserAgentBuilder('fake', '1.0.0').build()

    def test_includes_extras(self):
        expected = 'fake/1.0.0 another-fake/2.0.1 yet-another-fake/17.1.0'
        actual = ua.UserAgentBuilder('fake', '1.0.0').include_extras([
            ('another-fake', '2.0.1'),
            ('yet-another-fake', '17.1.0'),
        ]).build()
        assert expected == actual

    @patch('platform.python_implementation', return_value='CPython')
    @patch('platform.python_version', return_value='2.7.13')
    def test_include_implementation(self, *_):
        expected = 'fake/1.0.0 CPython/2.7.13'
        actual = ua.UserAgentBuilder('fake', '1.0.0').include_implementation(
            ).build()
        assert expected == actual

    @patch('platform.system', return_value='Linux')
    @patch('platform.release', return_value='4.9.5')
    def test_include_system(self, *_):
        expected = 'fake/1.0.0 Linux/4.9.5'
        actual = ua.UserAgentBuilder('fake', '1.0.0').include_system(
            ).build()
        assert expected == actual


class TestUserAgent(unittest.TestCase):
    def test_user_agent_provides_package_name(self):
        assert "my-package" in ua.user_agent("my-package", "0.0.1")

    def test_user_agent_provides_package_version(self):
        assert "0.0.1" in ua.user_agent("my-package", "0.0.1")

    def test_user_agent_builds_extras_appropriately(self):
        assert "extra/1.0.0" in ua.user_agent(
            "my-package", "0.0.1", extras=[("extra", "1.0.0")]
        )

    def test_user_agent_checks_extras_for_tuples_of_incorrect_length(self):
        with pytest.raises(ValueError):
            ua.user_agent("my-package", "0.0.1", extras=[
                ("extra", "1.0.0", "oops")
            ])

        with pytest.raises(ValueError):
            ua.user_agent("my-package", "0.0.1", extras=[
                ("extra",)
            ])


class TestImplementationString(unittest.TestCase):
    @patch('platform.python_implementation')
    @patch('platform.python_version')
    def test_cpython_implementation(self, mock_version, mock_implementation):
        mock_implementation.return_value = 'CPython'
        mock_version.return_value = '2.7.5'
        assert 'CPython/2.7.5' == ua._implementation_string()

    @patch('platform.python_implementation')
    def test_pypy_implementation_final(self, mock_implementation):
        mock_implementation.return_value = 'PyPy'
        sys.pypy_version_info = Object()
        sys.pypy_version_info.major = 2
        sys.pypy_version_info.minor = 0
        sys.pypy_version_info.micro = 1
        sys.pypy_version_info.releaselevel = 'final'

        assert 'PyPy/2.0.1' == ua._implementation_string()

    @patch('platform.python_implementation')
    def test_pypy_implementation_non_final(self, mock_implementation):
        mock_implementation.return_value = 'PyPy'
        sys.pypy_version_info = Object()
        sys.pypy_version_info.major = 2
        sys.pypy_version_info.minor = 0
        sys.pypy_version_info.micro = 1
        sys.pypy_version_info.releaselevel = 'beta2'

        assert 'PyPy/2.0.1beta2' == ua._implementation_string()

    @patch('platform.python_implementation')
    def test_unknown_implementation(self, mock_implementation):
        mock_implementation.return_value = "Lukasa'sSuperPython"

        assert "Lukasa'sSuperPython/Unknown" == ua._implementation_string()