예제 #1
0
def test_get_response_exception(mock_ssl_context, fake_handler_exception):
    app = App(urls={"": fake_handler_exception}, config=ZeroConfig())

    response = app.get_response("")
    assert isinstance(response, NotFoundResponse)

    response = app.get_response("/other")
    assert isinstance(response, NotFoundResponse)
예제 #2
0
def test_get_route_same_prefix(mock_ssl_context, fake_handler, fake_response):
    app = App(urls={
        "/test": fake_handler,
        "/test2": fake_response
    },
              config=ZeroConfig())
    assert app.get_route("/test") == ("/test", fake_handler)
    assert app.get_route("/test2") == ("/test2", fake_response)
예제 #3
0
def test_get_route_no_catchall(mock_ssl_context, fake_response):
    app = App(urls={"/hello": fake_response}, config=ZeroConfig())
    with pytest.raises(FileNotFoundError):
        app.get_route("")

    with pytest.raises(FileNotFoundError):
        app.get_route("/")

    with pytest.raises(FileNotFoundError):
        app.get_route("/something")
예제 #4
0
def test_get_response_handler(mock_ssl_context, fake_handler, fake_response):
    app = App(urls={
        "/handler": fake_handler,
        "/response": fake_response
    },
              config=ZeroConfig())

    # Just to make sure we're going through the get_route
    response = app.get_response("")
    assert isinstance(response, NotFoundResponse)
    assert response.reason == "Route Not Found"

    # Response from handler
    response = app.get_response("/handler")
    assert response.origin == "handler"

    # Direct response
    response = app.get_response("/response")
    assert response.origin == "direct"
예제 #5
0
def test_get_route_handler(mock_ssl_context, fake_handler, fake_response):
    app = App(urls={
        "": fake_handler,
        "/other": fake_response
    },
              config=ZeroConfig())
    # direct route
    assert app.get_route("") == ("", fake_handler)
    assert app.get_route("/") == ("", fake_handler)
    # Other
    assert app.get_route("/other") == ("/other", fake_response)
    # Catchall
    assert app.get_route("/something") == ("", fake_handler)
예제 #6
0
        "/direct": TextResponse(title="Direct Response", body="I am here"),
        "/template-response": TemplateResponse(
            template_file="examples/templates/template.txt",
            datetime="Not the dynamic datetime you were expecting",
        ),
        # Standard responses
        "/10": InputResponse(prompt="What's the ultimate answer?"),
        "/11": SensitiveInputResponse(prompt="What's the ultimate answer?"),
        "/30": RedirectResponse(target="/hello"),
        "/31": PermanentRedirectResponse(target="/hello"),
        # TODO: 40 TEMPORARY FAILURE
        # TODO: 41 SERVER UNAVAILABLE
        # TODO: 42 (?) CGI ERROR
        # TODO: 43 (?) PROXY ERROR
        # TODO: 44 SLOW DOWN
        # TODO: 50 PERMANENT FAILURE
        # TODO: 51 NOT FOUND (already covered by other response, but nice to have)
        "/51": NotFoundResponse("Nobody will escape the Area 51"),
        # TODO: 52 GONE
        "/53": ProxyRequestRefusedResponse(),
        "/59": BadRequestResponse(),
        # TODO: 60 (?) CLIENT CERTIFICATE REQUIRED
        # TODO: 61 (?) CERTIFICATE NOT AUTHORISED
        # TODO: 62 (?) CERTIFICATE NOT VALID
        # Configration errors. Uncomment to see how they're handled
        # "error": "I am an error",
        # "error": StaticHandler(static_dir="/tmp/not-a-directory"),
    }
    app = App(urls)
    app.run()
예제 #7
0
def test_no_urls():
    with pytest.raises(ImproperlyConfigured):
        App(urls=None, config=ZeroConfig())
예제 #8
0
def test_urls_response(mock_ssl_context, fake_response):
    app = App(urls={"": fake_response}, config=ZeroConfig())
    assert app
예제 #9
0
def test_urls_handler(mock_ssl_context, fake_handler):
    app = App(urls={"": fake_handler}, config=ZeroConfig())
    assert app
예제 #10
0
def test_not_a_handler_or_response_urls():
    with pytest.raises(ImproperlyConfigured):
        App(urls={"": "I am not a Handler/Response obj"}, config=ZeroConfig())
예제 #11
0
def test_empty_urls():
    with pytest.raises(ImproperlyConfigured):
        App(urls={}, config=ZeroConfig())
예제 #12
0
def test_not_a_dict_urls():
    with pytest.raises(ImproperlyConfigured):
        App(urls="", config=ZeroConfig())