Ejemplo n.º 1
0
    def test_allow_origins_request_handling(self, hug_api):
        """Test to ensure a route with allowed origins works as expected"""
        route = URLRouter(api=hug_api)
        test_headers = route.allow_origins("google.com",
                                           methods=("GET", "POST"),
                                           credentials=True,
                                           headers="OPTIONS",
                                           max_age=10)

        @test_headers.get()
        def my_endpoint():
            return "Success"

        assert hug.test.get(hug_api,
                            "/my_endpoint",
                            headers={
                                'ORIGIN': 'google.com'
                            }).data == "Success"
Ejemplo n.º 2
0
 def test_route_http(self):
     """Test to ensure you can dynamically create an HTTP route attached to a hug API"""
     assert self.router.http('/hi/').route == URLRouter('/hi/',
                                                        api=api).route
Ejemplo n.º 3
0
 def test_route_url(self):
     """Test to ensure you can dynamically create a URL route attached to a hug API"""
     assert self.router.urls('/hi/').route == URLRouter('/hi/',
                                                        api=api).route
Ejemplo n.º 4
0
class TestURLRouter(TestHTTPRouter):
    """Collection of tests to ensure the URLRouter object works as expected"""
    route = URLRouter('/here',
                      transform='transform',
                      output='output',
                      requires=('love', ))

    def test_urls(self):
        """Test to ensure the url routes can be replaced on the fly"""
        assert self.route.urls('/there').route['urls'] == ('/there', )

    def test_accept(self):
        """Test to ensure the accept HTTP METHODs can be replaced on the fly"""
        assert self.route.accept('GET').route['accept'] == ('GET', )

    def test_get(self):
        """Test to ensure the HTTP METHOD can be set to just GET on the fly"""
        assert self.route.get().route['accept'] == ('GET', )
        assert self.route.get('/url').route['urls'] == ('/url', )

    def test_delete(self):
        """Test to ensure the HTTP METHOD can be set to just DELETE on the fly"""
        assert self.route.delete().route['accept'] == ('DELETE', )
        assert self.route.delete('/url').route['urls'] == ('/url', )

    def test_post(self):
        """Test to ensure the HTTP METHOD can be set to just POST on the fly"""
        assert self.route.post().route['accept'] == ('POST', )
        assert self.route.post('/url').route['urls'] == ('/url', )

    def test_put(self):
        """Test to ensure the HTTP METHOD can be set to just PUT on the fly"""
        assert self.route.put().route['accept'] == ('PUT', )
        assert self.route.put('/url').route['urls'] == ('/url', )

    def test_trace(self):
        """Test to ensure the HTTP METHOD can be set to just TRACE on the fly"""
        assert self.route.trace().route['accept'] == ('TRACE', )
        assert self.route.trace('/url').route['urls'] == ('/url', )

    def test_patch(self):
        """Test to ensure the HTTP METHOD can be set to just PATCH on the fly"""
        assert self.route.patch().route['accept'] == ('PATCH', )
        assert self.route.patch('/url').route['urls'] == ('/url', )

    def test_options(self):
        """Test to ensure the HTTP METHOD can be set to just OPTIONS on the fly"""
        assert self.route.options().route['accept'] == ('OPTIONS', )
        assert self.route.options('/url').route['urls'] == ('/url', )

    def test_head(self):
        """Test to ensure the HTTP METHOD can be set to just HEAD on the fly"""
        assert self.route.head().route['accept'] == ('HEAD', )
        assert self.route.head('/url').route['urls'] == ('/url', )

    def test_connect(self):
        """Test to ensure the HTTP METHOD can be set to just CONNECT on the fly"""
        assert self.route.connect().route['accept'] == ('CONNECT', )
        assert self.route.connect('/url').route['urls'] == ('/url', )

    def test_call(self):
        """Test to ensure the HTTP METHOD can be set to accept all on the fly"""
        assert self.route.call().route['accept'] == hug.HTTP_METHODS

    def test_http(self):
        """Test to ensure the HTTP METHOD can be set to accept all on the fly"""
        assert self.route.http().route['accept'] == hug.HTTP_METHODS

    def test_get_post(self):
        """Test to ensure the HTTP METHOD can be set to GET & POST in one call"""
        return self.route.get_post().route['accept'] == ('GET', 'POST')

    def test_put_post(self):
        """Test to ensure the HTTP METHOD can be set to PUT & POST in one call"""
        return self.route.put_post().route['accept'] == ('PUT', 'POST')

    def test_examples(self):
        """Test to ensure examples can be modified on the fly"""
        assert self.route.examples('none').route['examples'] == ('none', )

    def test_prefixes(self):
        """Test to ensure adding prefixes works as expected"""
        assert self.route.prefixes('/js/').route['prefixes'] == ('/js/', )

    def test_suffixes(self):
        """Test to ensure setting suffixes works as expected"""
        assert self.route.suffixes('.js',
                                   '.xml').route['suffixes'] == ('.js', '.xml')
