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 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")
def testDo(self): '''Tests for reads on a route''' print("Testing PUTs now") request = Request.blank("http://localhost:80/users/do/some/stuff/with/iroiso") request.method = "PUT" response = request.execute(self.application, True) self.assertEquals(response.statuscode, 200) self.assertEquals(response.text, "Just created: iroiso") print("Testing POSTs now") request = Request.blank("http://localhost:80/users/do/some/stuff/with/iroiso") request.method = "POST" response = request.execute(self.application, True) self.assertEquals(response.statuscode, 200) self.assertEquals(response.text, "Just created: iroiso")
def testUpdate(self): '''Tests the update URI''' request = Request.blank("http://localhost:80/users/update/iroiso") request.method = "PUT" response = request.execute(self.application, True) self.assertEquals(response.statuscode, 200) self.assertEquals(response.text, "Just updated: iroiso")
def testFileSave(self): """Tests that we can read uploaded files with Gates.""" tester = self @Path("/") class UploadHandler(Resource): @POST @Route("/upload") def handle(self): iterator, name = self.request.upload("file") self.save(iterator, "src/tests/core/assets/upload.txt") self.response.write("welcome.") self.application = Application("/", resources=[UploadHandler,]) self.application.debug = False self.application.secret = "SHERLOCK" request = Request.blank("http://127.0.0.1:8000/upload", POST={'file': ('filename', StringIO('Hello Francis'))}) request.method = "POST" response = request.execute(self.application) self.assertTrue(response.statuscode, 200) self.assertEquals(response.body, "welcome.") self.assertTrue(os.path.exists("src/tests/core/assets/upload.txt")) content = open("src/tests/core/assets/upload.txt", 'r').read() self.assertTrue( content == "Hello Francis")
def testSessionProperties(self): '''Tests if the application saves sessions''' tester = self @Path("/") class SessionResource(Resource): @POST @Route("/test") def test(self): print "Show that we cannot access reserved keys" with tester.assertRaises(KeyError): self.session['ipaddr'] with tester.assertRaises(KeyError): del self.session['ua'] with tester.assertRaises(KeyError): self.session['ua'] = 'Something' body = self.request.body parts = body.split(":") self.session['user'] = parts[0] self.response.addCookie("hello", "world") self.response.write("welcome.") self.application = Application("/", resources=[SessionResource,]) self.application.debug = False self.application.secret = "SHERLOCK" request = Request.blank("http://127.0.0.1:8000/test") request.method = "POST" response = request.execute(self.application) self.assertTrue(response.statuscode, 200) self.assertEquals(response.body, "welcome.")
def testNew(self): '''Tests for the new route''' request = Request.blank("http://localhost:80/users/create/iroiso") request.method = "POST" response = request.execute(self.application, True) self.assertEquals(response.statuscode, 200) self.assertEquals(response.text, "Just created: iroiso")
def testDeactivate(self): '''Tests for reads on a route''' request = Request.blank("http://localhost:80/users/deactivate/iroiso") request.method = "DELETE" response = request.execute(self.application, True) self.assertEquals(response.statuscode, 200) self.assertEquals(response.text, "Just deactivated: iroiso")
def testRead(self): '''Tests for reads on a route''' request = Request.blank("http://localhost:80/heart/iroiso") request.method = "GET" response = request.execute(self.application, True) self.assertEquals(response.statuscode, 200) self.assertEquals(response.text, "Just fetched: iroiso")
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")
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')
def testFileShortcut(self): '''Tests if the shortcut for serving up directories work''' resource = serve("/static", os.path.join(os.getcwd(),"src/tests/core/assets")) self.application = Application("/", [resource,]) self.application.debug = False self.application.secret = "SHERLOCK" request = Request.blank("http://localhost:80/static/output.txt") response = request.execute(self.application) self.assertTrue("text/plain" in response.type) print response.body self.assertEquals(response.body , "Hello world\n") self.assertTrue(response.statuscode == 200)
def testFile(self): '''Tests if the application serves up the file''' resource = bind(r"/static", StaticFileHandler) root = os.path.join(os.getcwd(),"src/tests/core/assets") arguments = {"root" : root } self.application = Application("/", resources=[(resource, arguments),]) self.application.debug = False self.application.secret = "SHERLOCK" request = Request.blank("http://localhost:80/static/output.txt") response = request.execute(self.application) self.assertTrue("text/plain" in response.type) print response.body self.assertEquals(response.body , "Hello world\n") self.assertTrue(response.statuscode == 200)
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
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")
def testSessionWithApplication(self): '''Tests if the application saves sessions''' @Path("/") class SessionResource(Resource): @POST @Route("/login") def login(self): body = self.request.body parts = body.split(":") self.session['user'] = parts[0] self.response.addCookie("hello", "world") self.response.write("welcome.") @GET @Route("/who") def who(self): if 'user' in self.session: self.response.write(self.session['user']) else: abort(403, "forbidden") @POST @Route("/logout") def logout(self): del self.session['user'] self.response.write("bye.") self.application = Application("/", resources=[SessionResource,]) self.application.debug = False self.application.secret = "SHERLOCK" request = Request.blank("http://127.0.0.1:8000/login") request.body = "iroiso:watson" request.type = "text/plain" request.method = "POST" response = request.execute(self.application) self.assertTrue(response.statuscode, 200) self.assertEquals(response.body, "welcome.") request = Request.blank("http://127.0.0.1:8000/who") request.method = "GET" self.copyCookies(response, request) response = request.execute(self.application) self.assertTrue(response.statuscode, 200) self.assertEquals(response.body, "iroiso") request = Request.blank("http://127.0.0.1:8000/who") request.method = "GET" self.copyCookies(response, request) response = request.execute(self.application) self.assertTrue(response.statuscode, 200) self.assertEquals(response.body, "iroiso") request = Request.blank("http://127.0.0.1:8000/logout") request.type = "text/plain" request.method = "POST" self.copyCookies(response, request) response = request.execute(self.application) self.assertTrue(response.statuscode, 200) self.assertEquals(response.body, "bye.") print("After a logout, cookies would not save you.") request = Request.blank("http://127.0.0.1:8000/who") self.copyCookies(response, request) response = request.execute(self.application) self.assertTrue(response.statuscode, 403) self.assertTrue("forbidden" in response.body) print("Without cookies, you will be rejected all the same") request = Request.blank("http://127.0.0.1:8000/who") response = request.execute(self.application) self.assertTrue(response.statuscode, 403) self.assertTrue("forbidden" in response.body)
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")
def testBlank(self): '''Tests if Request.blank works''' req = Request.blank("http://localhost/test") self.assertTrue(req is not None) self.assertEquals(req.host, "localhost:80") self.assertEquals(req.path, "/test")