예제 #1
0
 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"})
예제 #2
0
 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")
예제 #3
0
 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"})
예제 #4
0
 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"})
예제 #5
0
    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(), {})
예제 #6
0
    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"})
예제 #7
0
 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/",
         },
     )
예제 #8
0
 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")
예제 #9
0
    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)
예제 #10
0
    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"
            },
        )
예제 #11
0
    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")
예제 #12
0
    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"))
예제 #13
0
 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)
예제 #14
0
 def setUp(self):
     self.request = itty3.HttpRequest(
         "/greet/person/?name=Daniel#visited", itty3.GET
     )