You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[httpbin](https://httpbin.org/) is an amazing web service for testing HTTP libraries. It has several great endpoints that can test pretty much everything you need in a HTTP library. The only problem is: maybe you don't want to wait for your tests to travel across the Internet and back to make assertions against a remote web service (speed), and maybe you want to work offline (convenience).
5
+
[httpbin](https://httpbin.org/) is an amazing web service for testing HTTP libraries. It
6
+
has several great endpoints that can test pretty much everything you need in a HTTP
7
+
library. The only problem is: maybe you don't want to wait for your tests to travel
8
+
across the Internet and back to make assertions against a remote web service (speed),
9
+
and maybe you want to work offline (convenience).
6
10
7
-
Enter **pytest-httpbin**. Pytest-httpbin creates a [pytest fixture](https://pytest.org/latest/fixture.html) that is dependency-injected into your tests. It automatically starts up a HTTP server in a separate thread running httpbin and provides your test with the URL in the fixture. Check out this example:
11
+
Enter **pytest-httpbin**. Pytest-httpbin creates a
12
+
[pytest fixture](https://pytest.org/latest/fixture.html) that is dependency-injected
13
+
into your tests. It automatically starts up a HTTP server in a separate thread running
14
+
httpbin and provides your test with the URL in the fixture. Check out this example:
8
15
9
16
```python
10
17
deftest_that_my_library_works_kinda_ok(httpbin):
@@ -22,7 +29,6 @@ If you're making a lot of requests to httpbin, it can radically speed up your te
It's actually starting 2 web servers in separate threads in the background: one HTTP and one HTTPS. The servers are started on a random port (see below for fixed port support), on the loopback interface on your machine. Pytest-httpbin includes a self-signed certificate. If your library verifies certificates against a CA (and it should), you'll have to add the CA from pytest-httpbin. The path to the pytest-httpbin CA bundle can by found like this `python -m pytest_httpbin.certs`.
41
+
It's actually starting 2 web servers in separate threads in the background: one HTTP and
42
+
one HTTPS. The servers are started on a random port (see below for fixed port support),
43
+
on the loopback interface on your machine. Pytest-httpbin includes a self-signed
44
+
certificate. If your library verifies certificates against a CA (and it should), you'll
45
+
have to add the CA from pytest-httpbin. The path to the pytest-httpbin CA bundle can by
46
+
found like this `python -m pytest_httpbin.certs`.
36
47
37
-
For example in requests, you can set the `REQUESTS_CA_BUNDLE` python path. You can run your tests like this:
48
+
For example in requests, you can set the `REQUESTS_CA_BUNDLE` python path. You can run
* join(string): Returns the results of calling `urlparse.urljoin` with the url from the injected server automatically applied as the first argument. You supply the second argument
65
+
- join(string): Returns the results of calling `urlparse.urljoin` with the url from the
66
+
injected server automatically applied as the first argument. You supply the second
67
+
argument
54
68
55
-
Also, I defined `__add__` on the object to append to `httpbin.url`. This means you can do stuff like `httpbin + '/get'` instead of `httpbin.url + '/get'`.
69
+
Also, I defined `__add__` on the object to append to `httpbin.url`. This means you can
70
+
do stuff like `httpbin + '/get'` instead of `httpbin.url + '/get'`.
56
71
57
72
## Testing both HTTP and HTTPS endpoints with one test
58
73
59
-
If you ever find yourself needing to test both the http and https version of and endpoint, you can use the `httpbin_both` funcarg like this:
60
-
74
+
If you ever find yourself needing to test both the http and https version of and
75
+
endpoint, you can use the `httpbin_both` funcarg like this:
Through the magic of pytest parametrization, this function will actually execute twice: once with an http url and once with an https url.
82
+
Through the magic of pytest parametrization, this function will actually execute twice:
83
+
once with an http url and once with an https url.
68
84
69
85
## Using pytest-httpbin with unittest-style test cases
70
86
71
-
I have provided 2 additional fixtures to make testing with class-based tests easier. I have also provided a couple decorators that provide some syntactic sugar around the pytest method of adding the fixtures to class-based tests. Just add the `use_class_based_httpbin` and/or `use_class_based_httpbin_secure` class decorators to your class, and then you can access httpbin using self.httpbin and self.httpbin_secure.
87
+
I have provided 2 additional fixtures to make testing with class-based tests easier. I
88
+
have also provided a couple decorators that provide some syntactic sugar around the
89
+
pytest method of adding the fixtures to class-based tests. Just add the
90
+
`use_class_based_httpbin` and/or `use_class_based_httpbin_secure` class decorators to
91
+
your class, and then you can access httpbin using self.httpbin and self.httpbin_secure.
72
92
73
93
```python
74
94
import pytest_httpbin
@@ -85,7 +105,10 @@ class TestClassBassedTests(unittest.TestCase):
85
105
86
106
## Running the server on fixed port
87
107
88
-
Sometimes a randomized port can be a problem. Worry not, you can fix the port number to a desired value with the `HTTPBIN_HTTP_PORT` and `HTTPBIN_HTTPS_PORT` environment variables. If those are defined during pytest plugins are loaded, `httbin` and `httpbin_secure` fixtures will run on given ports. You can run your tests like this:
108
+
Sometimes a randomized port can be a problem. Worry not, you can fix the port number to
109
+
a desired value with the `HTTPBIN_HTTP_PORT` and `HTTPBIN_HTTPS_PORT` environment
110
+
variables. If those are defined during pytest plugins are loaded, `httbin` and
111
+
`httpbin_secure` fixtures will run on given ports. You can run your tests like this:
To install from [PyPI](https://pypi.org/project/pytest-httpbin/), all you need to do is this:
122
+
To install from [PyPI](https://pypi.org/project/pytest-httpbin/), all you need to do is
123
+
this:
100
124
101
125
```bash
102
126
pip install pytest-httpbin
103
127
```
104
128
105
-
and your tests executed by pytest all will have access to the `httpbin` and `httpbin_secure` funcargs. Cool right?
129
+
and your tests executed by pytest all will have access to the `httpbin` and
130
+
`httpbin_secure` funcargs. Cool right?
106
131
107
132
## Support and dependencies
108
133
109
-
pytest-httpbin supports Python 2.6, 2.7, 3.4-3.6, and pypy. It will automatically install httpbin and flask when you install it from PyPI.
134
+
pytest-httpbin supports Python 2.6, 2.7, 3.4-3.6, and pypy. It will automatically
135
+
install httpbin and flask when you install it from PyPI.
110
136
111
-
[httpbin](https://github.com/postmanlabs/httpbin) itself does not support python 2.6 as of version 0.6.0, when the Flask-common dependency was added. If you need python 2.6 support pin the httpbin version to 0.5.0
137
+
[httpbin](https://github.com/postmanlabs/httpbin) itself does not support python 2.6 as
138
+
of version 0.6.0, when the Flask-common dependency was added. If you need python 2.6
139
+
support pin the httpbin version to 0.5.0
112
140
113
141
## Running the pytest-httpbin test suite
114
142
115
-
If you want to run pytest-httpbin's test suite, you'll need to install requests and pytest, and then use the ./runtests.sh script.
143
+
If you want to run pytest-httpbin's test suite, you'll need to install requests and
144
+
pytest, and then use the ./runtests.sh script.
116
145
117
146
```bash
118
147
pip install pytest
@@ -128,80 +157,81 @@ tox
128
157
129
158
## Changelog
130
159
131
-
* 1.0.2
132
-
* Switch from travis to github actions
133
-
* This will be the last release to support Python 2.6, 2.7 or 3.6
134
-
* 1.0.1
135
-
* httpbin_secure: fix redirect Location to have "https://" scheme (#62) - thanks @immerrr
136
-
* Include regression tests in pypi tarball (#56) - thanks @kmosiejczuk
137
-
* 1.0.0
138
-
* Update included self-signed cert to include IP address in SAN (See #52). Full version bump because this could be a breaking change for those depending on the certificate missing the IP address in the SAN (as it seems the requests test suite does)
139
-
* Only use @pytest.fixture decorator once (thanks @hroncok)
140
-
* Fix a few README typos (thanks @hemberger)
141
-
* 0.3.0
142
-
* Allow to run httpbin on fixed port using environment variables (thanks @hroncok)
143
-
* Allow server to be thread.join()ed (thanks @graingert)
144
-
* Add support for Python 3.6 (thanks @graingert)
145
-
* 0.2.3:
146
-
* Another attempt to fix #32 (Rare bug, only happens on Travis)
147
-
* 0.2.2:
148
-
* Fix bug with python3
149
-
* 0.2.1:
150
-
* Attempt to fix strange, impossible-to-reproduce bug with broken SSL certs
151
-
that only happens on Travis (#32) [Bad release, breaks py3]
152
-
* 0.2.0:
153
-
* Remove threaded HTTP server. I built it for Requests, but they deleted
154
-
their threaded test since it didn't really work very well. The threaded
155
-
server seems to cause some strange problems with HTTP chunking, so I'll
156
-
just remove it since nobody is using it (I hope)
157
-
* 0.1.1:
158
-
* Fix weird hang with SSL on pypy (again)
159
-
* 0.1.0:
160
-
* Update server to use multithreaded werkzeug server
161
-
* 0.0.7:
162
-
* Update the certificates (they expired)
163
-
* 0.0.6:
164
-
* Fix an issue where pypy was hanging when a request was made with an invalid
160
+
- 1.0.2
161
+
- Switch from travis to github actions
162
+
- This will be the last release to support Python 2.6, 2.7 or 3.6
163
+
- 1.0.1
164
+
- httpbin_secure: fix redirect Location to have "https://" scheme (#62) - thanks
165
+
@immerrr
166
+
- Include regression tests in pypi tarball (#56) - thanks @kmosiejczuk
167
+
- 1.0.0
168
+
- Update included self-signed cert to include IP address in SAN (See #52). Full
169
+
version bump because this could be a breaking change for those depending on the
170
+
certificate missing the IP address in the SAN (as it seems the requests test suite
171
+
does)
172
+
- Only use @pytest.fixture decorator once (thanks @hroncok)
173
+
- Fix a few README typos (thanks @hemberger)
174
+
- 0.3.0
175
+
- Allow to run httpbin on fixed port using environment variables (thanks @hroncok)
176
+
- Allow server to be thread.join()ed (thanks @graingert)
177
+
- Add support for Python 3.6 (thanks @graingert)
178
+
- 0.2.3:
179
+
- Another attempt to fix #32 (Rare bug, only happens on Travis)
180
+
- 0.2.2:
181
+
- Fix bug with python3
182
+
- 0.2.1:
183
+
- Attempt to fix strange, impossible-to-reproduce bug with broken SSL certs that only
184
+
happens on Travis (#32) [Bad release, breaks py3]
185
+
- 0.2.0:
186
+
- Remove threaded HTTP server. I built it for Requests, but they deleted their
187
+
threaded test since it didn't really work very well. The threaded server seems to
188
+
cause some strange problems with HTTP chunking, so I'll just remove it since nobody
189
+
is using it (I hope)
190
+
- 0.1.1:
191
+
- Fix weird hang with SSL on pypy (again)
192
+
- 0.1.0:
193
+
- Update server to use multithreaded werkzeug server
194
+
- 0.0.7:
195
+
- Update the certificates (they expired)
196
+
- 0.0.6:
197
+
- Fix an issue where pypy was hanging when a request was made with an invalid
165
198
certificate
166
-
* 0.0.5:
167
-
* Fix broken version parsing in 0.0.4
168
-
* 0.0.4:
169
-
***Bad release: Broken version parsing**
170
-
* Fix `BadStatusLine` error that occurs when sending multiple requests
171
-
in a single session (PR #16). Thanks @msabramo!
172
-
* Fix #9 ("Can't be installed at the same time than pytest?") (PR
173
-
#14). Thanks @msabramo!
174
-
* Add `httpbin_ca_bundle` pytest fixture. With this fixture there is
175
-
no need to specify the bundle on every request, as it will
176
-
automatically set `REQUESTS_CA_BUNDLE` if using
177
-
[requests](https://docs.python-requests.org/). And you don't have to
178
-
care about where it is located (PR #8). Thanks @t-8ch!
179
-
* 0.0.3: Add a couple test fixtures to make testing old class-based test suites
180
-
easier
181
-
* 0.0.2: Fixed a couple bugs with the wsgiref server to bring behavior in line
182
-
with httpbin.org, thanks @jakubroztocil for the bug reports
183
-
* 0.0.1: Initial release
199
+
- 0.0.5:
200
+
- Fix broken version parsing in 0.0.4
201
+
- 0.0.4:
202
+
-**Bad release: Broken version parsing**
203
+
- Fix `BadStatusLine` error that occurs when sending multiple requests in a single
204
+
session (PR #16). Thanks @msabramo!
205
+
- Fix #9 ("Can't be installed at the same time than pytest?") (PR #14). Thanks
206
+
@msabramo!
207
+
- Add `httpbin_ca_bundle` pytest fixture. With this fixture there is no need to
208
+
specify the bundle on every request, as it will automatically set
209
+
`REQUESTS_CA_BUNDLE` if using [requests](https://docs.python-requests.org/). And you
210
+
don't have to care about where it is located (PR #8). Thanks @t-8ch!
211
+
- 0.0.3: Add a couple test fixtures to make testing old class-based test suites easier
212
+
- 0.0.2: Fixed a couple bugs with the wsgiref server to bring behavior in line with
213
+
httpbin.org, thanks @jakubroztocil for the bug reports
214
+
- 0.0.1: Initial release
184
215
185
216
## License
186
217
187
218
The MIT License (MIT)
188
219
189
220
Copyright (c) 2014-2019 Kevin McCarthy
190
221
191
-
Permission is hereby granted, free of charge, to any person obtaining a copy
192
-
of this software and associated documentation files (the "Software"), to deal
193
-
in the Software without restriction, including without limitation the rights
194
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
195
-
copies of the Software, and to permit persons to whom the Software is
196
-
furnished to do so, subject to the following conditions:
197
-
198
-
The above copyright notice and this permission notice shall be included in
199
-
all copies or substantial portions of the Software.
200
-
201
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
202
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
204
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
205
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
206
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
207
-
THE SOFTWARE.
222
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
223
+
software and associated documentation files (the "Software"), to deal in the Software
224
+
without restriction, including without limitation the rights to use, copy, modify,
225
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
226
+
permit persons to whom the Software is furnished to do so, subject to the following
227
+
conditions:
228
+
229
+
The above copyright notice and this permission notice shall be included in all copies or
230
+
substantial portions of the Software.
231
+
232
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
233
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
234
+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
235
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
236
+
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0 commit comments