PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/extensions/WebSocketExtensionTestUtil.java

https://gitlab.com/taichu/netty
Java | 129 lines | 87 code | 22 blank | 20 comment | 4 complexity | 6a1349b6945f26af52b78c42b614182b MD5 | raw file
  1. /*
  2. * Copyright 2012 The Netty Project
  3. *
  4. * The Netty Project licenses this file to you under the Apache License, version
  5. * 2.0 (the "License"); you may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package io.netty.handler.codec.http.websocketx.extensions;
  17. import java.util.List;
  18. import io.netty.handler.codec.http.HttpHeaderNames;
  19. import io.netty.handler.codec.http.HttpHeaderValues;
  20. import org.easymock.EasyMock;
  21. import org.easymock.IArgumentMatcher;
  22. import io.netty.channel.ChannelHandlerContext;
  23. import io.netty.handler.codec.http.DefaultHttpRequest;
  24. import io.netty.handler.codec.http.DefaultHttpResponse;
  25. import io.netty.handler.codec.http.websocketx.WebSocketFrame;
  26. import io.netty.handler.codec.http.HttpMethod;
  27. import io.netty.handler.codec.http.HttpRequest;
  28. import io.netty.handler.codec.http.HttpResponse;
  29. import io.netty.handler.codec.http.HttpResponseStatus;
  30. import io.netty.handler.codec.http.HttpVersion;
  31. import io.netty.util.ReferenceCountUtil;
  32. public final class WebSocketExtensionTestUtil {
  33. public static HttpRequest newUpgradeRequest(String ext) {
  34. HttpRequest req = ReferenceCountUtil.releaseLater(new DefaultHttpRequest(
  35. HttpVersion.HTTP_1_1, HttpMethod.GET, "/chat"));
  36. req.headers().set(HttpHeaderNames.HOST, "server.example.com");
  37. req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET.toString().toLowerCase());
  38. req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
  39. req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
  40. if (ext != null) {
  41. req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS, ext);
  42. }
  43. return req;
  44. }
  45. public static HttpResponse newUpgradeResponse(String ext) {
  46. HttpResponse res = ReferenceCountUtil.releaseLater(new DefaultHttpResponse(
  47. HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS));
  48. res.headers().set(HttpHeaderNames.HOST, "server.example.com");
  49. res.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET.toString().toLowerCase());
  50. res.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
  51. res.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
  52. if (ext != null) {
  53. res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS, ext);
  54. }
  55. return res;
  56. }
  57. public static WebSocketExtensionData webSocketExtensionDataEqual(String name) {
  58. EasyMock.reportMatcher(new WebSocketExtensionDataMatcher(name));
  59. return null;
  60. }
  61. public static class WebSocketExtensionDataMatcher implements IArgumentMatcher {
  62. private final String name;
  63. public WebSocketExtensionDataMatcher(String name) {
  64. this.name = name;
  65. }
  66. @Override
  67. public void appendTo(StringBuffer buf) {
  68. buf.append("WebSocketExtensionData with name=" + name);
  69. }
  70. @Override
  71. public boolean matches(Object o) {
  72. return o instanceof WebSocketExtensionData &&
  73. name.equals(((WebSocketExtensionData) o).name());
  74. }
  75. }
  76. private WebSocketExtensionTestUtil() {
  77. // unused
  78. }
  79. static class DummyEncoder extends WebSocketExtensionEncoder {
  80. @Override
  81. protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg,
  82. List<Object> out) throws Exception {
  83. // unused
  84. }
  85. }
  86. static class DummyDecoder extends WebSocketExtensionDecoder {
  87. @Override
  88. protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg,
  89. List<Object> out) throws Exception {
  90. // unused
  91. }
  92. }
  93. static class Dummy2Encoder extends WebSocketExtensionEncoder {
  94. @Override
  95. protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg,
  96. List<Object> out) throws Exception {
  97. // unused
  98. }
  99. }
  100. static class Dummy2Decoder extends WebSocketExtensionDecoder {
  101. @Override
  102. protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg,
  103. List<Object> out) throws Exception {
  104. // unused
  105. }
  106. }
  107. }