Skip to content

Commit d98a6c4

Browse files
committed
howto.rst: add a chapter about dual-stack support
There was a strange issue with dual-stack, so the documentation is updated. Fixes #61
1 parent f8b88c5 commit d98a6c4

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

doc/howto.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,37 @@ To run an https server, `trustme` can be used to do the heavy lifting:
484484
context.load_cert_chain(crt_file, key_file)
485485
486486
return context
487+
488+
Using httpserver on a dual-stack (IPv4 and IPv6) system
489+
-------------------------------------------------------
490+
491+
*pytest-httpserver* can only listen on one address and it also means that
492+
address family is determined by that. As it relies on *Werkzeug*, it passes the
493+
provided host parameter to it and then it is up to *Werkzeug* how the port
494+
binding is done.
495+
496+
*Werkzeug* determines the address family by examining the string provided. If it
497+
contains a colon (``:``) then it will be an IPv6 (``AF_INET6``) socket, otherwise, it
498+
will be an IPv4 (``AF_INET``) socket. The default string in *pytest-httpserver* is
499+
``localhost`` so by default, the httpserver listens on IPv4. If you want it to
500+
listen on IPv6 address, provide an IPv6 address (``::1`` for example) to it.
501+
502+
It should be noted that dual-stack systems are still working with
503+
*pytest-httpserver* because the clients obtain the possible addresses for the a
504+
given name by calling ``getaddrinfo()`` or similar function which returns the
505+
addresses together with address families, and the client iterates over this
506+
list. In the case when *pytest-httpserver* is listening on ``127.0.0.1``, and
507+
the client uses ``localhost`` name in the url, it will try ``::1`` first, and
508+
then it will move on to ``127.0.0.1``, which will succeed, or vica-versa, where
509+
``127.0.0.1`` will be successful first.
510+
511+
If you want to test a connection error case in your test (such as TLS error),
512+
the client can fail in a strange way as we seen in `this issue
513+
<https://github.com/csernazs/pytest-httpserver/issues/61>`_. In such case,
514+
client tries with ``127.0.0.1`` first, then reaches a TLS error (which is normal
515+
as the test case is about testing for the TLS issue), then it moves on to
516+
``::1``, then it fails with ``Connection reset``. In such case fixing the bind
517+
address to ``127.0.0.1`` (and thereby fixing the host part of the URL returned
518+
by the `url_for` call) solves the issue as the client will receive the address
519+
(``127.0.0.1``) instead of the name (``localhost``) so it won't move on to the
520+
IPv6 address.

0 commit comments

Comments
 (0)