blob: eda202d436c2e1d5aa7183ebddebbae2c5485459 [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import copy
16
17import httplib2
18
19
20class fake_httplib2(object):
21
22 def __init__(self, return_type=None, *args, **kwargs):
23 self.return_type = return_type
24
25 def request(self, uri, method="GET", body=None, headers=None,
26 redirections=5, connection_type=None):
27 if not self.return_type:
28 fake_headers = httplib2.Response(headers)
29 return_obj = {
30 'uri': uri,
31 'method': method,
32 'body': body,
33 'headers': headers
34 }
35 return (fake_headers, return_obj)
36 elif isinstance(self.return_type, int):
37 body = body or "fake_body"
38 header_info = {
39 'content-type': 'text/plain',
40 'status': str(self.return_type),
41 'content-length': len(body)
42 }
43 resp_header = httplib2.Response(header_info)
44 return (resp_header, body)
45 else:
46 msg = "unsupported return type %s" % self.return_type
47 raise TypeError(msg)
48
49
50class fake_httplib(object):
51 def __init__(self, headers, body=None,
52 version=1.0, status=200, reason="Ok"):
53 """Fake httplib implementation
54
55 :param headers: dict representing HTTP response headers
56 :param body: file-like object
57 :param version: HTTP Version
58 :param status: Response status code
59 :param reason: Status code related message.
60 """
61 self.body = body
62 self.status = status
63 self.reason = reason
64 self.version = version
65 self.headers = headers
66
67 def getheaders(self):
68 return copy.deepcopy(self.headers).items()
69
70 def getheader(self, key, default):
71 return self.headers.get(key, default)
72
73 def read(self, amt):
74 return self.body.read(amt)