Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [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 | |
| 15 | import copy |
| 16 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 17 | |
| 18 | class 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 Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 26 | fake_headers = fake_http_response(headers) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 27 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 38 | 'content-length': len(body) |
| 39 | } |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 40 | resp_header = fake_http_response(header_info, |
| 41 | status=self.return_type) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 42 | return (resp_header, body) |
| 43 | else: |
| 44 | msg = "unsupported return type %s" % self.return_type |
| 45 | raise TypeError(msg) |
| 46 | |
| 47 | |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 48 | class fake_http_response(dict): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 49 | def __init__(self, headers, body=None, |
| 50 | version=1.0, status=200, reason="Ok"): |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 51 | """Initialization of fake HTTP Response |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 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 |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 61 | self['status'] = str(self.status) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 62 | self.reason = reason |
| 63 | self.version = version |
| 64 | self.headers = headers |
| 65 | |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 66 | if headers: |
| 67 | for key, value in headers.items(): |
| 68 | self[key.lower()] = value |
| 69 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 70 | 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) |