def test_add_trailing_slash(db): create_route(db, "hello/", "hello") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/hello", allow_redirects=False) assert response.status_code == 301 assert response.headers["Location"] == "/hello/"
def test_content_length_correct_with_non_ascii_characters(db): create_route(db, "", "template", config="template.html") create_sqlar_file(db, "template.html", "café".encode("utf-8")) app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.headers["Content-Length"] == str(len(response.content))
def test_missing_template(db): create_route(db, "", "template", config="missingtemplate.html") create_sqlar_file(db, "template.html", b"<h1>hello</h1>") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 500
def test_redirect_handler(db): sql = "SELECT '/after/' || :slug || '/'" create_route(db, "before/<slug:slug>/", "redirect", config=sql) app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/before/hello/", allow_redirects=False) assert response.status_code == 301 assert response.headers["Location"] == "/after/hello/"
def test_not_found(db): create_route(db, "", "hello") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/notfound") assert response.status_code == 404 assert response.text == "Not Found" assert response.headers["Content-Type"] == "text/plain; charset=utf-8"
def test_hello_world(db): create_route(db, "", "hello") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 200 assert response.text == "Hello from SQLSite" assert response.headers["Content-Type"] == "text/plain; charset=utf-8" assert response.headers["Content-Length"] == "18"
def test_json_handler(db): sql = "WITH t(greeting) AS (VALUES('hello')) SELECT * FROM t" create_route(db, "", "json", config=sql) app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 200 assert response.json() == [{"greeting": "hello"}] assert response.headers["Content-Type"] == "application/json" assert response.headers["Content-Length"] == "23"
def test_template_handler(db): create_route(db, "<str:name>/", "template", config="template.html") create_sqlar_file(db, "template.html", b"<h1>hello {{ url.name }}</h1>") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/world/") assert response.status_code == 200 assert response.text == "<h1>hello world</h1>" assert response.headers["Content-Type"] == "text/html; charset=utf-8" assert response.headers["Content-Length"] == "20"
def test_static_handler(db): create_route(db, "<path:name>", "static", config="") create_sqlar_file(db, "hello.txt", b"hello") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/hello.txt") assert response.status_code == 200 assert response.content == b"hello" assert response.headers["Content-Type"] == "text/plain" assert response.headers["Content-Length"] == "5"
def test_markdown(db): create_route(db, "", "template", config="template.html") template = b"{{ '# hello markdown' | markdown }}" create_sqlar_file(db, "template.html", template) app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 200 assert response.text.strip() == "<h1>hello markdown</h1>" assert response.headers["Content-Type"] == "text/html; charset=utf-8" assert response.headers["Content-Length"] == "24"
def test_redirect_with_sql_in_file(db): sql = "SELECT '/after/' || :slug || '/'" create_sqlar_file(db, "query.sql", sql.encode()) create_route(db, "before/<slug:slug>/", "redirect", config="file=query.sql") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/before/hello/", allow_redirects=False) assert response.status_code == 301 assert response.headers["Location"] == "/after/hello/"
def test_template_with_query_and_params(db): create_route(db, "", "template", config="template.html") template = """ {% with name = sql("VALUES(:arg)", params={"arg": "arg"})[0][0] %} <h1>hello {{ name }}</h1> {% endwith %} """ create_sqlar_file(db, "template.html", dedent(template).encode()) app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 200 assert response.text.strip() == "<h1>hello arg</h1>" assert response.headers["Content-Type"] == "text/html; charset=utf-8" assert response.headers["Content-Length"] == "21"
def test_template_with_query_in_file(db): create_route(db, "", "template", config="template.html") template = """ {% with name = sql("file=query.sql")[0][0] %} <h1>hello {{ name }}</h1> {% endwith %} """ query = "VALUES('sql')" create_sqlar_file(db, "template.html", dedent(template).encode()) create_sqlar_file(db, "query.sql", query.encode()) app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 200 assert response.text.strip() == "<h1>hello sql</h1>" assert response.headers["Content-Type"] == "text/html" assert response.headers["Content-Length"] == "21"
def test_existsquery_request(db): create_route(db, "", "hello", existsquery="SELECT 0") app = make_app(db) client = httpx.Client(app=app) response = client.get("http://test/") assert response.status_code == 404
def test_post_put_patch_ignored(db): app = make_app(db) client = httpx.Client(app=app) for method in ["POST", "PUT", "PATCH"]: response = client.request(method, "http://test/") assert response.status_code == 405