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")
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")