extras.http_server — Simple HTTP Server (Deprecated)


An HTTP YellowService used to easily mock and test HTTP connections.

Deprecated since version 0.6.6: Use webserver instead.

Example Usage
with HttpService().start() as service:
  @service.patch_route('GET', '/hello/world')
  def hello_world(handler: RouterHTTPRequestHandler):
     return "hi there"
  # hello_world is now a context manager
  with hello_world:
     # within this scope, the path "/hello/world" will return a 200 response
     # with the body "hi there"
     assert requests.get(service.local_url+"/hello/world").text == "hi there"
  # routes can also be set without a function
  with service.patch_route('GET', '/meaning_of_life', '42'):
     assert requests.get(service.local_url+"/meaning_of_life").content == b'42'
class extras.http_server.HttpService(host='0.0.0.0', port=0, name=None)[source]

A Simple HTTP server wrapped as YellowService.

Inherits from YellowService.

Parameters:
  • host (str) – is the listening host. Defaults to "0.0.0.0" to listen on all IPv4 interfaces.

  • port (int) – is the listening port. Defaults to 0 to let the OS choose a free port. Use server_port to fetch the port.

  • name (Optional[str]) – is a string identifying this server and thread for logging purposes. If None, a default name with a running number is generated.

property server_port: int[source]

HTTP Server listening port.

property local_url: str[source]

Full URL to access the HTTP Server from localhost (including port).

property container_url: str[source]

Full URL to access the HTTP Server from inside a locally-running container (including port).

patch_route(method, route, side_effect=..., name=None)[source]

Create a context manager that temporarily adds a route handler to the service.

Parameters:
  • method (str) – The request method to add the route to.

  • route (str | Pattern[str]) – The route to attach the side effect to. All routes must begin with a slash /. Alternatively, The route may be a compiled regex pattern, in which case the request path must fully match it. The match object is then stored in RouterHTTPRequestHandler.match, to be used by a side-effect callable.

  • side_effect (int | bytes | str | Callable) –

    The result of the route if requested, or a callback to return the result. Accepts following types:

    • int: to return the value as the HTTP status code, without a body.

    • bytes: to return 200, with the value as the response body.

    • str: invalid if the value is non-ascii, return 200 with the value, translated to bytes, as the response body.

    • Callable: A callback that accepts a RouterHTTPRequestHandler positional argument. Should return any of the above values for similar effects. Alternatively, the callback can return the handler itself to indicate that the callback handled the response on its own and no further actions are needed.

    If side_effect is not specified, this method can be used as a decorator.

Returns:

A context manager that will add the route to the service upon entry, and remove it upon exit.

class extras.http_server.RouterHTTPRequestHandler[source]

Inherits from http.server.BaseHTTPRequestHandler and adds the following utility methods:

match: Match[str] | True[source]

If the route has a regular expression path, the match of the path to route’s pattern will be stored here. If the path is a string, will be True.

body() bytes | None[source]

Return the body of the request as bytes, or None if it’s empty.

path_params(**kwargs)[source]

Extract parameters from the query string.

Params kwargs:

Forwarded to parse_qs().

Returns:

A mapping between parameter name and a list of the values provided in the query.

Return type:

Mapping[str, list[str]]

parse_url() ParseResult[source]

Parse the request URL into a ParseResult object.