forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate pong frames in WebSocketProtocolHandler (netty#7955)
Motivation: Currently, on recipt of a PongWebSocketFrame, the WebSocketProtocolHandler will drop the frame, rather than passing it along so it can be referenced by other handlers. Modifications: Add boolean field to WebSocketProtocolHandler to indicate whether Pong frames should be dropped or propagated, defaulting to "true" to preserve existing functionality. Add new constructors to the client and server implementations of WebSocketProtocolHandler that allow for overriding the behavior for the handling of Pong frames. Result: PongWebSocketFrames are passed along the channel, if specified.
- Loading branch information
1 parent
4d6b006
commit 19d1f4e
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...tp/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2018 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.netty.handler.codec.http.websocketx; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
import io.netty.util.CharsetUtil; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Tests common, abstract class functionality in {@link WebSocketClientProtocolHandler}. | ||
*/ | ||
public class WebSocketProtocolHandlerTest { | ||
|
||
@Test | ||
public void testPingFrame() { | ||
ByteBuf pingData = Unpooled.copiedBuffer("Hello, world", CharsetUtil.UTF_8); | ||
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketProtocolHandler() { }); | ||
|
||
PingWebSocketFrame inputMessage = new PingWebSocketFrame(pingData); | ||
assertFalse(channel.writeInbound(inputMessage)); // the message was not propagated inbound | ||
|
||
// a Pong frame was written to the channel | ||
PongWebSocketFrame response = channel.readOutbound(); | ||
assertEquals(pingData, response.content()); | ||
|
||
pingData.release(); | ||
assertFalse(channel.finish()); | ||
} | ||
|
||
@Test | ||
public void testPongFrameDropFrameFalse() { | ||
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketProtocolHandler(false) { }); | ||
|
||
PongWebSocketFrame pingResponse = new PongWebSocketFrame(); | ||
assertTrue(channel.writeInbound(pingResponse)); | ||
|
||
assertPropagatedInbound(pingResponse, channel); | ||
|
||
pingResponse.release(); | ||
assertFalse(channel.finish()); | ||
} | ||
|
||
@Test | ||
public void testPongFrameDropFrameTrue() { | ||
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketProtocolHandler(true) { }); | ||
|
||
PongWebSocketFrame pingResponse = new PongWebSocketFrame(); | ||
assertFalse(channel.writeInbound(pingResponse)); // message was not propagated inbound | ||
} | ||
|
||
@Test | ||
public void testTextFrame() { | ||
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketProtocolHandler() { }); | ||
|
||
TextWebSocketFrame textFrame = new TextWebSocketFrame(); | ||
assertTrue(channel.writeInbound(textFrame)); | ||
|
||
assertPropagatedInbound(textFrame, channel); | ||
|
||
textFrame.release(); | ||
assertFalse(channel.finish()); | ||
} | ||
|
||
/** | ||
* Asserts that a message was propagated inbound through the channel. | ||
*/ | ||
private static <T extends WebSocketFrame> void assertPropagatedInbound(T message, EmbeddedChannel channel) { | ||
T propagatedResponse = channel.readInbound(); | ||
assertEquals(message, propagatedResponse); | ||
} | ||
} |