66 Commits

Author SHA1 Message Date
Jenkins
bf7c25f8d4 Merge "Allow arbirtrary matcher to be add to match" 2017-02-01 04:31:51 +00:00
Jamie Lennox
86fb33d13d Type check the empty string when creating repsonse
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
2017-01-31 21:55:51 +11:00
Jamie Lennox
10c84747e0 Allow arbirtrary matcher to be add to match
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
2017-01-23 10:19:56 +11:00
Jamie Lennox
95dffefe83 Allow for nested mocking
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
2016-11-18 16:10:54 +11:00
Jamie Lennox
81b72f4397 Use create_response for fake http response in tests
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
2016-11-18 16:09:27 +11:00
Jamie Lennox
c3ed2aa479 Add hostname and port properties to request
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
2016-10-13 15:19:46 +11:00
Jamie Lennox
86e33d1eff Move request code and tests into their own files
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
2016-10-13 15:03:21 +11:00
Jenkins
3b48e48ebf Merge "Add called_once to Mocker" 2016-10-05 21:23:46 +00:00
Jamie Lennox
3deef1a454 Add called_once to Mocker
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
2016-10-04 19:11:24 +11:00
voith
890a4fe9ff Fix exception message when creating a response
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
2016-09-26 10:27:35 +10:00
Jenkins
c105f1cae2 Merge "Enable case sensitive matching" 2016-09-14 04:09:28 +00:00
Jamie Lennox
0c6e567ec7 Add a called_once property to adapter
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
2016-09-02 05:56:17 +00:00
Jamie Lennox
1b08dcc705 Enable case sensitive matching
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
2016-08-26 11:54:50 +10:00
Jenkins
e42b9828dc Merge "Return b'' when reading closed response." 2016-08-26 01:35:55 +00:00
Jamie Lennox
b2026313e3 Allow doing real_http per mock via the mocker
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
2016-08-25 17:43:19 +10:00
Jamie Lennox
fe147914a6 Return b'' when reading closed response.
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
2016-08-25 10:34:49 +10:00
Jamie Lennox
780eb4b4fe Expose cert and proxies as attributes of the last request.
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
2016-05-09 13:50:59 +10:00
Jamie Lennox
459e13cb24 Add timeout and allow_retries to RequestProxy
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
2016-04-04 10:17:10 +10:00
Jamie Lennox
fe37c6cc3a Support Cookies
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
2015-11-18 12:00:09 +11:00
Jamie Lennox
1b10e30f1e Expose Fake objects in compat
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
2015-11-18 12:00:06 +11:00
Jenkins
9547a7b6b1 Merge "Allow to decorate class with mock" 2015-01-15 01:44:21 +00:00
Sebastian Kalinowski
1774bb11a9 Allow to decorate class with mock
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
2014-12-22 08:37:55 +01:00
Jamie Lennox
ace78cd9c9 Use weakref to hold matcher
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
2014-12-19 09:39:36 +10:00
Jenkins
931f2b4989 Merge "Include matcher in request history" 2014-12-18 06:05:28 +00:00
Jenkins
7b0648a921 Merge "Add called and call_count to mocker" 2014-12-16 19:35:41 +00:00
Jamie Lennox
55bbc238c6 Include matcher in request history
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
2014-12-16 14:46:54 +10:00
Jamie Lennox
755257cbdc Add called and call_count to mocker
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
2014-12-16 14:41:29 +10:00
Jamie Lennox
d8f0394293 Allow specifying an exception to raise
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
2014-12-15 15:31:03 +10:00
Jenkins
ca1c6dd58a Merge "Allow interpreting a sent request as text and json" 2014-12-02 00:41:38 +00:00
Jenkins
c6bf1637a8 Merge "Convert headers to bytes testing request_headers" 2014-08-28 07:09:20 +00:00
Jamie Lennox
befd86afdf Convert headers to bytes testing request_headers
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
2014-08-28 11:03:08 +10:00
Jamie Lennox
7ba4595f95 Don't use requests.Response object directly
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
2014-08-28 10:18:24 +10:00
Jamie Lennox
968cc05105 Allow interpreting a sent request as text and json
This will make it much easier to do matching on last_request and when
doing callbacks.

Change-Id: I705f28b925afdf92c417433375d0f99d36071a6b
Closes-Bug: #1361462
2014-08-27 16:42:58 +10:00
Jenkins
d83b986520 Merge "Add get_all to compatibility function" 2014-08-26 19:31:14 +00:00
Jenkins
743edffba3 Merge "Don't use session.prepare_request" 2014-08-26 07:53:48 +00:00
Jamie Lennox
bdd2562d4f Add get_all to compatibility function
This is used in python 3 rather that the getheaders function that we
already stub out.

Change-Id: Ie147214a49fc744dd2ff1fc958c60547874d05d9
Closes-Bug: #1361527
2014-08-26 17:43:07 +10:00
Jamie Lennox
b0c746a04e Lowercase scheme in regexp test
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
2014-08-26 16:04:33 +10:00
Jamie Lennox
4f32485776 Don't use session.prepare_request
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
2014-08-26 15:03:59 +10:00
Jenkins
a2df6992c7 Merge "Correctly set adapter to response.connection" 2014-08-20 23:44:59 +00:00
Jamie Lennox
85d57f5678 Remove old _Context object
This was moved in a previous commit but apparently not deleted from the
old location.

Change-Id: I8efe097772a7a9fbf084ed105a2e650d0f16a19e
2014-08-20 09:34:09 +10:00
Jamie Lennox
df0e4ebd7e Correctly set adapter to response.connection
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
2014-08-20 09:31:30 +10:00
Jamie Lennox
eadbe7b1b2 Make compatible with requests 1.2
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
2014-08-06 07:55:22 +10:00
Jamie Lennox
3ca4acb494 Fix tests running for zuul
- Add Sphinx to test-requirements
- Add a venv environment to tox for zuul runner.
- Fix pep8 issue in tests.

Change-Id: I742802381e588790a86d00371829dc88d21c9e64
2014-08-01 14:48:45 +10:00
Jamie Lennox
35951b0386 Make a public create_response function
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.
2014-07-30 15:21:21 +10:00
Jamie Lennox
1246046ffe Change query string tests
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.
2014-07-30 11:45:38 +10:00
Jamie Lennox
1e6e6a1b72 Add wrapper around Request object
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.
2014-07-30 11:08:21 +10:00
Jamie Lennox
4ae5f15568 Add a mock() function
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.
2014-07-30 06:40:10 +10:00
Jamie Lennox
96fbd5a0ee Export symbols for HTTP methods
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.
2014-07-30 06:28:33 +10:00
Jamie Lennox
d792bb4176 Standardize request history tracking
Make a base class so that the adapter and the matcher both do request
history tracking in the same way and they both provide the same
interface.
2014-07-29 17:49:49 +10:00
Jamie Lennox
22760510b7 Allow querying if a mock was called.
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.
2014-07-29 17:40:50 +10:00