Ejemplo n.º 5
0
class TestURLRouter(TestHTTPRouter):
    """Collection of tests to ensure the URLRouter object works as expected"""

    route = URLRouter("/here", transform="transform", output="output", requires=("love",))

    def test_urls(self):
        """Test to ensure the url routes can be replaced on the fly"""
        assert self.route.urls("/there").route["urls"] == ("/there",)

    def test_accept(self):
        """Test to ensure the accept HTTP METHODs can be replaced on the fly"""
        assert self.route.accept("GET").route["accept"] == ("GET",)

    def test_get(self):
        """Test to ensure the HTTP METHOD can be set to just GET on the fly"""
        assert self.route.get().route["accept"] == ("GET",)
        assert self.route.get("/url").route["urls"] == ("/url",)

    def test_delete(self):
        """Test to ensure the HTTP METHOD can be set to just DELETE on the fly"""
        assert self.route.delete().route["accept"] == ("DELETE",)
        assert self.route.delete("/url").route["urls"] == ("/url",)

    def test_post(self):
        """Test to ensure the HTTP METHOD can be set to just POST on the fly"""
        assert self.route.post().route["accept"] == ("POST",)
        assert self.route.post("/url").route["urls"] == ("/url",)

    def test_put(self):
        """Test to ensure the HTTP METHOD can be set to just PUT on the fly"""
        assert self.route.put().route["accept"] == ("PUT",)
        assert self.route.put("/url").route["urls"] == ("/url",)

    def test_trace(self):
        """Test to ensure the HTTP METHOD can be set to just TRACE on the fly"""
        assert self.route.trace().route["accept"] == ("TRACE",)
        assert self.route.trace("/url").route["urls"] == ("/url",)

    def test_patch(self):
        """Test to ensure the HTTP METHOD can be set to just PATCH on the fly"""
        assert self.route.patch().route["accept"] == ("PATCH",)
        assert self.route.patch("/url").route["urls"] == ("/url",)

    def test_options(self):
        """Test to ensure the HTTP METHOD can be set to just OPTIONS on the fly"""
        assert self.route.options().route["accept"] == ("OPTIONS",)
        assert self.route.options("/url").route["urls"] == ("/url",)

    def test_head(self):
        """Test to ensure the HTTP METHOD can be set to just HEAD on the fly"""
        assert self.route.head().route["accept"] == ("HEAD",)
        assert self.route.head("/url").route["urls"] == ("/url",)

    def test_connect(self):
        """Test to ensure the HTTP METHOD can be set to just CONNECT on the fly"""
        assert self.route.connect().route["accept"] == ("CONNECT",)
        assert self.route.connect("/url").route["urls"] == ("/url",)

    def test_call(self):
        """Test to ensure the HTTP METHOD can be set to accept all on the fly"""
        assert self.route.call().route["accept"] == hug.HTTP_METHODS

    def test_http(self):
        """Test to ensure the HTTP METHOD can be set to accept all on the fly"""
        assert self.route.http().route["accept"] == hug.HTTP_METHODS

    def test_get_post(self):
        """Test to ensure the HTTP METHOD can be set to GET & POST in one call"""
        return self.route.get_post().route["accept"] == ("GET", "POST")

    def test_put_post(self):
        """Test to ensure the HTTP METHOD can be set to PUT & POST in one call"""
        return self.route.put_post().route["accept"] == ("PUT", "POST")

    def test_examples(self):
        """Test to ensure examples can be modified on the fly"""
        assert self.route.examples("none").route["examples"] == ("none",)

    def test_prefixes(self):
        """Test to ensure adding prefixes works as expected"""
        assert self.route.prefixes("/js/").route["prefixes"] == ("/js/",)

    def test_suffixes(self):
        """Test to ensure setting suffixes works as expected"""
        assert self.route.suffixes(".js", ".xml").route["suffixes"] == (".js", ".xml")
