Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from http.server import HTTPServer, BaseHTTPRequestHandler
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from urllib.parse import urlparse
import asyncio
import ngrok
import os
Expand Down Expand Up @@ -31,33 +32,52 @@ def retry_request():
return s


def tls_passthrough_request(url, timeout=10, get=requests.get, sleep=time.sleep):
"""GET a tls endpoint's URL over HTTPS; return the SSLError, or the response.
def get_when_online(url, timeout=10, get=requests.get, sleep=time.sleep):
"""GET a fresh endpoint's URL, retrying while ngrok's edge says it is offline.

A tls endpoint passes TLS through to the plain-HTTP test server, so the
handshake fails with an SSLError. Until a fresh endpoint is routable,
ngrok's edge answers it with its own 404 and ERR_NGROK_3200 (endpoint
offline); only that answer is retried, for up to timeout seconds. Each
GET is bounded by the time left, at least a second, so a stalled
handshake raises requests' Timeout instead of hanging the test.
get and sleep are replaceable so test_tls_passthrough.py can fake the edge.
Until a fresh endpoint is routable, ngrok's edge answers it with its own
404 and ERR_NGROK_3200 (endpoint offline), or, for an http URL, with a
redirect to https on the same host, where no endpoint answers an
http-scheme listener. Only those answers are retried, for up to timeout
seconds; redirects are not followed, and any other response is returned
at once. Each GET is bounded by the time left, at least a second, so a
stalled request raises requests' Timeout instead of hanging the test.
get and sleep are replaceable so tests can fake the edge.
"""
deadline = time.monotonic() + timeout
requested = urlparse(url)
while True:
try:
response = get(url, timeout=max(deadline - time.monotonic(), 1))
except requests.exceptions.SSLError as err:
return err
response = get(
url, timeout=max(deadline - time.monotonic(), 1), allow_redirects=False
)
location = urlparse(response.headers.get("location", ""))
offline = (
response.status_code == 404
and response.headers.get("ngrok-error-code") == "ERR_NGROK_3200"
) or (
response.is_redirect
and requested.scheme == "http"
and location.scheme == "https"
and location.netloc == requested.netloc
)
if not offline or time.monotonic() > deadline:
return response
# The edge gives no signal when the endpoint becomes routable.
sleep(0.5)


def tls_passthrough_request(url, timeout=10, get=requests.get, sleep=time.sleep):
"""GET a tls endpoint's URL over HTTPS; return the SSLError, or the response.

A tls endpoint passes TLS through to the plain-HTTP test server, so the
handshake fails with an SSLError, which ends get_when_online's retries.
"""
try:
return get_when_online(url, timeout, get, sleep)
except requests.exceptions.SSLError as err:
return err


class HelloHandler(BaseHTTPRequestHandler):
def do_GET(self):
body = bytes(expected, "utf-8")
Expand Down Expand Up @@ -159,7 +179,12 @@ async def test_http_listener(self):
http_server, session = await make_http_and_session()
listener = await session.http_endpoint().scheme("hTtP").listen()
self.assertTrue(listener.url().startswith("http://"))
await self.forward_validate_shutdown(http_server, listener, listener.url())
listener.forward(http_server.listen_to)
# retry_request would follow the redirect a fresh http-scheme endpoint
# gets, and retry the https URL it names, where nothing answers.
get_when_online(listener.url())
await self.validate_http_request(listener.url())
await shutdown(listener, http_server)

async def test_https_listener_with_policy(self):
policy = """
Expand Down Expand Up @@ -439,7 +464,7 @@ def handle(self):
tcp_server.listen_to = "{}:{}".format(addr[0], addr[1])
listener.forward(tcp_server.listen_to)
try:
resp = requests.get(listener.url(), timeout=3)
get_when_online(listener.url())
except requests.exceptions.ReadTimeout:
pass

Expand Down
42 changes: 39 additions & 3 deletions test/test_tls_passthrough.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
import socket
import threading
import unittest
from test import tls_passthrough_request
from test import get_when_online, tls_passthrough_request

OFFLINE = "ERR_NGROK_3200"


def response(status_code, ngrok_error_code=None):
def response(status_code, ngrok_error_code=None, location=None):
r = requests.Response()
r.status_code = status_code
if ngrok_error_code:
r.headers["ngrok-error-code"] = ngrok_error_code
if location:
r.headers["location"] = location
return r


Expand All @@ -20,17 +22,21 @@ class FakeEdge:

Answers each GET with the next step: a requests.Response, or "handshake"
to raise the SSLError that TLS passthrough to a plain-HTTP server gives.
A redirect is followed unless allow_redirects is False, as requests.get
does, to the 404 that the https URL of an http-scheme endpoint answers.
"""

def __init__(self, *steps):
self.steps = list(steps)
self.requests = 0

def get(self, url, timeout):
def get(self, url, timeout, allow_redirects=True):
self.requests += 1
step = self.steps.pop(0)
if step == "handshake":
raise requests.exceptions.SSLError("wrong version number")
if allow_redirects and step.is_redirect:
return response(404)
return step


Expand Down Expand Up @@ -72,6 +78,36 @@ def test_offline_past_the_deadline_returns_the_404(self):
self.assertEqual(1, edge.requests)


class TestGetWhenOnline(unittest.TestCase):
url = "http://example.ngrok.app"

def get(self, edge):
return get_when_online(self.url, get=edge.get, sleep=no_sleep)

# Until an http-scheme endpoint is routable, the edge redirects its http
# URL to https on the same host, where no endpoint will ever answer.
def test_a_redirect_to_https_on_the_same_host_is_retried(self):
edge = FakeEdge(
response(307, location="https://example.ngrok.app/"),
response(307, location="https://example.ngrok.app/"),
response(200),
)
self.assertEqual(200, self.get(edge).status_code)
self.assertEqual(3, edge.requests)

def test_a_redirect_elsewhere_is_returned(self):
edge = FakeEdge(response(307, location="https://elsewhere.example/"))
self.assertEqual(307, self.get(edge).status_code)
self.assertEqual(1, edge.requests)

# A proxy-proto endpoint answers 503 ERR_NGROK_3004 once a request
# reaches the test's handler, which writes no HTTP response.
def test_offline_answers_are_retried_until_one_reaches_the_server(self):
edge = FakeEdge(response(404, OFFLINE), response(503, "ERR_NGROK_3004"))
self.assertEqual(503, self.get(edge).status_code)
self.assertEqual(2, edge.requests)


class TestTlsPassthroughRequestStall(unittest.TestCase):
def test_a_stalled_handshake_times_out_instead_of_hanging(self):
# A local server that accepts and never answers stands in for an edge
Expand Down
Loading