|
| 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; |
| 20 | + |
| 21 | +import io.undertow.testutils.DefaultServer; |
| 22 | +import io.undertow.testutils.HttpClientUtils; |
| 23 | +import io.undertow.testutils.TestHttpClient; |
| 24 | +import io.undertow.util.Headers; |
| 25 | +import io.undertow.util.StatusCodes; |
| 26 | +import org.apache.http.HttpResponse; |
| 27 | +import org.apache.http.client.methods.HttpHead; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test that {@code HEAD} requests can be used with a blocking exchange, setting response |
| 37 | + * content-length without unnecessarily writing bytes. |
| 38 | + * |
| 39 | + * @author Carter Kozak |
| 40 | + */ |
| 41 | +@RunWith(DefaultServer.class) |
| 42 | +public class HeadBlockingExchangeTestCase { |
| 43 | + |
| 44 | + @BeforeClass |
| 45 | + public static void setup() { |
| 46 | + DefaultServer.setRootHandler(new BlockingHandler( |
| 47 | + exchange -> exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + 100))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void sendHttpHead() throws IOException { |
| 52 | + HttpHead head = new HttpHead(DefaultServer.getDefaultServerURL()); |
| 53 | + TestHttpClient client = new TestHttpClient(); |
| 54 | + try { |
| 55 | + HttpResponse result = client.execute(head); |
| 56 | + Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); |
| 57 | + Assert.assertEquals("", HttpClientUtils.readResponse(result)); |
| 58 | + Assert.assertEquals("100", result.getFirstHeader("Content-Length").getValue()); |
| 59 | + } finally { |
| 60 | + client.getConnectionManager().shutdown(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments