Skip to content

Instantly share code, notes, and snippets.

@alexdlaird
Last active September 18, 2023 19:19
Show Gist options
  • Save alexdlaird/58473839f13ea63c59eba2a0e1d9fa49 to your computer and use it in GitHub Desktop.
Save alexdlaird/58473839f13ea63c59eba2a0e1d9fa49 to your computer and use it in GitHub Desktop.
End-to-end testing example for pyngrok, full documentation found at https://pyngrok.readthedocs.io/en/latest/integrations.html
import unittest
import threading
from flask import request
from pyngrok import ngrok
from urllib import request
from server import create_app
class PyngrokTestCase(unittest.TestCase):
# Default Flask port
PORT = "5000"
@classmethod
def start_dev_server(cls):
app = create_app()
def shutdown():
request.environ.get("werkzeug.server.shutdown")()
@app.route("/shutdown", methods=("POST",))
def route_shutdown():
shutdown()
return "", 204
threading.Thread(target=app.run).start()
@classmethod
def stop_dev_server(cls):
req = request.Request("http://localhost:5000/shutdown", method="POST")
request.urlopen(req)
@classmethod
def init_webhooks(cls, base_url):
webhook_url = "{}/foo".format(base_url)
# ... Update inbound traffic via APIs to use the public-facing ngrok URL
@classmethod
def init_pyngrok(cls):
# Open a ngrok tunnel to the dev server
public_url = ngrok.connect(PORT)
# Update any base URLs or webhooks to use the public ngrok URL
cls.init_webhooks(public_url)
@classmethod
def setUpClass(cls):
cls.start_dev_server()
cls.init_pyngrok()
@classmethod
def tearDownClass(cls):
cls.stop_dev_server()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment