Skip to content

Commit 9098e7e

Browse files
committed
Merge branch 'release/1.5.0'
2 parents d3f21e7 + 6e2e7bc commit 9098e7e

19 files changed

Lines changed: 155 additions & 69 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.pyc
22
.vagrant
33
*.egg-info
4+
.eggs
45
build
56
.tox
67
flycheck-*

.pylintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[MESSAGES CONTROL]
2+
disable= R,attribute-defined-outside-init,bad-continuation,bad-option-value,bare-except,invalid-name,locally-disabled,missing-docstring,redefined-builtin,ungrouped-imports,wrong-import-order,wrong-import-position,unnecessary-pass,global-statement
3+
4+
[REPORTS]
5+
reports=no
6+
7+
[FORMAT]
8+
max-line-length=150

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ deploy:
1919
tags: true
2020
python: '3.6'
2121
repo: vmalloc/Flask-Loopback
22+
python: "3.6"

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
default: test
22

33
test: env
4-
.env/bin/py.test tests
4+
.env/bin/pytest tests
5+
.env/bin/pylint --rcfile .pylintrc flask_loopback tests setup.py
56

67
env: .env/.up-to-date
78

89
.env/.up-to-date: setup.py Makefile
910
python -m virtualenv .env
10-
.env/bin/pip install -e .
11-
.env/bin/pip install pytest
11+
.env/bin/pip install -e ".[testing]"
1212
touch $@
13-

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Build Status] (https://secure.travis-ci.org/vmalloc/Flask-Loopback.png )   ![Version] (https://img.shields.io/pypi/v/Flask-Loopback.svg )
1+
![Build Status](https://secure.travis-ci.org/vmalloc/Flask-Loopback.png)   ![Version](https://img.shields.io/pypi/v/Flask-Loopback.svg)
22

33
Overview
44
========

flask_loopback/__version__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
__version__ = "1.4.7"
1+
import pkg_resources
2+
3+
__version__ = pkg_resources.get_distribution('Flask-Loopback').version

flask_loopback/_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=import-error,unused-import
12
from contextlib import contextmanager
23
import gzip
34
import sys

flask_loopback/flask_loopback.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from contextlib import contextmanager
33

44
import requests
5+
import six
56
from requests.cookies import MockRequest
7+
from urllib3 import HTTPResponse
68

79
from . import dispatch
810
from ._compat import httplib, iteritems, gzip_decompress
@@ -23,6 +25,29 @@ def __init__(self, request, code):
2325
self.response.request = request
2426

2527

28+
class _IOReader(six.BytesIO):
29+
"""A reader that makes a BytesIO look like a HTTPResponse.
30+
A HTTPResponse will return an empty string when you read from it after
31+
the socket has been closed. A BytesIO will raise a ValueError. For
32+
compatibility we want to do the same thing a HTTPResponse does.
33+
"""
34+
35+
def read(self, *args, **kwargs): # pylint: disable=arguments-differ
36+
if self.closed:
37+
return six.b('')
38+
39+
# not a new style object in python 2
40+
result = six.BytesIO.read(self, *args, **kwargs)
41+
42+
# when using resp.iter_content(None) it'll go through a different
43+
# request path in urllib3. This path checks whether the object is
44+
# marked closed instead of the return value. see gh124.
45+
if result == six.b(''):
46+
self.close()
47+
48+
return result
49+
50+
2651
class FlaskLoopback(object):
2752

2853
def __init__(self, flask_app):
@@ -72,12 +97,12 @@ def handle_request(self, session, url, request):
7297
with ExitStack() as stack:
7398
for handler in self._request_context_handlers:
7499
try:
75-
stack.enter_context(handler(request))
100+
stack.enter_context(handler(request)) # pylint: disable=no-member
76101
except CustomHTTPResponse as e:
77102
return e.response
78103

79104
self._test_client.cookie_jar.clear()
80-
for cookie in request._cookies:
105+
for cookie in request._cookies: # pylint: disable=protected-access
81106
self._test_client.cookie_jar.set_cookie(cookie)
82107
resp = self._test_client.open(path, **open_kwargs)
83108
returned = requests.Response()
@@ -89,8 +114,16 @@ def handle_request(self, session, url, request):
89114
resp_data = resp.get_data()
90115
if resp.headers.get('content-encoding') == 'gzip':
91116
resp_data = gzip_decompress(resp_data)
92-
returned._content = resp_data # pylint: disable=protected-access
117+
returned._content = resp_data # pylint: disable=protected-access
93118
returned.headers.update(resp.headers.items())
119+
returned.raw = HTTPResponse(
120+
status=returned.status_code,
121+
reason=returned.reason,
122+
headers=returned.headers,
123+
body=_IOReader(resp_data) or _IOReader(six.b('')),
124+
decode_content=False,
125+
preload_content=False,
126+
)
94127
self._extract_cookies(session, request, resp, returned)
95128
return returned
96129

@@ -122,8 +155,9 @@ def get_all(self, name, default=None):
122155

123156
_hostname = None
124157

158+
125159
def _get_hostname():
126-
global _hostname # pylint: disable=global-statement
160+
global _hostname # pylint: disable=global-statement
127161
if _hostname is None:
128162
_hostname = socket.getfqdn()
129163
return _hostname

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
requests
2+
Flask
3+
URLObject
4+
5+
unittest2; python_version<'2.7'
6+
contextlib2; python_version<'3.3'

setup.cfg

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[metadata]
2+
name = Flask-Loopback
3+
classifiers =
4+
Programming Language :: Python :: 2.7
5+
Programming Language :: Python :: 3.5
6+
Programming Language :: Python :: 3.6
7+
Programming Language :: Python :: 3.7
8+
summary = Library for faking HTTP requests using flask applications without actual network operations
9+
description-file =
10+
README.md
11+
description-content-type = text/markdown
12+
license = BSD3
13+
author = Rotem Yaari
14+
author_email = vmalloc@gmail.com
15+
url = https://github.com/vmalloc/flask-loopback
16+
17+
[extras]
18+
testing =
19+
pytest
20+
astroid~=1.5.3; python_version=='2.7'
21+
astroid>=2.0; python_version >= '3.4'
22+
pylint~=1.7.2; python_version=='2.7'
23+
pylint~=2.3.1; python_version >= '3.4'
24+
25+
[tool:pytest]
26+
testpaths = tests

0 commit comments

Comments
 (0)