示例#1
0
    def test_json_favored(self):
        data = Person()
        data.id = 1
        data.name = "tester"
        data.email = "*****@*****.**"

        response_data = {
            "id": data.id,
            "name": data.name,
            "email": data.email
        }

        app = flask.Flask(__name__)
        with app.test_request_context(
            data=data.SerializeToString(),
            method='POST',
            content_type="application/x-protobuf",
            headers={
                "Accept": "*/*"
            }
        ):
            @api(json, protobuf(receives=Person))
            def view_method():
                return flask.request.data_dict

            response, status_code, headers = view_method()

        self.assertEquals(response.status_code, 200)
        self.assertEquals(loads(response.data), response_data)
        self.assertEquals(response.mimetype, "application/json")
示例#2
0
    def test_simple_pb_response(self):
        person = Person()
        person.id = 1
        person.name = "tester"
        person.email = "*****@*****.**"

        data = {
            "id": person.id,
            "name": person.name,
            "email": person.email
        }

        app = flask.Flask(__name__)

        with app.test_request_context(
            method='GET',
            content_type="application/x-protobuf",
            headers={
                "Accept": "application/x-protobuf"
            }
        ):
            @api(protobuf(sends=Person))
            def view_method():
                return data

            response, status_code, headers = view_method()

        response_data = Person()
        response_data.ParseFromString(response.data)

        self.assertEquals(response_data, person)
        self.assertEquals(response.mimetype, 'application/x-protobuf')
        self.assertEquals(status_code, 200)
示例#3
0
    def test_simple_pb_response(self):
        person = Person()
        person.id = 1
        person.name = "tester"
        person.email = "*****@*****.**"

        data = {"id": person.id, "name": person.name, "email": person.email}

        app = flask.Flask(__name__)

        with app.test_request_context(
                method='GET',
                content_type="application/x-protobuf",
                headers={"Accept": "application/x-protobuf"}):

            @api(protobuf(sends=Person))
            def view_method():
                return data

            response, status_code, headers = view_method()

        response_data = Person()
        response_data.ParseFromString(response.data)

        self.assertEquals(response_data, person)
        self.assertEquals(response.mimetype, 'application/x-protobuf')
        self.assertEquals(status_code, 200)
示例#4
0
    def test_bad_accept(self):
        data = {'a': 1}
        app = flask.Flask(__name__)
        with app.test_request_context(
                method='GET',
                content_type="application/x-protobuf",
                headers={"Accept": "application/x-plist"}):

            @api(protobuf(sends=Person))
            def view_method():
                return data

            with self.assertRaises(NotAcceptable):
                response, status_code, headers = view_method()
示例#5
0
    def test_bad_content_type(self):
        data = {'a': 1}
        app = flask.Flask(__name__)
        with app.test_request_context(
                method='POST',
                content_type="application/x-plist",
                headers={"Accept": "application/x-protobuf"}):

            @api(protobuf(receives=Person))
            def view_method():
                return data

            with self.assertRaises(UnsupportedMediaType):
                response, status_code, headers = view_method()
示例#6
0
 def test_malformed_data(self):
     app = flask.Flask(__name__)
     with app.test_request_context(
         data="this data is malformed because it is not a protobuf binary.",
         method='POST',
         content_type="application/x-protobuf",
         headers={
             "Accept": "application/x-protobuf"
         }
     ):
         @api(protobuf(receives=Person))
         def view_method():
             pass
         with self.assertRaises(BadRequest):
             view_method()
示例#7
0
 def test_bad_accept(self):
     data = {'a': 1}
     app = flask.Flask(__name__)
     with app.test_request_context(
         method='GET',
         content_type="application/x-protobuf",
         headers={
             "Accept": "application/x-plist"
         }
     ):
         @api(protobuf(sends=Person))
         def view_method():
             return data
         with self.assertRaises(NotAcceptable):
             response, status_code, headers = view_method()
示例#8
0
 def test_bad_content_type(self):
     data = {'a': 1}
     app = flask.Flask(__name__)
     with app.test_request_context(
         method='POST',
         content_type="application/x-plist",
         headers={
             "Accept": "application/x-protobuf"
         }
     ):
         @api(protobuf(receives=Person))
         def view_method():
             return data
         with self.assertRaises(UnsupportedMediaType):
             response, status_code, headers = view_method()
示例#9
0
    def test_malformed_data(self):
        app = flask.Flask(__name__)
        with app.test_request_context(
                data=
                "this data is malformed because it is not a protobuf binary.",
                method='POST',
                content_type="application/x-protobuf",
                headers={"Accept": "application/x-protobuf"}):

            @api(protobuf(receives=Person))
            def view_method():
                pass

            with self.assertRaises(BadRequest):
                view_method()
示例#10
0
    def test_simple_pb_request(self):
        data = Person()
        data.id = 1
        data.name = "tester"
        data.email = "*****@*****.**"

        app = flask.Flask(__name__)
        with app.test_request_context(
            data=data.SerializeToString(),
            method='POST',
            content_type="application/x-protobuf",
            headers={
                "Accept": "application/x-protobuf"
            }
        ):
            @api(protobuf(receives=Person))
            def view_method():
                self.assertEqual(flask.request.data_dict, {'id': 1, 'name': 'tester', 'email': '*****@*****.**'})
                return 200

            view_method()
示例#11
0
    def test_json_favored(self):
        data = Person()
        data.id = 1
        data.name = "tester"
        data.email = "*****@*****.**"

        response_data = {"id": data.id, "name": data.name, "email": data.email}

        app = flask.Flask(__name__)
        with app.test_request_context(data=data.SerializeToString(),
                                      method='POST',
                                      content_type="application/x-protobuf",
                                      headers={"Accept": "*/*"}):

            @api(json, protobuf(receives=Person))
            def view_method():
                return flask.request.data_dict

            response, status_code, headers = view_method()

        self.assertEquals(response.status_code, 200)
        self.assertEquals(loads(response.data), response_data)
        self.assertEquals(response.mimetype, "application/json")
示例#12
0
    def test_simple_pb_request(self):
        data = Person()
        data.id = 1
        data.name = "tester"
        data.email = "*****@*****.**"

        app = flask.Flask(__name__)
        with app.test_request_context(
                data=data.SerializeToString(),
                method='POST',
                content_type="application/x-protobuf",
                headers={"Accept": "application/x-protobuf"}):

            @api(protobuf(receives=Person))
            def view_method():
                self.assertEqual(flask.request.data_dict, {
                    'id': 1,
                    'name': 'tester',
                    'email': '*****@*****.**'
                })
                return 200

            view_method()