When creating a response we type check that the user has passed the
correct type to content and text, however we check this based on whether
the value is True and not whether it has been passed at all which means
that the empty string of the wrong type can incorrectly pass through
this check.
Fix this check to test the empty string.
Anyone relying on this passing will be caught with a more confusing
TypeError later and so this should be backwards compatible.
Change-Id: I826da9b7fd83bb88af50e4a96a5e6358ee35e4b2
Closes-Bug: #1647880
To handle requests for additional features on matching add a callback
function that can match anything on the request object. This is easier
than adding every combination of matcher option - though we should still
add things that are commonly used.
Change-Id: I300f74a1f2103545eca60087b2352a535add188d
Closes-Bug: #1657308
The double mocking solution we have now of mocking send and get_adapter
is a bit unusual. One of the problems is that if you have a nested mock
situation then send calling the last send means get_adapter is remocked
again and the outer adapter ends up being the last installed.
To avoid this always go to the real original send function to bypass any
outer installed mockers and make sure our get_adapter function is
triggered.
Change-Id: I47bff5e7da98e5238bd926e22845a7ca34f21940
Closes-Bug: #1642690
The old FakeHTTPResponse we're using in the tests is really basic and
lacks a lot of the information that is expected in a response from an
adapter. This is a hard problem that we've already solved in the
create_response function so use that to create a test response instead
of a custom solution here.
Change-Id: I2999c9c69b4e9ad895114fab8ae7f8ce275fa2a4
Closes-Bug: #1642697
Using request.netloc can be confusing because you have to check whether
or not the port is included. We are going to want this seperation in
later patches so expose it on the request.
Change-Id: I2e4bad425fdbc2501727b295752900d4ce4086bc
The Request proxy object is growing bigger and already has its own
category of tests. Move each into their own file with some simplified
helpers for testing.
Change-Id: I0dcc5d8d09feaf3febfcc8a4d114973096279c51
The called_once function was added to the Matcher and the Adapter but
not the top level Mocker function.
Change-Id: I4daa839d931b44fa69133ace663d41c84796b4ba
Closes-Bug: #1630159
When you register a handler the type for text or data can be a string or
a callback that provides a string. By the time you get to
create_response this callback should have been called and only a string
can be passed.
This check originally would have been in place for when a callback was
run and the return value was not the expected type, however
create_response is a public function that can be used on its own.
Correct the exception to clarify in create_response you must pass a
string or bytes and not a callback.
Change-Id: I3e700afddc1f40454f69a564066495bfc77d91c8
Closes-Bug: #1627506
Mock has a useful feature called assert_called_once. We don't have the
asserts here, but having called_once as a property is an easy way to
emulate this.
Change-Id: I59dfb53830195e73884fa0ca8a43b1de91fa1425
When matching URLs both strings are always lowercased to provide case
insensitive matching. Whilst this makes sense for the protocol and the
host names it does not necessarily hold true for paths and query
strings.
A byproduct of this is that the lowercased strings are being reported in
request_history which makes it harder to verify requests you made.
We enable globally and per adapter setting case sensitive matching. This
is intended to become the default in future releases.
Change-Id: I7bde70a52995ecf31a0eaeff96f2823a1a6682b2
Closes-Bug: #1584008
If you set up requests_mock to catch all requests (which I would
recommend) you sometimes get caught with things like file:// paths that
should be allowed to reach the filesystem.
To do this we should allow you to add a matcher that says a specific
route can bypass the catch all and do a real request.
This is a bit of a layer violation but I thought it was easy to start
with, then realized why it wasn't.
Change-Id: Ic2516f78413b88a489c8d6bd2bc39b8ebb5bf273
Closes-Bug: #1501665
A HTTPResponse returns a b'' when you read from a closed socket. BytesIO
raises a ValueError. This only comes to light when you are specifically
reading past the end of the stream in a chunking scenario like
swiftclient does.
Change-Id: I67ed45252deac9472cfb011a5eb89130a3791d6b
Closes-Bug: #1616690
When adding timeout and allow_redirects we added the ability to fetch
some of the connection parameters from the request. Given we have made
that link we should also expose the verify, cert and proxies so users
can test that.
Particularly testing that cert has been passed through to a request is
something i've come across a number of times.
Change-Id: I2c60be44769607c32eaf68ad697656b85cae0737
This information is available via the connection but not on the request
object. Make it available for testing as well.
Closes-Bug: #1565576
Co-Authored-By: Nikolay Martynov <mar.kolya@gmail.com>
Change-Id: I3bfb84472e223421dcdaa04161460e1cbfba352a
Cookies are treated unusually in requests. To handle them exactly as
requests does we would need to create httplib responses with headers and
pass those back. This would be a significant change that is a little
tricky. Instead use the available requests cookies handlers to merge the
cookies into the responses returned from the adapter.
Provide a way to create and preload a CookieJar that will be returned as
part of responses. We also provide the dict interface that requests
does. We don't really have a lot of choice here as these interfaces are
supported by the cookie apis and we would need to actively work around
things to remove that interface.
Change-Id: Ifc1253abc1b4004e81aa7bffad1faf32aedd0d4c
Closes-Bug: #1480835
The FakeHTTPMessage and FakeHTTPResponse objects were only defined for
old versions of requests to provide compatibility. It turns out we need
these for cookie extraction in new versions as well so always define the
object and leave the old compatibility check.
Change-Id: Ifb42fd1c6b1b895b26bee59632ad0dedd78a6256
Now it is possible to mock not only a function but also a class:
class TestClass(object)
def test_func_a(self, m):
m.register_uri('GET', 'http://test.com', text='data')
...
def test_func_b(self, m)
m.register_uri('GET', 'http://test.com', text='data')
...
This new behavior mimics behavior of `patch` from `mock` library.
Added docs for this new feature.
Closes-Bug: #1404805
Change-Id: I8303dc4bc682cf12ffe86e7712b5a1c54de83efb
There is a cyclical reference here where the matcher holds a list of
requests in history and the requests hold a reference to the matcher.
Break this using a weakref so that it can be cleaned up easily.
In practice I don't see this being a problem as people will want to test
that value as part of the test where the mock is still active.
There has not been a release with this functionality so there is no
compatibility change.
Change-Id: Id1669ea40a48d09432367646a0004866947ff72b
Include the matcher that handled this request as part of the request in
history. This will allow users to ensure that a particular history
response was generated by this mock matcher.
This is somewhat of a replacement for simply including the full response
in the history as the response object is not great to work with from a
user perspective and will be transformed between what is returned by the
adapter and what the user will see from requests.
Closes-Bug: #1402874
Change-Id: I5612f080755dbc4ce719aa961d9b29ed54a677a4
This seems to have been an oversight to leave off. Proxy the call and
call_count variables so that they can be retrieved from the mocker.
Change-Id: I5abe9cb0b3870fd9f50a8834193d939268f11efa
Make it easy to specify an exception that should be raised instead of
having to create a callback and raise it manually.
Change-Id: Iabf951e070e9ba8572f5ac14f4e09dbdd5824261
Closes-Bug: #1402496
In requests 1.2 and python 3 headers are bytes so we need to optionally
convert unicode text to bytes when testing that the request headers
match those being checked for.
Change-Id: I02dadc05dd5a93769da646686b938c2b26f6f267
Closes-Bug: #1361528
In requests 1.2 Responses are assumed to always have a HTTPResponse
object and it tries to extract headers from it. We solved this in the
create_response function but we didn't use it in all our tests.
Always use create_response when creating a custom matcher response.
Change-Id: I979f74a64c6f5accf1b88dcc3e5e004131945dff
Closes-Bug: #1361515
This will make it much easier to do matching on last_request and when
doing callbacks.
Change-Id: I705f28b925afdf92c417433375d0f99d36071a6b
Closes-Bug: #1361462
This is used in python 3 rather that the getheaders function that we
already stub out.
Change-Id: Ie147214a49fc744dd2ff1fc958c60547874d05d9
Closes-Bug: #1361527
This works in requests 1.1 and 2.0 but it fails in the 1.2 series. It's
not something we should be too concerned about enforcing at the
requests-mock layer so just fix up the test.
Closes-Bug: #1361497
Change-Id: I8f30262ab3177d9e3d3644c083aed6031eeba78f
Session.prepare_request didn't exist until requests 2.0 which is newer
than our requirements specifies.
We don't really need this function, we can use the older version for
testing rather than increase our minimum required version.
Change-Id: Ia5a8c187e31b8f883e93d8086481f5e08d764bc7
Closes-Bug: #1361488
The response.connection should be the adapter that the request went
through. This is important in some situations (requests-kerberos) where
the connection is reused by the auth plugin to send additional requests.
Change-Id: I87bf996e2edbfb29eba9b4000d976a49cbf6c8cc
Closes-Bug: #1358903
Stub out the httplib.HTTPResponse so that we can be compatible with
Requests < 2.3. We need this to be packaged in major systems that still
have older requests.
Change-Id: Ibec3cead76b7246a6987f9f3b3fef5df6bcc7bc8
- Add Sphinx to test-requirements
- Add a venv environment to tox for zuul runner.
- Fix pep8 issue in tests.
Change-Id: I742802381e588790a86d00371829dc88d21c9e64
We need a way for custom matchers to be able to return fully formed
responses. Make a new function for this and convert the existing
response matchers to use it.
There is a difference between how urlparse handles if it is a protocol
it recognises vs a general URI. This only seems to exist on pypy and
py2.6.
This is not something that should be managed by us. Let it be handled by
the standard urlparse mechanism.
The request object gets picked up in the history. It would be useful to
have a wrapper around it so that we can add some test helpers to the
last_request etc.
This is the same as the Mocker() however it looks more correct to do
@requests_mock.mock() rather than @requests_mock.Mocker().
Ideally mock() would be the decorator and Mocker() the context manager
but it doesn't really matter.
This shouldn't be considered the way to use these methods, using the
strings are fine, but in certain places people don't like using strings
everywhere and so we should have the constants available for them.
Firstly privatize everything on the existing matcher object as it is now
going to be a public interface.
Add called and call_count parameters for querying how many times that
particular mock was invoked. We do this by saving a reference to every
request that passes through. This is inefficient for now, however it
lets us do more interesting things like called_with in the future.