예제 #1
0
        def dispatch(self):
            add_cors_headers(self.response.headers)

            api = api_class()
            api.initialize_request_state(
                remote.HttpRequestState(
                    remote_host=None,
                    remote_address=self.request.remote_addr,
                    server_host=self.request.host,
                    server_port=self.request.server_port,
                    http_method=self.request.method,
                    service_path=service_path,
                    headers=self.request.headers.items()))

            try:
                req = decode_message(api_method.remote, self.request)
                # Check that required fields are populated.
                req.check_initialized()
            except (messages.DecodeError, messages.ValidationError,
                    ValueError) as ex:
                response_body = json.dumps({'error': {'message': ex.message}})
                self.response.set_status(httplib.BAD_REQUEST)
            else:
                try:
                    res = api_method(api, req)
                except endpoints.ServiceException as ex:
                    response_body = json.dumps(
                        {'error': {
                            'message': ex.message
                        }})
                    self.response.set_status(ex.http_status)
                else:
                    if isinstance(res, message_types.VoidMessage):
                        self.response.set_status(204)
                        response_body = None
                    else:
                        response_body = PROTOCOL.encode_message(res)
                        if self.request.get('fields'):
                            try:
                                # PROTOCOL.encode_message checks that the message is initialized
                                # before dumping it directly to JSON string. Therefore we can't
                                # mask the protocol buffer (if masking removes a required field
                                # then encode_message will fail). Instead, call encode_message
                                # first, then load the JSON string into a dict, mask the dict,
                                # and dump it back to JSON.
                                response_body = json.dumps(
                                    partial.mask(json.loads(response_body),
                                                 self.request.get('fields')))
                            except (partial.ParsingError, ValueError) as e:
                                # Log the error but return the full response.
                                logging.warning(
                                    'Ignoring erroneous field mask %r: %s',
                                    self.request.get('fields'), e)

            if self.response.status_int != 204:
                self.response.content_type = 'application/json; charset=utf-8'
                self.response.out.write(response_body)
            else:
                # webob sets content_type to text/html by default.
                self.response.content_type = ''
예제 #2
0
    def test_simple(self):
        r = {'a': 1, 'b': 2}
        partial.mask(r, 'a')
        self.assertEqual(r, {
            'a': 1,
        })

        r = {'a': 1, 'b': 2}
        partial.mask(r, 'b')
        self.assertEqual(r, {
            'b': 2,
        })

        r = {'a': 1, 'b': 2}
        partial.mask(r, 'a,b')
        self.assertEqual(r, {
            'a': 1,
            'b': 2,
        })
예제 #3
0
    def test_recursive(self):
        r = {'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': 4}}
        partial.mask(r, 'a')
        self.assertEqual(r, {
            'a': {
                'b': 1,
                'c': 2,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': 4}}
        partial.mask(r, 'd')
        self.assertEqual(r, {
            'd': {
                'e': 3,
                'f': 4,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': 4}}
        partial.mask(r, 'a,d')
        self.assertEqual(r, {
            'a': {
                'b': 1,
                'c': 2,
            },
            'd': {
                'e': 3,
                'f': 4,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': 4}}
        partial.mask(r, 'a/b')
        self.assertEqual(r, {
            'a': {
                'b': 1,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': 4}}
        partial.mask(r, 'a/b,d/f')
        self.assertEqual(r, {
            'a': {
                'b': 1,
            },
            'd': {
                'f': 4,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'b': 3, 'c': 4}}
        partial.mask(r, 'a/x,d/c')
        self.assertEqual(r, {
            'd': {
                'c': 4,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'b': 3, 'c': 4}}
        partial.mask(r, 'a/b,d/x')
        self.assertEqual(r, {
            'a': {
                'b': 1,
            },
        })

        r = {'a': {'b': 1, 'c': 2}, 'd': {'b': 3, 'c': 4}}
        partial.mask(r, 'a/x,d/y')
        self.assertFalse(r)

        r = {'a': {'b': 1, 'c': 2, 'd': 3}, 'e': {'b': 4, 'c': 5, 'd': 6}}
        partial.mask(r, 'a(b,d),e/c')
        self.assertEqual(r, {
            'a': {
                'b': 1,
                'd': 3,
            },
            'e': {
                'c': 5,
            },
        })

        r = {'a': {'b': 1, 'c': 2, 'd': 3}, 'e': {'b': 4, 'c': 5, 'd': 6}}
        partial.mask(r, '*(b,d),e/c')
        self.assertEqual(r, {
            'a': {
                'b': 1,
                'd': 3,
            },
            'e': {
                'b': 4,
                'c': 5,
                'd': 6,
            },
        })

        r = {'a': {'b': 1, 'c': 2, 'd': 3}, 'e': {'b': 4, 'c': 5, 'd': 6}}
        partial.mask(r, 'a(*),e/c')
        self.assertEqual(r, {
            'a': {
                'b': 1,
                'c': 2,
                'd': 3,
            },
            'e': {
                'c': 5,
            },
        })

        r = {
            'a': [{
                'b': 1,
                'c': 2
            }, {
                'b': 3,
                'c': 4
            }],
            'd': [{
                'b': 5,
                'c': 6
            }]
        }
        partial.mask(r, 'a(b)')
        self.assertEqual(r, {
            'a': [
                {
                    'b': 1
                },
                {
                    'b': 3
                },
            ],
        })

        r = {
            'a': [{
                'b': 1,
                'c': 2
            }, {
                'b': 3,
                'c': 4
            }],
            'd': [{
                'b': 5,
                'c': 6
            }]
        }
        partial.mask(r, 'd/b')
        self.assertEqual(r, {
            'd': [
                {
                    'b': 5
                },
            ],
        })
        r = {
            'a': [{
                'b': 1,
                'c': 2
            }, {
                'b': 3,
                'c': 4
            }],
            'd': [{
                'b': 5,
                'c': 6
            }]
        }
        partial.mask(r, 'a(b),d/b')
        self.assertEqual(r, {
            'a': [
                {
                    'b': 1
                },
                {
                    'b': 3
                },
            ],
            'd': [
                {
                    'b': 5
                },
            ],
        })

        r = {
            'a': [{
                'b': 1,
                'c': 2
            }, {
                'b': 3,
                'c': 4
            }],
            'd': [{
                'b': 5,
                'c': 6
            }]
        }
        partial.mask(r, '*(b)')
        self.assertEqual(r, {
            'a': [
                {
                    'b': 1
                },
                {
                    'b': 3
                },
            ],
            'd': [
                {
                    'b': 5
                },
            ],
        })

        r = {
            'a': [{
                'b': 1,
                'c': 2
            }, {
                'b': 3,
                'c': 4
            }],
            'd': [{
                'b': 5,
                'c': 6
            }]
        }
        partial.mask(r, '*(x)')
        self.assertFalse(r)