[Python-modules-commits] [pytest-httpbin] 01/05: New upstream version 0.3.0

Pierre-Elliott Bécue peb-guest at moszumanska.debian.org
Fri Jan 12 12:20:03 UTC 2018


This is an automated email from the git hooks/post-receive script.

peb-guest pushed a commit to branch debian/master
in repository pytest-httpbin.

commit 54b2575be21a7db6c405bf6eeacaf794810e8a4e
Author: Pierre-Elliott Bécue <becue at crans.org>
Date:   Fri Jan 12 13:03:01 2018 +0100

    New upstream version 0.3.0
---
 .travis.yml               |  8 ++++----
 README.md                 | 16 +++++++++++++++-
 pytest_httpbin/serve.py   | 16 +++++++++++++---
 pytest_httpbin/version.py |  2 +-
 setup.py                  |  4 +++-
 tests/test_server.py      | 42 ++++++++++++++++++++++++++++++++++++++++++
 tox.ini                   |  3 ++-
 7 files changed, 80 insertions(+), 11 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 2e736a4..fe2068d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,8 +4,8 @@ python:
 - 2.6
 - 2.7
 - 3.4
+- 3.5
+- 3.6
 - pypy
-install:
-- pip install requests
-- python setup.py install
-script: ./runtests.sh
+install: pip install -U tox-travis
+script: tox
diff --git a/README.md b/README.md
index 30b93a4..cb830b0 100644
--- a/README.md
+++ b/README.md
@@ -32,7 +32,7 @@ def test_that_my_library_works_kinda_ok(httpbin_secure):
     assert requests.get(httpbin_secure.url + '/get/').status_code == 200
 ```
 
-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, 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`.
+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`.
 
 For example in requests, you can set the `REQUESTS_CA_BUNDLE` python path.  You can run your tests like this:
 
@@ -83,6 +83,14 @@ class TestClassBassedTests(unittest.TestCase):
         assert requests.get(self.httpbin_secure.url + '/get').response
 ```
 
+## Running the server on fixed port
+
+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:
+
+```bash
+HTTPBIN_HTTP_PORT=8080 HTTPBIN_HTTPS_PORT=8443 py.test tests/
+```
+
 ## Installation
 
 All you need to do is this:
