-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extension for delayed matching and response #116
Comments
Hi, "valagit" 😋 This seems a good idea, and it makes sense, however I'm not sure this library can help you. pytest-httpserver pytest plugin is designed to run the httpserver in a separate thread and the test subject (the client) running in the main thread. Your case is the opposite: the client running in a separate thread and the server running in the main thread, so it could raise exceptions. I think there are two options still:
Both of these seems to be a hack for me on this library. I think I can help you creating a POC for the first one. |
This is for issue #116. So the client can be started first in a thread, then the we can set up the expectations and check for errors. Until the expectation is not set, the client is kept back and it won't receive any response.
I have pushed a very basic and ugly POC to the repo (see above), so you can get a glimpse into my idea. |
Hi, :) Thank you for your work. It is almost there. The request handler and most of the code and the fine documentation could be reusable for this special HTTP test server, If I understand well, the PoC HTTPServer's serving thread does not block until no response was given by the test execution thread. I suppose that each incoming request is served by a newly created thread which calls the application method. The dispatch in the PoC may block on getting the request handler from the queue. That is OK. |
This is not true. httpserver runs in its own thread, but the serving of the requests happens in a single thread. That means that it can work with one single http connection.
This cannot be done easily as the API is different. It accepts the On the other hand, in case the API would be in the order you need (eg. accepting I'm not saying that it is impossible but this would again require some clever trick to enforce it to the API which is currently working differently. |
Hi, This is my PoC which contains the logic needed for behave tests. from contextlib import contextmanager
from multiprocessing import Pool
from urllib.parse import urlparse
from queue import Queue, Empty
import requests
import pytest
from werkzeug.wrappers import Request, Response
from pytest_httpserver import HTTPServer
from pytest_httpserver.httpserver import \
UNDEFINED, URIPattern, HeaderValueMatcher, METHOD_ALL, QueryMatcher, RequestHandler
from typing import Any
from typing import Callable
from typing import Iterable
from typing import List
from typing import Mapping
from typing import Optional
from typing import Pattern
from typing import Union
class BRequestHandler(RequestHandler):
def __init__(self, request, response_queue):
super().__init__(None)
self.request = request
self.response_queue = response_queue
def respond_with_data(self, *args, **kwargs):
self._respond_via_queue(super().respond_with_data, *args, **kwargs)
def respond_with_response(self, *args, **kwargs):
self._respond_via_queue(super().respond_with_response, *args, **kwargs)
def _respond_via_queue(self, repond_method, *args, **kwargs):
repond_method(*args, **kwargs)
self.response_queue.put_nowait(self.respond(self.request))
class BHttpServer(HTTPServer):
def __init__(self, *args, timeout=30, **kwargs):
super().__init__(*args, **kwargs)
self.timeout = timeout
self.request_queue = Queue()
self.request_handlers = Queue()
def assert_request(
self,
uri: Union[str, URIPattern, Pattern[str]],
method: str = METHOD_ALL,
data: Union[str, bytes, None] = None,
data_encoding: str = "utf-8",
headers: Optional[Mapping[str, str]] = None,
query_string: Union[None, QueryMatcher, str, bytes, Mapping] = None,
header_value_matcher: Optional[HeaderValueMatcher] = None,
json: Any = UNDEFINED,
timeout: int = 30
) -> BRequestHandler:
matcher = self.create_matcher(
uri,
method=method.upper(),
data=data,
data_encoding=data_encoding,
headers=headers,
query_string=query_string,
header_value_matcher=header_value_matcher,
json=json,
)
try:
request = self.request_queue.get(timeout=timeout)
except Empty:
raise AssertionError(f'Waiting for request {matcher} timed out')
diff = matcher.difference(request)
request_handler = BRequestHandler(request, Queue())
self.request_handlers.put_nowait(request_handler)
if diff:
request_handler.respond_with_response(self.respond_nohandler(request))
raise AssertionError(f'Request {matcher} does not match: {diff}')
return request_handler
def dispatch(self, request: Request) -> Response:
self.request_queue.put_nowait(request)
try:
request_handler = self.request_handlers.get(timeout=self.timeout)
except Empty:
return self.respond_nohandler(request)
try:
return request_handler.response_queue.get(timeout=self.timeout)
except Empty:
assertion = AssertionError(f"No response for requesr: {request_handler.request}")
self.add_assertion(assertion)
raise assertion
@pytest.fixture
def httpserver():
server = BHttpServer(timeout=5)
server.start()
yield server
server.clear()
if server.is_running():
server.stop()
def test_blocking_http_server_behave_workflow(httpserver: BHttpServer):
request = dict(
method='GET',
url=httpserver.url_for('/my/path'),
)
with when_a_request_is_being_sent_to_the_server(request) as server_connection:
client_connection = then_the_server_gets_the_request(httpserver, request)
response = {"foo": "bar"}
when_the_server_responds_to(client_connection, response)
then_the_response_is_got_from(server_connection, response)
def test_blocking_http_server_raises_assertion_error_when_request_does_not_match(httpserver: BHttpServer):
request = dict(
method='GET',
url=httpserver.url_for('/my/path'),
)
with when_a_request_is_being_sent_to_the_server(request):
with pytest.raises(AssertionError) as exc:
httpserver.assert_request(uri='/not/my/path/')
assert '/not/my/path/' in str(exc)
assert 'does not match' in str(exc)
def test_blocking_http_server_raises_assertion_error_when_request_was_not_sent(httpserver: BHttpServer):
with pytest.raises(AssertionError) as exc:
httpserver.assert_request(uri='/my/path/', timeout=1)
assert '/my/path/' in str(exc)
assert 'timed out' in str(exc)
def test_blocking_http_server_ignores_when_request_is_not_asserted(httpserver: BHttpServer):
request = dict(
method='GET',
url=httpserver.url_for('/my/path'),
)
httpserver.timeout = 1
with when_a_request_is_being_sent_to_the_server(request) as server_connection:
assert server_connection.get(timeout=9).text == 'No handler found for this request'
@contextmanager
def when_a_request_is_being_sent_to_the_server(request):
with Pool(1) as pool:
yield pool.apply_async(requests.request, kwds=request)
def then_the_server_gets_the_request(server, request):
_request = dict(request)
del _request['url']
_request['uri'] = get_uri(request['url'])
return server.assert_request(**_request)
def get_uri(url):
url = urlparse(url)
return '?'.join(item for item in [url.path, url.query] if item)
def when_the_server_responds_to(client_connection, response):
client_connection.respond_with_json(response)
def then_the_response_is_got_from(server_connection, response):
assert server_connection.get(timeout=9).json() == response |
Hi, I'm trying to catch up with this. I started to look at your code but first I need to look at mine and read this issue again as a few months have passed since then. Thanks for the code! Zsolt |
Hi Zsolt, Thank you very much for checking the code. In case of the contribution is acceptable, to make the process faster, I created a pull request with slight changes and added documentation. BR, |
Hi,
I have a system with 2 independent interfaces: A <----> System under test (SUT) <----> B
A typical communication sequence looks like:
A sends request to SUT that
triggers that SUT sends request to B;
B responds that
triggers that SUT responds.
I would like to use behave to test this communication sequence.
Currently, using pytest_httpserver, this looks like:
That looks a bit confusing, isn't it?
Would an extension of HTTPServer be possible (and worth to do) which:
So, the dispatch would be delayed until the expect_request call and the response would be delayed until the respond_with_* call.
Usage would look like:
The text was updated successfully, but these errors were encountered: