コード例 #1
0
    def test_get_file_content(self):
        request = Request(Environ())
        stream = _io.BytesIO("hello world".encode())
        request.files = {"file": FileStorage(stream=stream, filename="foo")}

        self.assertEqual("hello world",
                         BaseHandler._get_file_content(request).decode())
コード例 #2
0
    def test_call_route_args(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)

        res = handler._call(handler.dummy_endpoint, {"param": "42"}, request)
        self.assertEqual(43, res["incremented"])
コード例 #3
0
    def test_call_general_attrs(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)

        res = handler._call(handler.myip, {}, request)
        self.assertEqual("1.2.3.4", res)
コード例 #4
0
    def test_call_default(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)

        res = handler._call(handler.dummy_endpoint, {}, request)
        self.assertEqual(124, res["incremented"])
コード例 #5
0
    def test_call_file(self):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)
        request.files = {"file": FileStorage(filename="foo")}

        res = handler._call(handler.file, {}, request)
        self.assertEqual("foo", res)
コード例 #6
0
    def test_call_fail_cast_parameter(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)
        request.form = {"param": "nope"}

        with self.assertRaises(BadRequest):
            handler._call(handler.dummy_endpoint, {}, request)
コード例 #7
0
    def test_call_cast_parameter(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)
        request.form = {'param': '42'}

        res = handler._call(handler.dummy_endpoint, {}, request)
        self.assertEqual(43, res["incremented"])
コード例 #8
0
    def test_call_with_error(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)

        with self.assertRaises(Forbidden) as ex:
            handler._call(handler.required, {"param": 42}, request)

        response = ex.exception.response
        self.assertIn("NOBUONO", response.data.decode())
        self.assertIn("nononono", response.data.decode())
コード例 #9
0
    def test_call_required_args(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)

        with self.assertRaises(BadRequest) as ex:
            handler._call(handler.required, {}, request)

        response = ex.exception.response
        self.assertIn("MISSING_PARAMETER", response.data.decode())
        self.assertIn("param", response.data.decode())
コード例 #10
0
    def test_call(self, name_mock, content_mock, ip_mock):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)
        request.form = {"param": 42}

        res = handler._call(handler.dummy_endpoint, {}, request)
        self.assertEqual(43, res["incremented"])

        Logger.c.execute("SELECT * FROM logs WHERE category = 'HTTP'")
        row = Logger.c.fetchone()
        self.assertIn("1.2.3.4", row[3])
        self.assertIn("dummy_endpoint", row[3])
コード例 #11
0
    def test_call_with_decorators(self):
        handler = TestBaseHandler.DummyHandler()
        env = Environ({"wsgi.input": None})
        request = Request(env)
        Database.add_user("token", "", "")
        Database.add_task("poldo", "", "", 1, 1)
        Database.add_input("inputid", "token", "poldo", 1, "", 42)
        Database.add_output("outputid", "inputid", "", 42, "")

        handler._call(handler.with_decorators, {
            "input_id": "inputid",
            "output_id": "outputid"
        }, request)
コード例 #12
0
    def test_get_ip_no_proxies(self):
        Config.num_proxies = 0
        request = Request(Environ(REMOTE_ADDR="1.2.3.4"))

        ip = BaseHandler.get_ip(request)
        self.assertEqual("1.2.3.4", ip)
コード例 #13
0
    def test_get_file_content_no_file(self):
        request = Request(Environ())
        request.files = {}

        self.assertIsNone(BaseHandler._get_file_content(request))
コード例 #14
0
    def test_get_file_name(self):
        request = Request(Environ())
        request.files = {"file": FileStorage(filename="foo")}

        self.assertEqual("foo", BaseHandler._get_file_name(request))