def test_build_forward_url_with_path(self):
        forwarder = UrlMatchingForwarder("/", "/p")

        def geturl(host, path):
            return forwarder.build_forward_url(host, path).geturl()

        assert geturl("", "/") == "/p"
        assert geturl("", "/fo") == "/p/fo"
        assert geturl("", "/foo") == "/p/foo"
        assert geturl("", "/foo/") == "/p/foo/"
        assert geturl("", "/foo/bar") == "/p/foo/bar"
        assert geturl("", "/fooo") == "/p/fooo"
        assert geturl("example.com", "/") == "example.com/p"
        assert geturl("example.com", "/fo") == "example.com/p/fo"
        assert geturl("example.com", "/foo") == "example.com/p/foo"
        assert geturl("example.com", "/foo/") == "example.com/p/foo/"
        assert geturl("example.com", "/foo/bar") == "example.com/p/foo/bar"
        assert geturl("example.com", "/fooo") == "example.com/p/fooo"
        assert geturl("localhost", "/") == "localhost/p"
        assert geturl("localhost", "/fo") == "localhost/p/fo"
        assert geturl("localhost", "/foo") == "localhost/p/foo"
        assert geturl("localhost", "/foo/") == "localhost/p/foo/"
        assert geturl("localhost", "/foo/bar") == "localhost/p/foo/bar"
        assert geturl("localhost", "/fooo") == "localhost/p/fooo"
        assert geturl("localhost:8080", "/") == "localhost:8080/p"
        assert geturl("localhost:8080", "/foo") == "localhost:8080/p/foo"
        assert geturl("localhost:8080", "/foo/bar") == "localhost:8080/p/foo/bar"
    def test_forward_request(self, httpserver):
        httpserver.expect_request("/baz", method="GET").respond_with_data("baz")
        forwarder = UrlMatchingForwarder("/foo", httpserver.url_for("/baz"))

        # not matching
        response = forwarder.forward_request("GET", "/fooo", "", {})
        assert response is True

        response = forwarder.forward_request("GET", "/foo", "", {})
        assert response is not True
        assert response.text == "baz"

        httpserver.expect_request("/baz/bar", method="GET").respond_with_data("baz/bar")
        response = forwarder.forward_request("GET", "/foo/bar", "", {})
        assert response is not True
        assert response.text == "baz/bar"

        httpserver.check()
    def test_build_forward_url_with_host_and_subpath(self):
        forwarder = UrlMatchingForwarder("/foo", "http://localhost:1234/oof")

        def geturl(host, path):
            return forwarder.build_forward_url(host, path).geturl()

        assert geturl("", "/foo") == "http://localhost:1234/oof"
        assert geturl("", "/foo/") == "http://localhost:1234/oof/"
        assert geturl("", "/foo/bar") == "http://localhost:1234/oof/bar"
        assert geturl("", "/foo/bar/") == "http://localhost:1234/oof/bar/"
    def test_build_forward_url_with_host_and_trailing_path(self):
        forwarder = UrlMatchingForwarder("/", "http://localhost/p/")

        def geturl(host, path):
            return forwarder.build_forward_url(host, path).geturl()

        assert geturl("", "/") == "http://localhost/p/"
        assert geturl("", "/fo") == "http://localhost/p/fo"
        assert geturl("localhost", "/") == "http://localhost/p/"
        assert geturl("localhost", "/fo") == "http://localhost/p/fo"
    def test_matches_with_path_with_host(self):
        forwarder = UrlMatchingForwarder("http://example.com/foo", "http://localhost")

        assert not forwarder.matches("", "")
        assert not forwarder.matches("", "/")
        assert not forwarder.matches("", "/fo")
        assert not forwarder.matches("", "/foo")
        assert not forwarder.matches("", "/foo/")
        assert not forwarder.matches("", "/foo/bar")
        assert not forwarder.matches("", "/fooo")
        assert not forwarder.matches("example.com", "")
        assert not forwarder.matches("example.com", "/")
        assert not forwarder.matches("example.com", "/fo")
        assert forwarder.matches("example.com", "/foo")
        assert forwarder.matches("example.com", "/foo/")
        assert forwarder.matches("example.com", "/foo/bar")
        assert not forwarder.matches("example.com", "/fooo")
        assert not forwarder.matches("localhost", "")
        assert not forwarder.matches("localhost", "/")
        assert not forwarder.matches("localhost", "/fo")
        assert not forwarder.matches("localhost", "/foo")
        assert not forwarder.matches("localhost", "/foo/")
        assert not forwarder.matches("localhost", "/foo/bar")
        assert not forwarder.matches("localhost", "/fooo")
        assert not forwarder.matches("localhost:8080", "/")
        assert not forwarder.matches("localhost:8080", "")
        assert not forwarder.matches("localhost:8080", "/foo")
        assert not forwarder.matches("localhost:8080", "/foo/bar")