/** * Use with care, typically {@link #toSend(String, RequestHeader)} should be used instead. */ public ByteBuffer serialize(RequestHeader header) { return serialize(header.toStruct(), toStruct()); }
@Test public void testUnexpectedRequestType() throws IOException { TransportLayer transportLayer = mock(TransportLayer.class); Map<String, ?> configs = Collections.singletonMap(BrokerSecurityConfigs.SASL_ENABLED_MECHANISMS_CONFIG, Collections.singletonList(SCRAM_SHA_256.mechanismName())); SaslServerAuthenticator authenticator = setupAuthenticator(configs, transportLayer, SCRAM_SHA_256.mechanismName()); final RequestHeader header = new RequestHeader(ApiKeys.METADATA, (short) 0, "clientId", 13243); final Struct headerStruct = header.toStruct(); when(transportLayer.read(any(ByteBuffer.class))).then(invocation -> { invocation.<ByteBuffer>getArgument(0).putInt(headerStruct.sizeOf()); return 4; }).then(invocation -> { // serialize only the request header. the authenticator should not parse beyond this headerStruct.writeTo(invocation.getArgument(0)); return headerStruct.sizeOf(); }); try { authenticator.authenticate(); fail("Expected authenticate() to raise an exception"); } catch (IllegalSaslStateException e) { // expected exception } verify(transportLayer, times(2)).read(any(ByteBuffer.class)); }
@Test public void testRequestHeader() { RequestHeader header = new RequestHeader(ApiKeys.FIND_COORDINATOR, (short) 1, "", 10); ByteBuffer buffer = toBuffer(header.toStruct()); RequestHeader deserialized = RequestHeader.parse(buffer); assertEquals(header, deserialized); }
@Test public void testSerdeControlledShutdownV0() { // Verify that version 0 of controlled shutdown does not include the clientId field int correlationId = 2342; ByteBuffer rawBuffer = ByteBuffer.allocate(32); rawBuffer.putShort(ApiKeys.CONTROLLED_SHUTDOWN.id); rawBuffer.putShort((short) 0); rawBuffer.putInt(correlationId); rawBuffer.flip(); RequestHeader deserialized = RequestHeader.parse(rawBuffer); assertEquals(ApiKeys.CONTROLLED_SHUTDOWN, deserialized.apiKey()); assertEquals(0, deserialized.apiVersion()); assertEquals(correlationId, deserialized.correlationId()); assertEquals("", deserialized.clientId()); Struct serialized = deserialized.toStruct(); ByteBuffer serializedBuffer = toBuffer(serialized); assertEquals(ApiKeys.CONTROLLED_SHUTDOWN.id, serializedBuffer.getShort(0)); assertEquals(0, serializedBuffer.getShort(2)); assertEquals(correlationId, serializedBuffer.getInt(4)); assertEquals(8, serializedBuffer.limit()); }
@Test public void testRequestHeaderWithNullClientId() { RequestHeader header = new RequestHeader(ApiKeys.FIND_COORDINATOR, (short) 1, null, 10); Struct headerStruct = header.toStruct(); ByteBuffer buffer = toBuffer(headerStruct); RequestHeader deserialized = RequestHeader.parse(buffer); assertEquals(header.apiKey(), deserialized.apiKey()); assertEquals(header.apiVersion(), deserialized.apiVersion()); assertEquals(header.correlationId(), deserialized.correlationId()); assertEquals("", deserialized.clientId()); // null defaults to "" }