PerMessageDeflateExtension extends CompressExtension { @Override public void setConfig(final ExtensionConfig config) { configRequested = new ExtensionConfig(config); configNegotiated = new ExtensionConfig(config.getName()); for (String key : config.getParameterKeys()) { key = key.trim(); switch (key) { case "client_max_window_bits": case "server_max_window_bits": { break; } case "client_no_context_takeover": { configNegotiated.setParameter("client_no_context_takeover"); switch (getPolicy().getBehavior()) { case CLIENT: incomingContextTakeover = false; break; case SERVER: outgoingContextTakeover = false; break; } break; } case "server_no_context_takeover": { configNegotiated.setParameter("server_no_context_takeover"); switch (getPolicy().getBehavior()) { case CLIENT: outgoingContextTakeover = false; break; case SERVER: incomingContextTakeover = false; break; } break; } default: { throw new IllegalArgumentException(); } } } LOG.debug("config: outgoingContextTakeover={}, incomingContextTakeover={} : {}", outgoingContextTakeover, incomingContextTakeover, this); super.setConfig(configNegotiated); }  @Override String getName(); @Override void incomingFrame(Frame frame); @Override void setConfig(final ExtensionConfig config); @Override String toString();  }
@Test public void testOutgoingPing() throws IOException { PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.setConfig(config); OutgoingFramesCapture capture = new OutgoingFramesCapture(); ext.setNextOutgoingFrames(capture); String payload = "Are you there?"; Frame ping = new PingFrame().setPayload(payload); ext.outgoingFrame(ping, null); capture.assertFrameCount(1); capture.assertHasFrame(OpCode.PING, 1); WebSocketFrame actual = capture.getFrames().getFirst(); assertEquals(OpCode.PING, actual.getOpCode()); assertTrue(actual.isFin()); assertFalse(actual.isRsv1()); assertFalse(actual.isRsv2()); assertFalse(actual.isRsv3()); ByteBuffer expected = BufferUtils.toBuffer(payload, StandardCharsets.UTF_8); assertEquals(expected.remaining(), actual.getPayloadLength()); ByteBufferAssert.assertEquals("Frame.payload", expected, actual.getPayload().slice()); }
@Test public void testOutgoingFragmentedMessage() throws InterruptedException { PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.setConfig(config); OutgoingFramesCapture capture = new OutgoingFramesCapture(); ext.setNextOutgoingFrames(capture); Frame txtFrame = new TextFrame().setPayload("Hello ").setFin(false); Frame con1Frame = new ContinuationFrame().setPayload("World").setFin(false); Frame con2Frame = new ContinuationFrame().setPayload("!").setFin(true); ext.outgoingFrame(txtFrame, null); ext.outgoingFrame(con1Frame, null); ext.outgoingFrame(con2Frame, null); capture.assertFrameCount(3); WebSocketFrame capturedFrame; capturedFrame = capture.getFrames().poll(1, TimeUnit.SECONDS); assertEquals(OpCode.TEXT, capturedFrame.getOpCode()); assertFalse(capturedFrame.isFin()); assertTrue(capturedFrame.isRsv1()); assertFalse(capturedFrame.isRsv2()); assertFalse(capturedFrame.isRsv3()); capturedFrame = capture.getFrames().poll(1, TimeUnit.SECONDS); assertEquals(OpCode.CONTINUATION, capturedFrame.getOpCode()); assertFalse(capturedFrame.isFin()); assertFalse(capturedFrame.isRsv1()); assertFalse(capturedFrame.isRsv2()); assertFalse(capturedFrame.isRsv3()); capturedFrame = capture.getFrames().poll(1, TimeUnit.SECONDS); assertEquals(OpCode.CONTINUATION, capturedFrame.getOpCode()); assertTrue(capturedFrame.isFin()); assertFalse(capturedFrame.isRsv1()); assertFalse(capturedFrame.isRsv2()); assertFalse(capturedFrame.isRsv3()); }