OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 """Tests for Endpoints-specific ProtoJson class.""" |
| 16 |
| 17 import json |
| 18 import unittest |
| 19 |
| 20 import endpoints.protojson as protojson |
| 21 from protorpc import messages |
| 22 |
| 23 import test_util |
| 24 |
| 25 |
| 26 class MyMessage(messages.Message): |
| 27 """Test message containing various types.""" |
| 28 var_int32 = messages.IntegerField(1, variant=messages.Variant.INT32) |
| 29 var_int64 = messages.IntegerField(2, variant=messages.Variant.INT64) |
| 30 var_repeated_int64 = messages.IntegerField(3, variant=messages.Variant.INT64, |
| 31 repeated=True) |
| 32 var_sint64 = messages.IntegerField(4, variant=messages.Variant.SINT64) |
| 33 var_uint64 = messages.IntegerField(5, variant=messages.Variant.UINT64) |
| 34 var_bytes = messages.BytesField(6) |
| 35 |
| 36 |
| 37 class ModuleInterfaceTest(test_util.ModuleInterfaceTest, |
| 38 unittest.TestCase): |
| 39 |
| 40 MODULE = protojson |
| 41 |
| 42 |
| 43 class EndpointsProtoJsonTest(unittest.TestCase): |
| 44 |
| 45 def setUp(self): |
| 46 self.__protojson = protojson.EndpointsProtoJson() |
| 47 super(EndpointsProtoJsonTest, self).setUp() |
| 48 |
| 49 def CompareEncoded(self, expected_encoded, actual_encoded): |
| 50 """JSON encoding will be laundered to remove string differences.""" |
| 51 self.assertEquals(json.loads(expected_encoded), json.loads(actual_encoded)) |
| 52 |
| 53 def testEncodeInt32(self): |
| 54 """Make sure int32 values are encoded as integers.""" |
| 55 encoded_message = self.__protojson.encode_message(MyMessage(var_int32=123)) |
| 56 expected_encoding = '{"var_int32": 123}' |
| 57 self.CompareEncoded(expected_encoding, encoded_message) |
| 58 |
| 59 def testEncodeInt64(self): |
| 60 """Make sure int64 values are encoded as strings.""" |
| 61 encoded_message = self.__protojson.encode_message(MyMessage(var_int64=123)) |
| 62 expected_encoding = '{"var_int64": "123"}' |
| 63 self.CompareEncoded(expected_encoding, encoded_message) |
| 64 |
| 65 def testEncodeRepeatedInt64(self): |
| 66 """Make sure int64 in repeated fields are encoded as strings.""" |
| 67 encoded_message = self.__protojson.encode_message( |
| 68 MyMessage(var_repeated_int64=[1, 2, 3])) |
| 69 expected_encoding = '{"var_repeated_int64": ["1", "2", "3"]}' |
| 70 self.CompareEncoded(expected_encoding, encoded_message) |
| 71 |
| 72 def testEncodeSint64(self): |
| 73 """Make sure sint64 values are encoded as strings.""" |
| 74 encoded_message = self.__protojson.encode_message(MyMessage(var_sint64=-12)) |
| 75 expected_encoding = '{"var_sint64": "-12"}' |
| 76 self.CompareEncoded(expected_encoding, encoded_message) |
| 77 |
| 78 def testEncodeUint64(self): |
| 79 """Make sure uint64 values are encoded as strings.""" |
| 80 encoded_message = self.__protojson.encode_message(MyMessage(var_uint64=900)) |
| 81 expected_encoding = '{"var_uint64": "900"}' |
| 82 self.CompareEncoded(expected_encoding, encoded_message) |
| 83 |
| 84 def testBytesNormal(self): |
| 85 """Verify that bytes encoded with standard b64 encoding are accepted.""" |
| 86 for encoded, decoded in (('/+==', '\xff'), |
| 87 ('/+/+', '\xff\xef\xfe'), |
| 88 ('YWI+Y2Q/', 'ab>cd?')): |
| 89 self.assertEqual(decoded, self.__protojson.decode_field( |
| 90 MyMessage.var_bytes, encoded)) |
| 91 |
| 92 def testBytesUrlSafe(self): |
| 93 """Verify that bytes encoded with urlsafe b64 encoding are accepted.""" |
| 94 for encoded, decoded in (('_-==', '\xff'), |
| 95 ('_-_-', '\xff\xef\xfe'), |
| 96 ('YWI-Y2Q_', 'ab>cd?')): |
| 97 self.assertEqual(decoded, self.__protojson.decode_field( |
| 98 MyMessage.var_bytes, encoded)) |
| 99 |
| 100 def testBytesMisalignedEncoding(self): |
| 101 """Verify that misaligned BytesField data raises an error.""" |
| 102 for encoded in ('garbagebb', 'a===', 'a', 'abcde'): |
| 103 self.assertRaises(messages.DecodeError, |
| 104 self.__protojson.decode_field, |
| 105 MyMessage.var_bytes, encoded) |
| 106 |
| 107 def testBytesUnpaddedEncoding(self): |
| 108 """Verify that unpadded BytesField data is accepted.""" |
| 109 for encoded, decoded in (('YQ', 'a'), |
| 110 ('YWI', 'ab'), |
| 111 ('_-', '\xff'), |
| 112 ('VGVzdGluZyB1bnBhZGRlZCBtZXNzYWdlcw', |
| 113 'Testing unpadded messages')): |
| 114 self.assertEqual(decoded, self.__protojson.decode_field( |
| 115 MyMessage.var_bytes, encoded)) |
| 116 |
| 117 def testBytesInvalidChars(self): |
| 118 """Verify that invalid characters are ignored in BytesField encodings.""" |
| 119 for encoded in ('\x00\x01\x02\x03', '\xff==='): |
| 120 self.assertEqual('', self.__protojson.decode_field(MyMessage.var_bytes, |
| 121 encoded)) |
| 122 |
| 123 if __name__ == '__main__': |
| 124 unittest.main() |
OLD | NEW |