def test_write_header_given_fcontext(self): ctx = FContext("corrId") # Manually set the op id to avoid changes to global op id ctx._set_op_id(0) expected = bytearray(b'\x00\x00\x00\x00 \x00\x00\x00\x05_opid\x00\x00' b'\x00\x010\x00\x00\x00\x04_cid\x00\x00\x00\x06' b'corrId\x00\x00\x00\x08_timeout\x00\x00\x00' b'\x044000') buff = self.headers._write_to_bytearray(ctx.get_request_headers()) self.assertEqual(len(expected), len(buff))
def test_write_request_headers(self, mock_write): context = FContext("foo") mock_write.return_value = "bar" mock_trans = mock.Mock() self.protocol.trans = mock_trans self.protocol.write_request_headers(context) mock_write.assert_called_with(context.get_request_headers()) mock_trans.write.assert_called_with("bar")
def test_write_read(self): context = FContext("corrId") context.set_request_header("foo", "bar") expected = context.get_request_headers() buff = self.headers._write_to_bytearray(expected) actual = self.headers._read(BytesIO(buff)) self.assertEqual(expected["_opid"], actual["_opid"]) self.assertEqual(expected["_cid"], actual["_cid"]) self.assertEqual(expected["foo"], actual["foo"])
def test_copy(self): ctx = FContext() ctx.set_request_header('foo', 'bar') copied = ctx.copy() ctxHeaders = ctx.get_request_headers() copiedHeaders = copied.get_request_headers() # Should not have the same opid self.assertNotEqual(ctxHeaders[_OPID_HEADER], copiedHeaders[_OPID_HEADER]) # Everything else should be the same del ctxHeaders[_OPID_HEADER] del copiedHeaders[_OPID_HEADER] self.assertEqual(ctxHeaders, copiedHeaders) # Modifying the originals headers shouldn't affect the clone ctx.set_request_header('baz', 'qux') self.assertIsNone(copied.get_request_header('baz'))
def test_request_headers(self): context = FContext(self.correlation_id) context.set_request_header("foo", "bar") headers = context.get_request_headers() self.assertEqual("bar", headers.get('foo'))