Skip to content

Commit

Permalink
PoC: add proxy support
Browse files Browse the repository at this point in the history
Proof of concept implementation of the proxy support described in #39.
It uses wsgiprox and re-uses werkzeug's own wsgi server, and it works like charm.

Currently implemented in a separate class.
See test_proxy.py for example.

Two minor things:
- wsgiprox uses gevent, yay
- creates a 'ca' directory in cwd
  • Loading branch information
csernazs committed Oct 22, 2020
1 parent 077a3f2 commit 51125dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pytest_httpserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# flake8: noqa

from .httpserver import HTTPServer
from .httpserver import HTTPServer, HTTPProxy
from .httpserver import HTTPServerError, Error, NoHandlerError
from .httpserver import WaitingSettings, HeaderValueMatcher, RequestHandler
from .httpserver import URIPattern, URI_DEFAULT, METHOD_ALL
11 changes: 11 additions & 0 deletions pytest_httpserver/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import werkzeug.urls
from werkzeug.datastructures import MultiDict
from wsgiprox.wsgiprox import WSGIProxMiddleware


URI_DEFAULT = ""
METHOD_ALL = "__ALL"
Expand Down Expand Up @@ -1118,3 +1120,12 @@ def __exit__(self, *args, **kwargs):
"""
if self.is_running():
self.stop()


class HTTPProxy(HTTPServer):
def start(self):
proxy = WSGIProxMiddleware(self.application, "/proxy/", "wsgiprox")
self.server = make_server(self.host, self.port, proxy, ssl_context=self.ssl_context)
self.port = self.server.port # Update port (needed if `port` was set to 0)
self.server_thread = threading.Thread(target=self.thread_target)
self.server_thread.start()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
python_requires=">=3.4",
install_requires=[
"typing;python_version<'3.5'",
"wsgiprox",
"werkzeug"
],
extras_require={
Expand Down
16 changes: 16 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import requests
from pytest_httpserver import HTTPProxy


def test_proxy():
proxy = HTTPProxy(port=8080)
proxy.expect_request("/proxy/http://example.com/path/file.html").respond_with_data("Hello world!")

proxy.start()
try:
resp = requests.get("http://example.com/path/file.html", proxies={"http": "http://localhost:8080/"})
assert resp.status_code == 200
assert resp.text == "Hello world!"
finally:
proxy.stop()

0 comments on commit 51125dd

Please sign in to comment.