|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2022 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.undertow.server.handlers.blocking; |
| 20 | + |
| 21 | +import io.undertow.server.handlers.BlockingHandler; |
| 22 | +import io.undertow.testutils.DefaultServer; |
| 23 | +import io.undertow.testutils.HttpClientUtils; |
| 24 | +import io.undertow.testutils.TestHttpClient; |
| 25 | +import io.undertow.util.Headers; |
| 26 | +import io.undertow.util.StatusCodes; |
| 27 | +import org.apache.http.HttpResponse; |
| 28 | +import org.apache.http.client.methods.HttpGet; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.OutputStream; |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Carter Kozak |
| 39 | + */ |
| 40 | +@RunWith(DefaultServer.class) |
| 41 | +public class BlockingServerStreamClosureTestCase { |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testFlushAfterContentLengthReached() throws IOException { |
| 45 | + AtomicReference<Throwable> thrown = new AtomicReference<>(); |
| 46 | + DefaultServer.setRootHandler(new BlockingHandler(exchange -> { |
| 47 | + exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "1"); |
| 48 | + try { |
| 49 | + OutputStream out = exchange.getOutputStream(); |
| 50 | + out.write(65); |
| 51 | + out.flush(); |
| 52 | + out.close(); |
| 53 | + } catch (Throwable t) { |
| 54 | + thrown.set(t); |
| 55 | + throw t; |
| 56 | + } |
| 57 | + })); |
| 58 | + makeSuccessfulRequest("A"); |
| 59 | + Throwable maybeFailure = thrown.get(); |
| 60 | + if (maybeFailure != null) { |
| 61 | + throw new AssertionError("Unexpected failure", maybeFailure); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testEmptyWriteAfterContentLength() throws IOException { |
| 67 | + AtomicReference<Throwable> thrown = new AtomicReference<>(); |
| 68 | + DefaultServer.setRootHandler(new BlockingHandler(exchange -> { |
| 69 | + exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "1"); |
| 70 | + try { |
| 71 | + OutputStream out = exchange.getOutputStream(); |
| 72 | + out.write(65); |
| 73 | + out.write(new byte[10], 2, 0); |
| 74 | + out.close(); |
| 75 | + } catch (Throwable t) { |
| 76 | + thrown.set(t); |
| 77 | + throw t; |
| 78 | + } |
| 79 | + })); |
| 80 | + makeSuccessfulRequest("A"); |
| 81 | + Throwable maybeFailure = thrown.get(); |
| 82 | + if (maybeFailure != null) { |
| 83 | + throw new AssertionError("Unexpected failure", maybeFailure); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testFlushAfterClose() throws IOException { |
| 89 | + AtomicReference<Throwable> thrown = new AtomicReference<>(); |
| 90 | + DefaultServer.setRootHandler(new BlockingHandler(exchange -> { |
| 91 | + try { |
| 92 | + OutputStream out = exchange.getOutputStream(); |
| 93 | + out.write(65); |
| 94 | + out.close(); |
| 95 | + out.flush(); |
| 96 | + } catch (Throwable t) { |
| 97 | + thrown.set(t); |
| 98 | + throw t; |
| 99 | + } |
| 100 | + })); |
| 101 | + makeSuccessfulRequest("A"); |
| 102 | + Throwable maybeFailure = thrown.get(); |
| 103 | + if (maybeFailure != null) { |
| 104 | + throw new AssertionError("Unexpected failure", maybeFailure); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static void makeSuccessfulRequest(String expectedContent) throws IOException { |
| 109 | + TestHttpClient client = new TestHttpClient(); |
| 110 | + try { |
| 111 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL()); |
| 112 | + HttpResponse result = client.execute(get); |
| 113 | + Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); |
| 114 | + Assert.assertEquals(expectedContent, HttpClientUtils.readResponse(result)); |
| 115 | + } finally { |
| 116 | + client.getConnectionManager().shutdown(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments