File size: 2,872 Bytes
d439dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015-2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from http import client as httplib

from nose.tools import eq_


class MockResponse(object):
    def __init__(self, method, url, headers, status_code,
                 response_headers=None, content=None):
        self.method = method
        self.url = url
        self.request_headers = {
            key.lower(): value for key, value in headers.items()
        }
        self.status = status_code
        self.headers = {
            key.lower(): value for key, value in (
                response_headers or {}).items()
        }
        self.data = content
        if content is None:
            self.reason = httplib.responses[status_code]

    # noinspection PyUnusedLocal
    def read(self, *args, **kwargs):
        return self.data

    def mock_verify(self, method, url, headers):
        eq_(self.method, method)
        eq_(self.url, url)
        headers = {
            key.lower(): value for key, value in headers.items()
        }
        for header in self.request_headers:
            eq_(self.request_headers[header], headers[header])

    # noinspection PyUnusedLocal
    def stream(self, chunk_size=1, decode_unicode=False):
        if self.data is not None:
            return iter(bytearray(self.data, 'utf-8'))
        return iter([])

    # dummy release connection call.
    def release_conn(self):
        return

    def getheader(self, key, value=None):
        return self.headers.get(key, value) if self.headers else value

    def __getitem__(self, key):
        if key == "status":
            return self.status


class MockConnection(object):
    def __init__(self):
        self.requests = []

    def mock_add_request(self, request):
        self.requests.append(request)

    # noinspection PyUnusedLocal
    def request(self, method, url, headers, redirect=False):
        # only pop off matching requests
        return_request = self.requests[0]
        return_request.mock_verify(method, url, headers)
        return self.requests.pop(0)

    # noinspection PyRedeclaration,PyUnusedLocal,PyUnusedLocal

    def urlopen(self, method, url, headers={}, preload_content=False,
                body=None, redirect=False):
        return self.request(method, url, headers)