Skip to main content

Build status License: MIT

sawpit

Work in progress fork/continuation of pywsitest

A python API to assist with automated websocket integration testing

Installation

// TODO

Package contents

WSTest

WSTest is the main test running class in sawpit. It currently has the following methods:

  • with_parameter: add a query parameter to the connection
  • with_header: add a header to the connection
  • with_response: add an expected response to the test runner
  • with_message: add a message for the test runner to send on connection
  • with_request: attach a rest api request to the instance of this class
  • with_response_timeout: set the timeout in seconds for the test runner to wait for a response from the websocket
  • with_message_timeout: set the timeout in seconds for the test runner to wait while trying to send a message to the websocket
  • with_request_timeout: set the timeout in seconds for the rest request attached to the instance of this class
  • with_test_timeout: set the timeout in seconds for the test runner to run for
  • with_received_response_logging: enable logging of received responses on response timeout error
  • run: asyncronously run the test runner, sending all messages and listening for responses
  • is_complete: check whether all expected responses have been received and messages have been sent

WSResponse

WSResponse is a class to represent an expected response from the websocket

  • with_attribute: add an attribute to check an incoming response against
  • with_trigger: add a message to trigger when a response matching this instance has been received
  • is_match: check whether a received response matches the attributes of this instance

WSMessage

WSMessage is a class to represent a message to send to the websocket

  • with_attribute: add an attribute to the message to be sent to the websocket host
  • with_delay: add a delay to the message to be sent to the websocket host

RestRequest

RestRequest is a class to represent a request to send to rest api

  • with_header: add a header to the request to be sent to the rest api
  • with_body: add a body to the request to be sent to the rest api
  • with_delay: add a delay to the request to be sent to the rest api

Examples

Response testing

Testing a response with a body is received on connection to a websocket host:

from sawpit import WSTest, WSResponse

ws_test = (
    WSTest("wss://example.com")
    .with_response(
        WSResponse()
        .with_attribute("body")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Testing that a response with the following more complicated body is received on connection to a websocket host:

{
    "body": {
        "attribute": "value"
    }
}
from sawpit import WSTest, WSResponse

ws_test = (
    WSTest("wss://example.com")
    .with_response(
        WSResponse()
        .with_attribute("body")
        .with_attribute("body/attribute", "value")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Testing that a response with the following body with a list is received on connection to a websocket host:

{
    "body": [
        {"colour": "red"},
        {"colour": "green"},
        {"colour": "blue"}
    ]
}
from sawpit import WSTest, WSResponse

ws_test = (
    WSTest("wss://example.com")
    .with_response(
        WSResponse()
        .with_attribute("body/0/colour", "red")
        .with_attribute("body/1/colour", "green")
        .with_attribute("body/2/colour", "blue")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Testing that a response with the following body with a list containing the colour green somewhere is received on connection to a websocket host:

{
    "body": [
        {"colour": "red"},
        {"colour": "green"},
        {"colour": "blue"}
    ]
}
from sawpit import WSTest, WSResponse

ws_test = (
    WSTest("wss://example.com")
    .with_response(
        WSResponse()
        .with_attribute("body//colour", "green")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Message sending

Sending a message on connection to a websocket host:

from sawpit import WSTest, WSMessage

ws_test = (
    WSTest("wss://example.com")
    .with_message(
        WSMessage()
        .with_attribute("body", "Hello, world!")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Triggering a message to be sent with extracted data when the following response is received:

{
    "body": {
        "message": "Hello, world!"
    }
}
from sawpit import WSTest, WSResponse, WSMessage

ws_test = (
    WSTest("wss://example.com")
    .with_response(
        WSResponse()
        .with_attribute("body/message")
        .with_trigger(
            WSMessage()
            .with_attribute("body", "${body/message}")
        )
    )
)

await ws_test.run()

assert ws_test.is_complete()

Triggering a message to be sent with the first colour extracted from a list when the following response is received:

{
    "body": [
        {"colour": "red"},
        {"colour": "green"},
        {"colour": "blue"}
    ]
}
from sawpit import WSTest, WSResponse, WSMessage

ws_test = (
    WSTest("wss://example.com")
    .with_response(
        WSResponse()
        .with_attribute("body/0/colour")
        .with_trigger(
            WSMessage()
            .with_attribute("body", "${body/0/colour}")
        )
    )
)

await ws_test.run()

assert ws_test.is_complete()

Using rest requests

Attaching simple rest get request and sending it:

rest_request = (
    RestRequest("https://example.com", "GET")
    .with_body({"some_key": some_value})
)

ws_test = (
    WSTest("wss://example.com")
    .with_request(rest_request)
)

await ws_test.run()

for response in ws_tester.received_request_responses:
    print(response.status_code)
    print(response.json())

assert ws_test.is_complete()

Error handling

Force a test to fail is execution takes more than 30 seconds (default 60 seconds)

ws_test = (
    WSTest("wss://example.com")
    .with_test_timeout(30)
    .with_response(
        WSResponse()
        .with_attribute("body")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Force a test to fail if no response is received for 15 seconds (default 10 seconds)

  • Any responses that haven't been sent will be output along with the WSTimeoutError
  • Received responses can be output too by calling with_received_response_logging on the WSTest instance
ws_test = (
    WSTest("wss://example.com")
    .with_response_timeout(15)
    .with_received_response_logging()
    .with_response(
        WSResponse()
        .with_attribute("body")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Force a test to fail is a message takes longer than 15 seconds to send (default 10 seconds)

  • The message that the test runner failed to send will be output along with the WSTimeoutError
ws_test = (
    WSTest("wss://example.com")
    .with_message_timeout(15)
    .with_message(
        WSMessage()
        .with_attribute("body", "Hello, world!")
    )
)

await ws_test.run()

assert ws_test.is_complete()

Documentation

Users can get the docstring help by running:

from sawpit import WSTest
help(WSTest.with_response)
  • Github
  • PyPI // TODO
  • Test PyPI // TODO

Metadata

Release files for sawpit 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sawpit 0.0.1
File Size Uploaded Provenance Actions
sawpit-0.0.1.tar.gz 10.4 kB Without provenance Details for sawpit-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sawpit 0.0.1
File Interpreter ABI Platform Actions
sawpit-0.0.1-py3-none-any.whl Python 3 none any Details for sawpit-0.0.1-py3-none-any.whl

Total release size: 21.4 kB

Release files / sawpit-0.0.1.tar.gz

Download URL sawpit-0.0.1.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
246b7ffc9d3f4f773f3af6327da5f3f0143b09188437efc86b9462a395281d89
BLAKE2b-256 checksum
How to use checksums
76973ad43a0711d7565f11dba884ec35494063b4c6d79ccb03ccff0796b72355
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.0

Release files / sawpit-0.0.1-py3-none-any.whl

Download URL sawpit-0.0.1-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b98aa3a76e264e3315424868d2e6c3e9cd2b2feced7a49dfb78516ae5da97ef
BLAKE2b-256 checksum
How to use checksums
9476c0e2a48c4a073a13baa415511c9abdd622c50251170ac47bdd47bf65ed23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.0

Security for sawpit 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page