@@ -317,6 +317,118 @@ void testNotUnescapeHeadersForConnectedCommand() { |
317 | 317 | assertNull(obj); |
318 | 318 | } |
319 | 319 |
|
| 320 | + @Test |
| 321 | + public void testHeartbeatOnlyDoesNotThrowException() { |
| 322 | + // STOMP heartbeat is just a LF byte - should not cause IndexOutOfBoundsException |
| 323 | + ByteBuf heartbeat = Unpooled.buffer(); |
| 324 | + heartbeat.writeByte('\n'); |
| 325 | + channel.writeInbound(heartbeat); |
| 326 | + |
| 327 | + // Heartbeat should be consumed silently, no output produced |
| 328 | + Object result = channel.readInbound(); |
| 329 | + assertNull(result); |
| 330 | + } |
| 331 | + |
| 332 | + @Test |
| 333 | + public void testMultipleHeartbeatsDoNotThrowException() { |
| 334 | + // Multiple consecutive heartbeats |
| 335 | + ByteBuf heartbeats = Unpooled.buffer(); |
| 336 | + heartbeats.writeByte('\n'); |
| 337 | + heartbeats.writeByte('\n'); |
| 338 | + heartbeats.writeByte('\n'); |
| 339 | + channel.writeInbound(heartbeats); |
| 340 | + |
| 341 | + Object result = channel.readInbound(); |
| 342 | + assertNull(result); |
| 343 | + } |
| 344 | + |
| 345 | + @Test |
| 346 | + public void testCarriageReturnLineFeedHeartbeat() { |
| 347 | + // CR+LF heartbeat |
| 348 | + ByteBuf heartbeat = Unpooled.buffer(); |
| 349 | + heartbeat.writeByte('\r'); |
| 350 | + heartbeat.writeByte('\n'); |
| 351 | + channel.writeInbound(heartbeat); |
| 352 | + |
| 353 | + Object result = channel.readInbound(); |
| 354 | + assertNull(result); |
| 355 | + } |
| 356 | + |
| 357 | + @Test |
| 358 | + public void testHeartbeatFollowedByFrame() { |
| 359 | + // Heartbeat bytes followed by a real STOMP frame should decode correctly |
| 360 | + ByteBuf incoming = Unpooled.buffer(); |
| 361 | + incoming.writeByte('\n'); |
| 362 | + incoming.writeByte('\n'); |
| 363 | + incoming.writeBytes(StompTestConstants.CONNECT_FRAME.getBytes()); |
| 364 | + channel.writeInbound(incoming); |
| 365 | + |
| 366 | + StompHeadersSubframe frame = channel.readInbound(); |
| 367 | + assertNotNull(frame); |
| 368 | + assertEquals(StompCommand.CONNECT, frame.command()); |
| 369 | + |
| 370 | + StompContentSubframe content = channel.readInbound(); |
| 371 | + assertSame(LastStompContentSubframe.EMPTY_LAST_CONTENT, content); |
| 372 | + content.release(); |
| 373 | + |
| 374 | + assertNull(channel.readInbound()); |
| 375 | + } |
| 376 | + |
| 377 | + @Test |
| 378 | + public void testHeartbeatBetweenFrames() { |
| 379 | + // Heartbeat bytes between two STOMP frames |
| 380 | + ByteBuf incoming = Unpooled.buffer(); |
| 381 | + incoming.writeBytes(StompTestConstants.CONNECT_FRAME.getBytes()); |
| 382 | + incoming.writeByte('\n'); |
| 383 | + incoming.writeByte('\n'); |
| 384 | + incoming.writeBytes(StompTestConstants.CONNECTED_FRAME.getBytes()); |
| 385 | + channel.writeInbound(incoming); |
| 386 | + |
| 387 | + StompHeadersSubframe frame1 = channel.readInbound(); |
| 388 | + assertNotNull(frame1); |
| 389 | + assertEquals(StompCommand.CONNECT, frame1.command()); |
| 390 | + |
| 391 | + StompContentSubframe content1 = channel.readInbound(); |
| 392 | + assertSame(LastStompContentSubframe.EMPTY_LAST_CONTENT, content1); |
| 393 | + content1.release(); |
| 394 | + |
| 395 | + StompHeadersSubframe frame2 = channel.readInbound(); |
| 396 | + assertNotNull(frame2); |
| 397 | + assertEquals(StompCommand.CONNECTED, frame2.command()); |
| 398 | + |
| 399 | + StompContentSubframe content2 = channel.readInbound(); |
| 400 | + assertSame(LastStompContentSubframe.EMPTY_LAST_CONTENT, content2); |
| 401 | + content2.release(); |
| 402 | + |
| 403 | + assertNull(channel.readInbound()); |
| 404 | + } |
| 405 | + |
| 406 | + @Test |
| 407 | + public void testHeartbeatSentSeparatelyThenFrame() { |
| 408 | + // Simulate heartbeat arriving in a separate TCP segment, then a frame later |
| 409 | + ByteBuf heartbeat = Unpooled.buffer(); |
| 410 | + heartbeat.writeByte('\n'); |
| 411 | + channel.writeInbound(heartbeat); |
| 412 | + |
| 413 | + // No output from heartbeat |
| 414 | + assertNull(channel.readInbound()); |
| 415 | + |
| 416 | + // Now send a real frame |
| 417 | + ByteBuf frame = Unpooled.buffer(); |
| 418 | + frame.writeBytes(StompTestConstants.CONNECT_FRAME.getBytes()); |
| 419 | + channel.writeInbound(frame); |
| 420 | + |
| 421 | + StompHeadersSubframe headersSubframe = channel.readInbound(); |
| 422 | + assertNotNull(headersSubframe); |
| 423 | + assertEquals(StompCommand.CONNECT, headersSubframe.command()); |
| 424 | + |
| 425 | + StompContentSubframe content = channel.readInbound(); |
| 426 | + assertSame(LastStompContentSubframe.EMPTY_LAST_CONTENT, content); |
| 427 | + content.release(); |
| 428 | + |
| 429 | + assertNull(channel.readInbound()); |
| 430 | + } |
| 431 | + |
320 | 432 | @Test |
321 | 433 | void testInvalidEscapeHeadersSequence() { |
322 | 434 | channel = new EmbeddedChannel(new StompSubframeDecoder(true)); |
|