diff --git a/test/test.py b/test/test.py index a4cde0bf..670a1b1e 100644 --- a/test/test.py +++ b/test/test.py @@ -549,21 +549,22 @@ def test_tracestate_multiple_headers_different_keys(self): def test_tracestate_duplicated_keys(self): ''' harness sends a request with an invalid tracestate header with duplicated keys - expects the tracestate to be discarded + expects the tracestate to be inherited, and the duplicated keys to be either kept as-is or one of them + to be discarded ''' traceparent, tracestate = self.make_single_request_and_get_tracecontext([ ['traceparent', '00-12345678901234567890123456789012-1234567890123456-00'], ['tracestate', 'foo=1,foo=1'], ]) self.assertEqual(traceparent.trace_id.hex(), '12345678901234567890123456789012') - self.assertRaises(KeyError, lambda: tracestate['foo']) + self.assertTrue('foo=1' in str(tracestate)) traceparent, tracestate = self.make_single_request_and_get_tracecontext([ ['traceparent', '00-12345678901234567890123456789012-1234567890123456-00'], ['tracestate', 'foo=1,foo=2'], ]) self.assertEqual(traceparent.trace_id.hex(), '12345678901234567890123456789012') - self.assertRaises(KeyError, lambda: tracestate['foo']) + self.assertTrue('foo=1' in str(tracestate) or 'foo=2' in str(tracestate)) traceparent, tracestate = self.make_single_request_and_get_tracecontext([ ['traceparent', '00-12345678901234567890123456789012-1234567890123456-00'], @@ -571,7 +572,7 @@ def test_tracestate_duplicated_keys(self): ['tracestate', 'foo=1'], ]) self.assertEqual(traceparent.trace_id.hex(), '12345678901234567890123456789012') - self.assertRaises(KeyError, lambda: tracestate['foo']) + self.assertTrue('foo=1' in str(tracestate)) traceparent, tracestate = self.make_single_request_and_get_tracecontext([ ['traceparent', '00-12345678901234567890123456789012-1234567890123456-00'], @@ -579,7 +580,7 @@ def test_tracestate_duplicated_keys(self): ['tracestate', 'foo=2'], ]) self.assertEqual(traceparent.trace_id.hex(), '12345678901234567890123456789012') - self.assertRaises(KeyError, lambda: tracestate['foo']) + self.assertTrue('foo=1' in str(tracestate) or 'foo=2' in str(tracestate)) def test_tracestate_all_allowed_characters(self): ''' diff --git a/test/tracecontext/tracestate.py b/test/tracecontext/tracestate.py index 9aab8bfb..040e9503 100644 --- a/test/tracecontext/tracestate.py +++ b/test/tracecontext/tracestate.py @@ -56,9 +56,11 @@ def from_string(self, string): if not match: raise ValueError('illegal key-value format {!r}'.format(member)) key, eq, value = match.groups() - if key in self._traits: - raise ValueError('conflict key {!r}'.format(key)) - self._traits[key] = value + if key not in self._traits: + self._traits[key] = value + # If key is already in self._traits, the incoming tracestate header contained a duplicated key. + # According to the spec, two behaviors are valid: Either pass on the duplicated key as-is or drop + # it. We opt for dropping it. return self def to_string(self):