summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/urls/test_gzip.py
blob: 8d850f2dbeeb8d67533a7801564aa5fc273346d6 (plain)
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
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
# (c) 2021 Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations

import gzip
import io
import sys
import http.client

from ansible.module_utils.urls import GzipDecodedReader, Request

import pytest


def compress(data):
    buf = io.BytesIO()
    try:
        f = gzip.GzipFile(fileobj=buf, mode='wb')
        f.write(data)
    finally:
        f.close()
    return buf.getvalue()


class Sock(io.BytesIO):
    def makefile(self, *args, **kwds):
        return self


@pytest.fixture
def urlopen_mock(mocker):
    return mocker.patch('ansible.module_utils.urls.urllib.request.urlopen')


JSON_DATA = b'{"foo": "bar", "baz": "qux", "sandwich": "ham", "tech_level": "pickle", "pop": "corn", "ansible": "awesome"}'


RESP = b'''HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Set-Cookie: foo
Set-Cookie: bar
Content-Length: 108

%s''' % JSON_DATA

GZIP_RESP = b'''HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Set-Cookie: foo
Set-Cookie: bar
Content-Encoding: gzip
Content-Length: 100

%s''' % compress(JSON_DATA)


def test_Request_open_gzip(urlopen_mock):
    h = http.client.HTTPResponse(
        Sock(GZIP_RESP),
        method='GET',
    )
    h.begin()

    urlopen_mock.return_value = h

    r = Request().open('GET', 'https://ansible.com/')
    assert isinstance(r.fp, GzipDecodedReader)
    assert r.read() == JSON_DATA


def test_Request_open_not_gzip(urlopen_mock):
    h = http.client.HTTPResponse(
        Sock(RESP),
        method='GET',
    )
    h.begin()

    urlopen_mock.return_value = h

    r = Request().open('GET', 'https://ansible.com/')
    assert not isinstance(r.fp, GzipDecodedReader)
    assert r.read() == JSON_DATA


def test_Request_open_decompress_false(urlopen_mock):
    h = http.client.HTTPResponse(
        Sock(RESP),
        method='GET',
    )
    h.begin()

    urlopen_mock.return_value = h

    r = Request().open('GET', 'https://ansible.com/', decompress=False)
    assert not isinstance(r.fp, GzipDecodedReader)
    assert r.read() == JSON_DATA


def test_GzipDecodedReader_no_gzip(monkeypatch, mocker):
    monkeypatch.delitem(sys.modules, 'gzip')
    monkeypatch.delitem(sys.modules, 'ansible.module_utils.urls')

    orig_import = __import__

    def _import(*args):
        if args[0] == 'gzip':
            raise ImportError
        return orig_import(*args)

    mocker.patch('builtins.__import__', _import)

    mod = __import__('ansible.module_utils.urls').module_utils.urls
    assert mod.HAS_GZIP is False
    pytest.raises(mod.MissingModuleError, mod.GzipDecodedReader, None)