Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 1 | # 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. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 15 | import copy |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 16 | |
Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 17 | import httplib2 |
| 18 | |
| 19 | |
| 20 | class fake_httplib2(object): |
| 21 | |
Mauro S. M. Rodrigues | c3e573c | 2014-02-19 07:59:29 -0500 | [diff] [blame] | 22 | def __init__(self, return_type=None, *args, **kwargs): |
Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 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) |
Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 36 | 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. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class 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) |