Skip to content

Instantly share code, notes, and snippets.

@FND
Last active April 7, 2024 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FND/ac2842a94991630537b29d133145cd9b to your computer and use it in GitHub Desktop.
Save FND/ac2842a94991630537b29d133145cd9b to your computer and use it in GitHub Desktop.
gabbi in the browser

gabbi in the browser

  1. start a CORS-enabled HTTP server at http://localhost:8000 (see below)
  2. visit Pyodide REPL
  3. paste setup.py contents into the REPL
  4. paste test_case.py contents into the REPL

HTTP Server

You might use nc -l 8000 and manually respond to the incoming request:

HTTP/1.1 200 OK
Content-Type: text/plain
Access-Control-Allow-Origin: *

hello world
import micropip
import sys
from http import client
def create_module(name):
module_cls = type(sys) # cf. https://github.com/brettlangdon/virtualmod
mod = module_cls(name)
sys.modules[name] = mod
return mod
class HTTPSConnection(client.HTTPConnection):
pass
# mock `HTTPSConnection` because OpenSSL functionality is unavailable in Pyodide
setattr(client, "HTTPSConnection", HTTPSConnection)
# mock `jsonpath-rw-ext` because there's no wheel distribution for `jsonpath-rw`
jsonpath = create_module("jsonpath_rw_ext")
parser = create_module("jsonpath_rw_ext.parser")
setattr(parser, "ExtentedJsonPathParser",
lambda *args: print("mocking JSONPath parser"))
# mock `colorama` to suppress escape sequences
colorama = create_module("colorama")
setattr(colorama, "init",
lambda *args: print("mocking colorama"))
# manually install dependencies due to the above
await micropip.install("gabbi", deps=False)
await micropip.install("pytest")
await micropip.install("pyyaml")
await micropip.install("wsgi_intercept")
await micropip.install("certifi")
await micropip.install("urllib3")
print(micropip.list())
import sys
from gabbi import runner
from io import StringIO
sys.argv = ["gabbi-run"]
sys.stdin = StringIO("""
tests:
- name: hello world
method: GET
url: http://localhost:8000/
status: 200
response_strings:
- lorem ipsum
""")
runner.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment