private void testHttpConnectionNonAsync(long sleepBeforeResponse) throws IOException, InterruptedException { ServerHttpRequest request = mock(ServerHttpRequest.class); given(request.getAsyncRequestControl(this.response)) .willThrow(new IllegalArgumentException()); final HttpConnection connection = new HttpConnection(request, this.response); final AtomicBoolean responded = new AtomicBoolean(); Thread connectionThread = new Thread() { @Override public void run() { connection.waitForResponse(); responded.set(true); } }; connectionThread.start(); assertThat(responded.get()).isFalse(); Thread.sleep(sleepBeforeResponse); connection.respond(HttpStatus.NO_CONTENT); connectionThread.join(); assertThat(responded.get()).isTrue(); }
private void testHttpConnectionNonAsync(long sleepBeforeResponse) throws IOException, InterruptedException { ServerHttpRequest request = mock(ServerHttpRequest.class); given(request.getAsyncRequestControl(this.response)) .willThrow(new IllegalArgumentException()); final HttpConnection connection = new HttpConnection(request, this.response); final AtomicBoolean responded = new AtomicBoolean(); Thread connectionThread = new Thread() { @Override public void run() { connection.waitForResponse(); responded.set(true); } }; connectionThread.start(); assertThat(responded.get(), equalTo(false)); Thread.sleep(sleepBeforeResponse); connection.respond(HttpStatus.NO_CONTENT); connectionThread.join(); assertThat(responded.get(), equalTo(true)); }
@Test public void httpConnectionRespondWithPayload() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); connection.waitForResponse(); connection.respond(new HttpTunnelPayload(1, ByteBuffer.wrap("hello".getBytes()))); assertThat(this.servletResponse.getStatus()).isEqualTo(200); assertThat(this.servletResponse.getContentAsString()).isEqualTo("hello"); assertThat(this.servletResponse.getHeader(SEQ_HEADER)).isEqualTo("1"); }
@Test public void httpConnectionRespondWithStatus() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); connection.waitForResponse(); connection.respond(HttpStatus.I_AM_A_TEAPOT); assertThat(this.servletResponse.getStatus()).isEqualTo(418); assertThat(this.servletResponse.getContentLength()).isEqualTo(0); }
@Test public void httpConnectionAsync() throws Exception { ServerHttpAsyncRequestControl async = mock(ServerHttpAsyncRequestControl.class); ServerHttpRequest request = mock(ServerHttpRequest.class); given(request.getAsyncRequestControl(this.response)).willReturn(async); HttpConnection connection = new HttpConnection(request, this.response); connection.waitForResponse(); verify(async).start(); connection.respond(HttpStatus.NO_CONTENT); verify(async).complete(); }
@Test public void httpConnectionRunning() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); assertThat(connection.isOlderThan(100)).isFalse(); Thread.sleep(200); assertThat(connection.isOlderThan(100)).isTrue(); }
@Test public void httpConnectionRespondWithPayload() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); connection.waitForResponse(); connection.respond(new HttpTunnelPayload(1, ByteBuffer.wrap("hello".getBytes()))); assertThat(this.servletResponse.getStatus(), equalTo(200)); assertThat(this.servletResponse.getContentAsString(), equalTo("hello")); assertThat(this.servletResponse.getHeader(SEQ_HEADER), equalTo("1")); }
@Test public void httpConnectionRespondWithStatus() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); connection.waitForResponse(); connection.respond(HttpStatus.I_AM_A_TEAPOT); assertThat(this.servletResponse.getStatus(), equalTo(418)); assertThat(this.servletResponse.getContentLength(), equalTo(0)); }
@Test public void httpConnectionRunning() throws Exception { HttpConnection connection = new HttpConnection(this.request, this.response); assertThat(connection.isOlderThan(100), equalTo(false)); Thread.sleep(200); assertThat(connection.isOlderThan(100), equalTo(true)); }