def string_integrety_tests_json(self, methodname): fp_req = open('data/stringtests/in_out_test.json', 'rb') req = PORTABLE_STRING(fp_req.read(), 'utf-8') fp_req.close() req = json.loads(req) req['methodname'] = methodname req = json.dumps(req) # utf-8 encoded request to bytesEncodingTest status, reason, resdata = self.post_helper.post_request( req.encode('utf-8'), encoding='utf-8') self.assertEqual(status, 200) res = json.loads(PORTABLE_STRING(resdata, 'utf-8')) expected_result = str_to_portable_string('äöüÄÖÄæøåÆØÅß') self.assertEqual(res['result'], expected_result) # latin-1 encoded request to bytesEncodingTest status, reason, resdata = self.post_helper.post_request( req.encode('latin-1'), encoding='latin-1') self.assertEqual(status, 200) res = json.loads(PORTABLE_STRING(resdata, 'latin-1')) self.assertEqual(res['result'], expected_result)
def from_unicode_string(self, val, typ, prefilters=None, postfilters=None, attr_name=None): """Convert a unicode string to certain types. """ # Run incoming_raw filters aka. incoming prefilters if prefilters: for func in prefilters: val = func(val) vtyp = type(val) # The following fix (accepting booleans) has been added because # keyword parameter parse_constant of json.loads has changed to # not react on true/false (http://bugs.python.org/issue14692) if vtyp == bool and typ == bool: val = PORTABLE_STRING(val) vtyp = PORTABLE_STRING # Check the value type if not vtyp == PORTABLE_STRING: raise NonUnicodeError(type(val), attr_name=attr_name) # unicode to unicode or raw string if typ in [PORTABLE_BYTES, PORTABLE_STRING]: if typ == PORTABLE_BYTES: val = val.encode(self.encoding) # Run incoming filters aka. incoming postfilters if postfilters: for func in postfilters: val = func(val) return val # other primitive types try: if typ == bool and (val[0].upper() == PORTABLE_STRING('F') or val.strip() == '0'): val = False else: val = typ(val) # Run incoming filters aka. incoming postfilters if postfilters: for func in postfilters: val = func(val) return val except Exception as e: raise FromUnicodeConversionError(typ, str(e), attr_name=attr_name)
def from_unicode_string(self,val,typ,prefilters=None,postfilters=None): """Convert a unicode string to certain types. """ # Run incoming_raw filters aka. incoming prefilters if prefilters: for func in prefilters: val = func(val) vtyp = type(val) # The following fix (accepting booleans) has been added because # keyword parameter parse_constant of json.loads has changed to # not react on true/false (http://bugs.python.org/issue14692) if vtyp==bool and typ==bool: val = PORTABLE_STRING(val) vtyp = PORTABLE_STRING # Check the value type if not vtyp==PORTABLE_STRING: raise NonUnicodeError(type(val)) # unicode to unicode or raw string if typ in [PORTABLE_BYTES,PORTABLE_STRING]: if typ==PORTABLE_BYTES: val = val.encode(self.encoding) # Run incoming filters aka. incoming postfilters if postfilters: for func in postfilters: val = func(val) return val # other primitive types try: if typ==bool and (val[0].upper()==PORTABLE_STRING('F') or val.strip()=='0'): val = False else: val = typ(val) # Run incoming filters aka. incoming postfilters if postfilters: for func in postfilters: val = func(val) return val except Exception as e: raise FromUnicodeConversionError(typ,str(e))