@@ -97,6 +105,8 @@ and your tests executed by pytest all will have access to the `httpbin` and `htt
 
 pytest-httpbin suports Python 2.6, 2.7, 3.4, and pypy.  It will automatically install httpbin and flask when you install it from pypi.
 
+[httpbin](https://github.com/kennethreitz/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
+
 ## Running the pytest-httpbin test suite
 
 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.
@@ -115,6 +125,10 @@ tox
 
 ## Changelog
 
+* 0.3.0
+  * Allow to run httpbin on fixed port using environment variables (thanks @hroncok)
+  * Allow server to be thread.join()ed (thanks @graingert)
+  * Add support for Python 3.6 (thanks @graingert)
 * 0.2.3: 
   * Another attempt to fix #32 (Rare bug, only happens on Travis)
 * 0.2.2: 
diff --git a/pytest_httpbin/serve.py b/pytest_httpbin/serve.py
index 7c9953e..07a92dc 100644
--- a/pytest_httpbin/serve.py
+++ b/pytest_httpbin/serve.py
@@ -77,13 +77,17 @@ class SecureWSGIServer(WSGIServer):
         # Thanks, WSGIRequestHandler!!
 
 
-class Server(threading.Thread):
+class Server(object):
     """
     HTTP server running a WSGI application in its own thread.
     """
 
+    port_envvar = 'HTTPBIN_HTTP_PORT'
+
     def __init__(self, host='127.0.0.1', port=0, application=None, **kwargs):
         self.app = application
+        if self.port_envvar in os.environ:
+            port = int(os.environ[self.port_envvar])
         self._server = make_server(
             host,
             port,
@@ -95,13 +99,17 @@ class Server(threading.Thread):
         self.port = self._server.server_address[1]
         self.protocol = 'http'
 
-        super(Server, self).__init__(
+        self._thread = threading.Thread(
             name=self.__class__,
             target=self._server.serve_forever,
         )
 
     def __del__(self):
-        self.stop()
+        if hasattr(self, '_server'):
+            self.stop()
+
+    def start(self):
+        self._thread.start()
 
     def __add__(self, other):
         return self.url + other
@@ -118,6 +126,8 @@ class Server(threading.Thread):
 
 
 class SecureServer(Server):
+    port_envvar = 'HTTPBIN_HTTPS_PORT'
+
     def __init__(self, host='127.0.0.1', port=0, application=None, **kwargs):
         kwargs['server_class'] = SecureWSGIServer
         super(SecureServer, self).__init__(host, port, application, **kwargs)
diff --git a/pytest_httpbin/version.py b/pytest_httpbin/version.py
index d93b5b2..0404d81 100644
--- a/pytest_httpbin/version.py
+++ b/pytest_httpbin/version.py
@@ -1 +1 @@
-__version__ = '0.2.3'
+__version__ = '0.3.0'
diff --git a/setup.py b/setup.py
index 11c649c..2b2ed43 100644
--- a/setup.py
+++ b/setup.py
@@ -44,13 +44,15 @@ setup(
         'Programming Language :: Python :: 2.7',
         'Programming Language :: Python :: 3',
         'Programming Language :: Python :: 3.4',
+        'Programming Language :: Python :: 3.5',
+        'Programming Language :: Python :: 3.6',
     ],
 
     # What does your project relate to?
     keywords='pytest-httpbin testing pytest httpbin',
     packages=find_packages(exclude=["contrib", "docs", "tests*"]),
     include_package_data = True, # include files listed in MANIFEST.in
-    install_requires = ['Flask','decorator','httpbin','six'],
+    install_requires = ['httpbin','six'],
 
     # the following makes a plugin available to pytest
     entry_points = {
diff --git a/tests/test_server.py b/tests/test_server.py
index 9e27685..d97d60f 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -2,9 +2,12 @@
 # -*- coding: utf8 -*-
 # vim: set fileencoding=utf8 :
 
+import os
 import requests
 import pytest
 from util import get_raw_http_response
+from pytest_httpbin import serve
+from httpbin import app as httpbin_app
 
 
 def test_content_type_header_not_automatically_added(httpbin):
@@ -50,3 +53,42 @@ def test_dont_crash_on_certificate_problems(httpbin_secure):
         httpbin_secure + '/get',
         verify = True,
     )
+
+
+ at pytest.mark.parametrize('protocol', ('http', 'https'))
+def test_fixed_port_environment_variables(protocol):
+    """
+    Note that we cannot test the fixture here because it is session scoped
+    and was already started. Thus, let's just test a new Server instance.
+    """
+    if protocol == 'http':
+        server_cls = serve.Server
+        envvar = 'HTTPBIN_HTTP_PORT'
+    elif protocol == 'https':
+        server_cls = serve.SecureServer
+        envvar = 'HTTPBIN_HTTPS_PORT'
+    else:
+        raise RuntimeError('Unexpected protocol param: {0}'.format(protocol))
+
+    # just have different port to avoid adrress already in use
+    # if the second test run too fast after the first one (happens on pypy)
+    port = 12345 + len(protocol)
+
+    try:
+        envvar_original = os.environ.get(envvar, None)
+        os.environ[envvar] = str(port)
+        server = server_cls(application=httpbin_app)
+        assert server.port == port
+    finally:
+        # if we don't do this, it blocks:
+        try:
+            server.start()
+            server.stop()
+        except UnboundLocalError:
+            pass
+
+        # restore the original environ:
+        if envvar_original is None:
+            del os.environ[envvar]
+        else:
+            os.environ[envvar] = envvar_original
diff --git a/tox.ini b/tox.ini
index 472b865..9437a9e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,9 +1,10 @@
 # content of: tox.ini , put in same dir as setup.py
 
 [tox]
-envlist = py26, py27, py33, py34, pypy
+envlist = py26, py27, py33, py34, py35, py36, pypy, pypy3
 
 [testenv]
 deps = pytest
        requests
+       py26: httpbin==0.5.0
 commands = ./runtests.sh {posargs:tests/}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/pytest-httpbin.git



More information about the Python-modules-commits mailing list