blob: 397c856459229d0b118d8ba190fad8922879287f [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
Matthew Treinish9e26ca82016-02-23 11:43:20 -050017
18class fake_httplib2(object):
19
20 def __init__(self, return_type=None, *args, **kwargs):
21 self.return_type = return_type
22
23 def request(self, uri, method="GET", body=None, headers=None,
24 redirections=5, connection_type=None):
25 if not self.return_type:
Jordan Pittier00f25962016-03-18 17:10:07 +010026 fake_headers = fake_http_response(headers)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050027 return_obj = {
28 'uri': uri,
29 'method': method,
30 'body': body,
31 'headers': headers
32 }
33 return (fake_headers, return_obj)
34 elif isinstance(self.return_type, int):
35 body = body or "fake_body"
36 header_info = {
37 'content-type': 'text/plain',
Matthew Treinish9e26ca82016-02-23 11:43:20 -050038 'content-length': len(body)
39 }
Jordan Pittier00f25962016-03-18 17:10:07 +010040 resp_header = fake_http_response(header_info,
41 status=self.return_type)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050042 return (resp_header, body)
43 else:
44 msg = "unsupported return type %s" % self.return_type
45 raise TypeError(msg)
46
47
Jordan Pittier00f25962016-03-18 17:10:07 +010048class fake_http_response(dict):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050049 def __init__(self, headers, body=None,
50 version=1.0, status=200, reason="Ok"):
Jordan Pittier00f25962016-03-18 17:10:07 +010051 """Initialization of fake HTTP Response
Matthew Treinish9e26ca82016-02-23 11:43:20 -050052
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
Jordan Pittier00f25962016-03-18 17:10:07 +010061 self['status'] = str(self.status)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050062 self.reason = reason
63 self.version = version
64 self.headers = headers
65
Jordan Pittier00f25962016-03-18 17:10:07 +010066 if headers:
67 for key, value in headers.items():
68 self[key.lower()] = value
69
Matthew Treinish9e26ca82016-02-23 11:43:20 -050070 def getheaders(self):
71 return copy.deepcopy(self.headers).items()
72
73 def getheader(self, key, default):
74 return self.headers.get(key, default)
75
76 def read(self, amt):
77 return self.body.read(amt)