Exemplo n.º 1
0
    def testCrossDomain(self):
        """test routing and application coupling"""

        @Path("/products")
        class Product(Resource):
            @POST
            @Route("/hello/{name}")
            @CrossDomain(origin=["*"])
            def hello(self, name):
                """A route that writes something to the header"""
                self.response.status = "200 OK"
                self.response.type = "text/plain"
                self.response.write(name)

        application = Application(base="/base", resources=[Product])
        application.secret = "SHERLOCK"
        application.debug = True

        request = Request.blank("http://localhost:80/base/products/hello/iroiso")
        request.method = "POST"
        response = request.execute(application, False)
        self.assertTrue("text/plain" in response.type)
        self.assertEquals(response.body, "iroiso")

        request = Request.blank("http://localhost:80/base/products/hello/iroiso")
        request.method = "OPTIONS"
        response = request.execute(application, False)
        self.assertEquals(response.body, "")
        self.assertEquals(response.headers["Access-Control-Allow-Origin"], "*")
        print response.headers["Access-Control-Allow-Methods"]
        self.assertEquals(response.headers["Access-Control-Allow-Methods"], "HEAD, OPTIONS, POST")
Exemplo n.º 2
0
 def testCookies(self):
     '''Tests cookies and query parameters with gates'''
     app = Application("/", [Referrer,])
     app.secret = "SHERLOCK HOLMES"
     req = Request.blank("http://localhost:80/l/hello.html?a=b")
     resp = req.execute(app)
     found = []
     for header in resp.headers.getall("Set-Cookie"):
         result = 'a=b' in header
         found.append(result)
     self.assertTrue(True in found)
     self.assertTrue(resp.body == 'hello')
Exemplo n.º 3
0
    def testSubdomains(self):
        '''Shows that URLs with subdomains work'''
        @Path("/products", subdomain="shop")
        class Product(Resource):
        
            @GET
            @Route("/{product}/{colour}")
            def show(self, product, colour):
                '''Just display some text'''
                self.response.status = "200 OK"
                self.response.type = "text/plain"
                self.response.charset = "UTF-8"
                self.response.write("Showing product: %s with colour: %s" % (product, colour))

        application = Application(base="/", resources=[Product,])
        application.secret = "SHERLOCK"
        request = Request.blank("http://shop.rafiki.me/products/BMW/black")
        response = request.execute(application, False)
        print response.body
Exemplo n.º 4
0
    def testRedirection(self):
        '''Shows that Redirection works within Gates.'''
        # A Path decorator with a path in the route.
        @Path("/{user}/filters")
        class RecommendedFilter(Resource):
            '''Sample filter...'''

            @GET
            @Route("/recommended")
            def handle(self, user):
                '''Just writes a Hello to the user'''
                self.redirect("http://plus.google.com/%s" % user)
                
        application = Application(base="/", resources=[RecommendedFilter,])
        application.secret = "SHERLOCK"
        request = Request.blank("http://localhost:80/iroiso/filters/recommended")
        response = request.execute(application, True)
        assert isinstance(response, Response)
        self.assertEquals(response.location , "http://plus.google.com/iroiso")
Exemplo n.º 5
0
 def testCrossDomainWithPUT(self):
     '''test routing and application coupling'''
     @Path("/products")
     class Product(Resource):
     
         
         @PUT
         @Route("/hello/{name}")
         @CrossDomain(origin=["*"])
         def hello(self, name):
             '''A route that writes something to the header'''
             self.response.status = "200 OK"
             self.response.type = "text/plain"
             self.response.write(name)
             
     application = Application(base="/base", resources=[Product,])
     application.secret = "SHERLOCK"
     application.debug = True
     
     print "Simulating a pre-flight request"
     request = Request.blank(
         "http://localhost:80/base/products/hello/iroiso",
         headers=[
             ('Access-Control-Request-Method', "PUT")
         ]
     )
     request.method = "OPTIONS"
     response = request.execute(application, False)
     self.assertEquals(response.body , "") 
     self.assertEquals(response.headers["Access-Control-Allow-Origin"], "*")
     print response.headers["Access-Control-Allow-Methods"]
     self.assertEquals(response.headers["Access-Control-Allow-Methods"], 'HEAD, OPTIONS, PUT')
     
     print "Testing that we can still invoke the action"
     request = Request.blank("http://localhost:80/base/products/hello/iroiso")
     request.method = "PUT"
     response = request.execute(application, False)
     self.assertTrue("text/plain" in response.type)
     self.assertEquals(response.body , "iroiso") 
     
     
