| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
|
| 14 |
import os |
| 15 |
|
| 16 |
import fixtures |
| 17 |
import requests |
| 18 |
import testtools |
| 19 |
|
| 20 |
|
| 21 |
class TestCase(testtools.TestCase): |
| 22 |
|
| 23 |
def setUp(self): |
| 24 |
super(TestCase, self).setUp() |
| 25 |
if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 26 |
os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
| 27 |
stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 28 |
self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
| 29 |
if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 30 |
os.environ.get('OS_STDERR_CAPTURE') == '1'): |
| 31 |
stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 32 |
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
| 33 |
|
| 34 |
|
| 35 |
class TestResponse(requests.Response): |
| 36 |
|
| 37 |
""" |
| 38 |
Class used to wrap requests.Response and provide some |
| 39 |
convenience to initialize with a dict |
| 40 |
""" |
| 41 |
|
| 42 |
def __init__(self, data): |
| 43 |
self._text = None |
| 44 |
super(TestResponse, self) |
| 45 |
if isinstance(data, dict): |
| 46 |
self.status_code = data.get('status_code', None) |
| 47 |
self.headers = data.get('headers', None) |
| 48 |
# Fake the text attribute to streamline Response creation |
| 49 |
self._text = data.get('text', None) |
| 50 |
else: |
| 51 |
self.status_code = data |
| 52 |
|
| 53 |
def __eq__(self, other): |
| 54 |
return self.__dict__ == other.__dict__ |
| 55 |
|
| 56 |
@property |
| 57 |
def text(self): |
| 58 |
return self._text |