Ejemplo n.º 6
0
class TestURLRouter(TestHTTPRouter):
    """Collection of tests to ensure the URLRouter object works as expected"""

    route = URLRouter("/here",
                      transform="transform",
                      output="output",
                      requires=("love", ))

    def test_urls(self):
        """Test to ensure the url routes can be replaced on the fly"""
        assert self.route.urls("/there").route["urls"] == ("/there", )

    def test_accept(self):
        """Test to ensure the accept HTTP METHODs can be replaced on the fly"""
        assert self.route.accept("GET").route["accept"] == ("GET", )

    def test_get(self):
        """Test to ensure the HTTP METHOD can be set to just GET on the fly"""
        assert self.route.get().route["accept"] == ("GET", )
        assert self.route.get("/url").route["urls"] == ("/url", )

    def test_delete(self):
        """Test to ensure the HTTP METHOD can be set to just DELETE on the fly"""
        assert self.route.delete().route["accept"] == ("DELETE", )
        assert self.route.delete("/url").route["urls"] == ("/url", )

    def test_post(self):
        """Test to ensure the HTTP METHOD can be set to just POST on the fly"""
        assert self.route.post().route["accept"] == ("POST", )
        assert self.route.post("/url").route["urls"] == ("/url", )

    def test_put(self):
        """Test to ensure the HTTP METHOD can be set to just PUT on the fly"""
        assert self.route.put().route["accept"] == ("PUT", )
        assert self.route.put("/url").route["urls"] == ("/url", )

    def test_trace(self):
        """Test to ensure the HTTP METHOD can be set to just TRACE on the fly"""
        assert self.route.trace().route["accept"] == ("TRACE", )
        assert self.route.trace("/url").route["urls"] == ("/url", )

    def test_patch(self):
        """Test to ensure the HTTP METHOD can be set to just PATCH on the fly"""
        assert self.route.patch().route["accept"] == ("PATCH", )
        assert self.route.patch("/url").route["urls"] == ("/url", )

    def test_options(self):
        """Test to ensure the HTTP METHOD can be set to just OPTIONS on the fly"""
        assert self.route.options().route["accept"] == ("OPTIONS", )
        assert self.route.options("/url").route["urls"] == ("/url", )

    def test_head(self):
        """Test to ensure the HTTP METHOD can be set to just HEAD on the fly"""
        assert self.route.head().route["accept"] == ("HEAD", )
        assert self.route.head("/url").route["urls"] == ("/url", )

    def test_connect(self):
        """Test to ensure the HTTP METHOD can be set to just CONNECT on the fly"""
        assert self.route.connect().route["accept"] == ("CONNECT", )
        assert self.route.connect("/url").route["urls"] == ("/url", )

    def test_call(self):
        """Test to ensure the HTTP METHOD can be set to accept all on the fly"""
        assert self.route.call().route["accept"] == hug.HTTP_METHODS

    def test_http(self):
        """Test to ensure the HTTP METHOD can be set to accept all on the fly"""
        assert self.route.http().route["accept"] == hug.HTTP_METHODS

    def test_get_post(self):
        """Test to ensure the HTTP METHOD can be set to GET & POST in one call"""
        return self.route.get_post().route["accept"] == ("GET", "POST")

    def test_put_post(self):
        """Test to ensure the HTTP METHOD can be set to PUT & POST in one call"""
        return self.route.put_post().route["accept"] == ("PUT", "POST")

    def test_examples(self):
        """Test to ensure examples can be modified on the fly"""
        assert self.route.examples("none").route["examples"] == ("none", )

    def test_prefixes(self):
        """Test to ensure adding prefixes works as expected"""
        assert self.route.prefixes("/js/").route["prefixes"] == ("/js/", )

    def test_suffixes(self):
        """Test to ensure setting suffixes works as expected"""
        assert self.route.suffixes(".js",
                                   ".xml").route["suffixes"] == (".js", ".xml")

    def test_allow_origins_request_handling(self, hug_api):
        """Test to ensure a route with allowed origins works as expected"""
        route = URLRouter(api=hug_api)
        test_headers = route.allow_origins("google.com",
                                           methods=("GET", "POST"),
                                           credentials=True,
                                           headers="OPTIONS",
                                           max_age=10)

        @test_headers.get()
        def my_endpoint():
            return "Success"

        assert hug.test.get(hug_api,
                            "/my_endpoint",
                            headers={
                                'ORIGIN': 'google.com'
                            }).data == "Success"