def test_render_json(self): req = itty3.HttpRequest("/greet/?name=Daniel", "GET") resp = self.app.render_json(req, {"greeting": "Hello, Daniel!"}) self.assertEqual(resp.body, '{"greeting": "Hello, Daniel!"}') self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content_type, itty3.JSON) self.assertEqual(resp.headers, {"Content-Type": "application/json"})
def test_PUT(self): req = itty3.HttpRequest( "/", itty3.PUT, body="name=Daniel&msg=Hello&submit=true" ) self.assertEqual(req.PUT["name"], "Daniel") self.assertEqual(req.PUT["msg"], "Hello") self.assertEqual(req.PUT["submit"], "true")
def test_render(self): req = itty3.HttpRequest("/greet/?name=Daniel", "GET") resp = self.app.render(req, "Hello, Daniel!", content_type=itty3.HTML) self.assertEqual(resp.body, "Hello, Daniel!") self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content_type, itty3.HTML) self.assertEqual(resp.headers, {"Content-Type": "text/html"})
def test_error_500(self): req = itty3.HttpRequest("/greet/?name=Daniel", "GET") resp = self.app.error_500(req) self.assertEqual(resp.body, "Internal Error") self.assertEqual(resp.status_code, 500) self.assertEqual(resp.content_type, itty3.HTML) self.assertEqual(resp.headers, {"Content-Type": "text/html"})
def test_json_but_its_not_json(self): req = itty3.HttpRequest( "/greet/", itty3.POST, body="Hello, world!", headers={"Content-Type": itty3.PLAIN}, ) # Trying to call `.json()` on a non-JSON payload gives you an empty # dict instead of blowing up trying to decode something. self.assertEqual(req.json(), {})
def test_json(self): req = itty3.HttpRequest( "/greet/", itty3.POST, body='{"hello": "world"}', headers={ "Content-Type": "application/json", "Accept": "application/json", }, ) self.assertEqual(req.json(), {"hello": "world"})
def test_redirect(self): req = itty3.HttpRequest("/greet/?name=Daniel", "GET") resp = self.app.redirect(req, "/why/hello/there/Daniel/") self.assertEqual(resp.body, "") self.assertEqual(resp.status_code, 302) self.assertEqual(resp.content_type, itty3.PLAIN) self.assertEqual( resp.headers, { "Content-Type": "text/plain", "Location": "/why/hello/there/Daniel/", }, )
def test_content_type_complex(self): req = itty3.HttpRequest( "https://example.com:443/greet/person/?name=Daniel#visited", itty3.PATCH, body='{"hello": "world"}', headers={ "Content-Type": "application/json", "Accept": "application/json", }, scheme="https", host="example.com", port=443, ) self.assertEqual(req.content_type(), "application/json")
def test_render_static_not_found(self): # We have to manually setup the static serving here (normally # handled in `App.run`). self.app.static_root = os.path.join( os.path.dirname(__file__), "test_static_assets", ) self.app.static_url_path = "/static/" self.app.add_route(itty3.GET, "/static/<any:asset_path>", self.app.render_static) req = itty3.HttpRequest("/static/not/even/../present//whatev.wmv", "GET") resp = self.app.render_static( req, "/static/not/even/../present//whatev.wmv") self.assertEqual(resp.status_code, 404)
def test_render_static_png(self): # We have to manually setup the static serving here (normally # handled in `App.run`). self.app.static_root = os.path.join( os.path.dirname(__file__), "test_static_assets", ) self.app.static_url_path = "/static/" self.app.add_route(itty3.GET, "/static/<any:asset_path>", self.app.render_static) req = itty3.HttpRequest("/static/itty.png", "GET") resp = self.app.render_static(req, "itty.png") self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content_type, "image/png") self.assertEqual( resp.headers, { "Content-Type": "image/png", "Content-Length": "1473" }, )
def test_attributes_complex(self): req = itty3.HttpRequest( "https://example.com:443/greet/person/?name=Daniel#visited", itty3.PATCH, body='{"hello": "world"}', headers={ "Content-Type": "application/json", "Accept": "application/json", }, scheme="https", host="example.com", port=443, ) self.assertEqual(req.method, "PATCH") self.assertEqual(req.body, '{"hello": "world"}') self.assertEqual(req.scheme, "https") self.assertEqual(req.host, "example.com") self.assertEqual(req.port, 443) self.assertEqual(req.path, "/greet/person/") self.assertEqual(req.query["name"], ["Daniel"]) self.assertEqual(req.fragment, "visited")
def test_render_static_css(self): # We have to manually setup the static serving here (normally # handled in `App.run`). self.app.static_root = os.path.join( os.path.dirname(__file__), "test_static_assets", ) self.app.static_url_path = "/static/" self.app.add_route(itty3.GET, "/static/<any:asset_path>", self.app.render_static) req = itty3.HttpRequest("/static/css/default.css", "GET") resp = self.app.render_static(req, "css/default.css") self.assertEqual(resp.status_code, 200) self.assertEqual(resp.content_type, "text/css") self.assertEqual( resp.headers, { "Content-Type": "text/css", "Content-Length": "146" }, ) self.assertTrue(resp.body.startswith("/* Reset"))
def test_render_static_not_configured(self): req = itty3.HttpRequest("/static/css/default.css", "GET") resp = self.app.render_static(req, "css/default.css") self.assertEqual(resp.status_code, 404)
def setUp(self): self.request = itty3.HttpRequest( "/greet/person/?name=Daniel#visited", itty3.GET )