Exemplo n.º 6
0
    def testRule(self):
        """Tests the redirect function"""
        resource = redirect("/download", "http://a16z.com/resources")
        app = Application("/", [resource])
        app.secret = "SHERLOCK"
        request = Request.blank("http://localhost:80/download")
        response = request.execute(app)
        print "STATUS CODE: %s" % response.statuscode
        print response.body
        self.assertTrue(response.statuscode == 301)
        self.assertEquals(response.location, "http://a16z.com/resources")

        resource = redirect("/download/", "http://a16z.com/resources")
        app = Application("/", [resource])
        app.secret = "SHERLOCK"
        print "Testing hitting the base redirection URL directly"
        request = Request.blank("http://localhost:80/download/")
        response = request.execute(app)
        print "STATUS CODE: %s" % response.statuscode
        print response.body
        self.assertTrue(response.statuscode == 301)
        self.assertEquals(response.location, "http://a16z.com/resources")

        resource = redirect("/", "http://a16z.com/resources")
        app = Application("/", [resource])
        app.secret = "SHERLOCK"
        print "Testing redirecting from the base URL"
        request = Request.blank("http://localhost:80/")
        response = request.execute(app)
        print "STATUS CODE: %s" % response.statuscode
        print response.body
        self.assertTrue(response.statuscode == 301)
        self.assertEquals(response.location, "http://a16z.com/resources")
Exemplo n.º 7
0
 def testRouting(self):
     '''test routing and application coupling'''
     body = "Hello Potates"
     assertions = self
     @Path("/products")
     class Product(Resource):
     
         @GET
         @Route("/{product}/{colour}")
         def show(self, product, colour):
             '''Just display some text'''
             assertions.assertEquals(self.request.body, body)
             self.response.status = "200 OK"
             self.response.type = "text/plain"
             self.response.charset = "UTF-8"
             self.response.write("Showing product: %s with colour: %s" % (product, colour))
         
         @POST
         @Route("/broken")
         def broken(self):
             '''A route that just breaks'''
             raise Exception("I'm broken")
         
         @OPTIONS
         @Route("/hello/{name}")
         def hello(self, name):
             '''A route that writes something to the header'''
             self.response.status = "200 OK"
             self.response.type = "text/plain"
             self.response.headers["Access-Control-Allow-Origin"] = "http://foo.example"
             self.response.write(name)
             
           
     application = Application(base="/base", resources=[Product,])
     application.secret = "SHERLOCK"
     request = Request.blank("http://localhost:80/base/products/BMW/black")
     request.body = body
     response = request.execute(application, False)
     self.assertTrue("text/plain" in response.type)
     self.assertEquals(response.body , "Showing product: BMW with colour: black")
     
     application = Application(base="/base", resources=[Product,])
     application.secret = "SHERLOCK"
     request = Request.blank("http://localhost:80/base/products/BMW/black")
     request.body = body
     request.method = "HEAD"
     response = request.execute(application, False)
     self.assertTrue("text/plain" in response.type)
     self.assertEquals(response.body , "") #There shouldn't be any body on a HEAD
     
     request = Request.blank("http://localhost:80/base/products/broken")
     request.body = body
     request.method = "POST"
     response = request.execute(application, False)
     self.assertTrue("text/plain" in response.type)
     self.assertEquals(500, response.statuscode)
     self.assertEquals("500 Internal Server Error", response.status)
     
     request = Request.blank("http://localhost:80/base/products/broken")
     request.body = body
     request.method = "HEAD"
     response = request.execute(application, False)
     self.assertTrue("text/plain" in response.type)
     self.assertEquals(500, response.statuscode)
     self.assertEquals("500 Internal Server Error", response.status)
     
     request = Request.blank("http://localhost:80/base/products/hello/iroiso")
     request.method = "OPTIONS"
     response = request.execute(application, False)
     self.assertTrue("text/plain" in response.type)
     self.assertEquals(response.body , "iroiso") 
     self.assertEquals(response.headers["Access-Control-Allow-Origin"], "http://foo.example")