blob: 7d77484a80cb2688f18508b449ef1465ada9cfc3 [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 Treinish96e9e882014-06-09 18:37:19 -040016
Matthew Treinisha33037e2013-12-05 23:16:39 +000017import httplib2
18
19
20class fake_httplib2(object):
21
Mauro S. M. Rodriguesc3e573c2014-02-19 07:59:29 -050022 def __init__(self, return_type=None, *args, **kwargs):
Matthew Treinisha33037e2013-12-05 23:16:39 +000023 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)
Matthew Treinisha33037e2013-12-05 23:16:39 +000036 elif isinstance(self.return_type, int):
37 body = "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)
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040048
49
50class fake_httplib(object):
51 def __init__(self, headers, body=None,
52 version=1.0, status=200, reason="Ok"):
53 """
54 :param headers: dict representing HTTP response headers
55 :param body: file-like object
56 :param version: HTTP Version
57 :param status: Response status code
58 :param reason: Status code related message.
59 """
60 self.body = body
61 self.status = status
62 self.reason = reason
63 self.version = version
64 self.headers = headers
65
66 def getheaders(self):
67 return copy.deepcopy(self.headers).items()
68
69 def getheader(self, key, default):
70 return self.headers.get(key, default)
71
72 def read(self, amt):
73 return self.body.read(amt)