def test_successful_base64_conversion(original, base64_encoded): # all unicode characters should be handled correctly assert to_base64(original) == base64_encoded assert from_base64(base64_encoded) == original # "to" and "from" should be inverses assert from_base64(to_base64(original)) == original assert to_base64(from_base64(base64_encoded)) == base64_encoded
def test_failed_base64_conversion(input): # conversion from base64 should fail if given input of the wrong type or # input which isn't a valid base64 string assert from_base64(input) is None # any string can be converted to base64, so only type errors will cause # failures if type(input) not in string_types: assert to_base64(input) is None
def compute_tracestate_value(data): # type: (typing.Mapping[str, str]) -> str """ Computes a new tracestate value using the given data. Note: Returns just the base64-encoded data, NOT the full `sentry=...` tracestate entry. """ tracestate_json = json.dumps(data) # Base64-encoded strings always come out with a length which is a multiple # of 4. In order to achieve this, the end is padded with one or more `=` # signs. Because the tracestate standard calls for using `=` signs between # vendor name and value (`sentry=xxx,dogsaregreat=yyy`), to avoid confusion # we strip the `=` return (to_base64(tracestate_json) or "").rstrip("=")
def test_tracestate_reinflation(data): encoded_tracestate = to_base64(json.dumps(data)).strip("=") assert reinflate_tracestate(encoded_tracestate) == data