blob: ce2b2c0359f57db291d49ae2b15c785a8092d812 [file] [log] [blame]
Matthew Treinisha33037e2013-12-05 23:16:39 +00001# 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
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040015import copy
Matthew Treinisha33037e2013-12-05 23:16:39 +000016import httplib2
17
18
19class fake_httplib2(object):
20
Mauro S. M. Rodriguesc3e573c2014-02-19 07:59:29 -050021 def __init__(self, return_type=None, *args, **kwargs):
Matthew Treinisha33037e2013-12-05 23:16:39 +000022 self.return_type = return_type
23
24 def request(self, uri, method="GET", body=None, headers=None,
25 redirections=5, connection_type=None):
26 if not self.return_type:
27 fake_headers = httplib2.Response(headers)
28 return_obj = {
29 'uri': uri,
30 'method': method,
31 'body': body,
32 'headers': headers
33 }
34 return (fake_headers, return_obj)
Matthew Treinisha33037e2013-12-05 23:16:39 +000035 elif isinstance(self.return_type, int):
36 body = "fake_body"
37 header_info = {
38 'content-type': 'text/plain',
39 'status': str(self.return_type),
40 'content-length': len(body)
41 }
42 resp_header = httplib2.Response(header_info)
43 return (resp_header, body)
44 else:
45 msg = "unsupported return type %s" % self.return_type
46 raise TypeError(msg)
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040047
48
49class fake_httplib(object):
50 def __init__(self, headers, body=None,
51 version=1.0, status=200, reason="Ok"):
52 """
53 :param headers: dict representing HTTP response headers
54 :param body: file-like object
55 :param version: HTTP Version
56 :param status: Response status code
57 :param reason: Status code related message.
58 """
59 self.body = body
60 self.status = status
61 self.reason = reason
62 self.version = version
63 self.headers = headers
64
65 def getheaders(self):
66 return copy.deepcopy(self.headers).items()
67
68 def getheader(self, key, default):
69 return self.headers.get(key, default)
70
71 def read(self, amt):
72 return self.body.read(amt)