kyoukai.testing

Testing helpers for Kyoukai.

Functions

to_wsgi_environment(headers, method, path, ...) Produces a new WSGI environment from a set of data that is passed in.

Classes

Blueprint(name[, parent, prefix, ...]) A Blueprint class contains a Map of URL rules, which is checked and ran for every
BytesIO Buffered I/O implementation using an in-memory bytes buffer.
Context([parent, default_timeout]) Contexts give request handlers and callbacks access to resources.
Kyoukai(application_name, *[, server_name]) The Kyoukai type is the core of the Kyoukai framework, and the core of your web application based upon the Kyoukai framework.
Request(environ[, populate_request, shallow]) Full featured request object implementing the following mixins:
Response([response, status, headers, ...]) Full featured response object implementing the following mixins:
TestKyoukai(*args[, base_context]) A special subclass that allows you to easily test your Kyoukai-based app.
class kyoukai.testing._TestingBpCtxManager(app)[source]

Bases: object

A context manager that is returned from testing_bp(). When entered, this will produce a new Blueprint object, that is then set onto the test application as the root blueprint.

After exiting, it will automatically restore the old root Blueprint onto the application, allowing complete isolation of individual test routes away from eachother.

class kyoukai.testing.TestKyoukai(*args, base_context=None, **kwargs)[source]

Bases: kyoukai.app.Kyoukai

A special subclass that allows you to easily test your Kyoukai-based app.

Parameters:base_context (Optional[Context]) – The base context to use for all request testing.
classmethod wrap_existing_app(other_app, base_context=None)[source]

Wraps an existing app in a test frame.

This allows easy usage of writing unit tests:

# main.py
kyk = Kyoukai("my_app")

# test.py
testing = TestKyoukai.wrap_existing_app(other_app)
# use testing as you would normally
Parameters:
  • other_app (Kyoukai) –

    The application object to wrap. Internally, this creates a new instance of ourselves, then sets the process_request of the subclass to the copied object.

    This means whenever inject_request is called, it will use the old app’s process_request to run with, which will use the environment of the previous instance.

    Of course, if the old app has any side effects upon process_request, these side effects will happen when the testing application runs as well, as the old app is completely copied over.

  • base_context (Optional[Context]) – The base context to use for this.
testing_bp()[source]

Context handler that allows with TestKyoukai.testing_bp() as bp:

You can then register items onto this new root blueprint until __exit__, which will then destroy the blueprint.

Return type:_TestingBpCtxManager
coroutine inject_request(self, headers, url, method='GET', body=None)[source]

Injects a request into the test client.

This will automatically create the correct context.

Parameters:
  • headers (dict) – The headers to use.
  • body (Optional[str]) – The body to use.
  • url (str) – The URL to use.
  • method (str) – The method to use.
Return type:

Response

Returns:

The result.

finalize()

Finalizes the app and blueprints.

This will calculate the current werkzeug.routing.Map which is required for routing to work.

coroutine handle_httpexception(self, ctx, exception, environ=None)

Handle a HTTP Exception.

Parameters:
Return type:

Response

Returns:

A werkzeug.wrappers.Response that handles this response.

log_route(request, code)

Logs a route invocation.

Parameters:
  • request (Request) – The request produced.
  • code (int) – The response code of the route.
coroutine process_request(self, request, parent_context)

Processes a Request and returns a Response object.

This is the main processing method of Kyoukai, and is meant to be used by one of the HTTP server backends, and not by client code.

Parameters:
  • request (Request) – The werkzeug.wrappers.Request object to process. A new HTTPRequestContext will be provided to wrap this request inside of to client code.
  • parent_context (Context) – The asphalt.core.Context that is the parent context for this particular app. It will be used as the parent for the HTTPRequestContext.
Return type:

Response

Returns:

A werkzeug.wrappers.Response object that can be written to the client as a response.

register_blueprint(child)

Registers a child blueprint to this app’s root Blueprint.

This will set up the Blueprint tree, as well as setting up the routing table when finalized.

Parameters:child (Blueprint) – The child Blueprint to add. This must be an instance of Blueprint.
request_class

alias of Request

response_class

alias of Response

root
Return type:Blueprint
Returns:The root Blueprint for the routing tree.
run(ip='127.0.0.1', port=4444, *, component=None)

Runs the Kyoukai server from within your code.

This is not normally invoked - instead Asphalt should invoke the Kyoukai component. However, this is here for convenience.

coroutine start(self, ip='127.0.0.1', port=4444, *, component=None, base_context=None)

Runs the Kyoukai component asynchronously.

This will bypass Asphalt’s default runner, and allow you to run your app easily inside something else, for example.

Parameters:
  • ip (str) – The IP of the built-in server.
  • port (int) – The port of the built-in server.
  • component – The component to start the app with. This should be an instance of kyoukai.asphalt.KyoukaiComponent.
  • base_context (Optional[Context]) – The base context that the HTTPRequestContext should be started with.