|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://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 |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.handler.codec.http; |
| 17 | + |
| 18 | +import io.netty.buffer.Unpooled; |
| 19 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.function.Executable; |
| 22 | + |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 30 | + |
| 31 | +public class HttpMethodTest { |
| 32 | + |
| 33 | + private static final String NUL = String.valueOf((char) 0x00); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void valueOfReturnsCachedInstanceForKnownMethods() { |
| 37 | + assertSame(HttpMethod.GET, HttpMethod.valueOf("GET")); |
| 38 | + assertSame(HttpMethod.POST, HttpMethod.valueOf("POST")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void constructorAcceptsCustomMethodName() { |
| 43 | + HttpMethod custom = new HttpMethod("CUSTOM"); |
| 44 | + assertEquals("CUSTOM", custom.name()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void constructorTrimsLeadingAndTrailingSpaces() { |
| 49 | + HttpMethod method = new HttpMethod(" GET "); |
| 50 | + assertEquals("GET", method.name()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void constructorTrimsLeadingAndTrailingTabs() { |
| 55 | + HttpMethod method = new HttpMethod("\tGET\t"); |
| 56 | + assertEquals("GET", method.name()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void constructorRejectsLeadingNul() { |
| 61 | + assertThrows(IllegalArgumentException.class, newInstance(NUL + "GET")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void constructorRejectsTrailingNul() { |
| 66 | + assertThrows(IllegalArgumentException.class, newInstance("GET" + NUL)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void constructorRejectsLeadingAndTrailingNul() { |
| 71 | + assertThrows(IllegalArgumentException.class, newInstance(NUL + "GET" + NUL)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void constructorRejectsEmbeddedNul() { |
| 76 | + assertThrows(IllegalArgumentException.class, newInstance("GE" + NUL + "T")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void constructorRejectsCarriageReturn() { |
| 81 | + assertThrows(IllegalArgumentException.class, newInstance("GET\r")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void constructorRejectsLineFeed() { |
| 86 | + assertThrows(IllegalArgumentException.class, newInstance("GET\n")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void constructorRejectsVerticalTab() { |
| 91 | + assertThrows(IllegalArgumentException.class, newInstance("GET" + (char) 0x0B)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void constructorRejectsFormFeed() { |
| 96 | + assertThrows(IllegalArgumentException.class, newInstance("GET\f")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void constructorRejectsEmbeddedSpace() { |
| 101 | + assertThrows(IllegalArgumentException.class, newInstance("GE T")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void constructorRejectsEmptyString() { |
| 106 | + assertThrows(IllegalArgumentException.class, newInstance("")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void constructorRejectsBlankString() { |
| 111 | + assertThrows(IllegalArgumentException.class, newInstance(" ")); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void requestDecoderRejectsNulPaddedMethod() { |
| 116 | + // RFC 7230 forbids any non-token character in the method. NUL-padded methods are |
| 117 | + // a known request-smuggling vector if silently stripped, so the decoder must |
| 118 | + // surface a decoder failure rather than producing a valid GET message. |
| 119 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder()); |
| 120 | + byte[] data = (NUL + "GET" + NUL + " / HTTP/1.1\r\nHost: x\r\n\r\n") |
| 121 | + .getBytes(StandardCharsets.US_ASCII); |
| 122 | + ch.writeInbound(Unpooled.wrappedBuffer(data)); |
| 123 | + HttpRequest req = ch.readInbound(); |
| 124 | + assertNotNull(req); |
| 125 | + assertFalse(req.decoderResult().isSuccess(), |
| 126 | + "decoder must reject method names containing NUL bytes"); |
| 127 | + ch.finishAndReleaseAll(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void requestDecoderAcceptsCleanMethod() { |
| 132 | + // Regression: ordinary GET requests must still parse normally. |
| 133 | + EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder()); |
| 134 | + byte[] data = "GET / HTTP/1.1\r\nHost: x\r\n\r\n".getBytes(StandardCharsets.US_ASCII); |
| 135 | + ch.writeInbound(Unpooled.wrappedBuffer(data)); |
| 136 | + HttpRequest req = ch.readInbound(); |
| 137 | + assertNotNull(req); |
| 138 | + assertEquals(HttpMethod.GET, req.method()); |
| 139 | + ch.finishAndReleaseAll(); |
| 140 | + } |
| 141 | + |
| 142 | + private static Executable newInstance(final String name) { |
| 143 | + return new Executable() { |
| 144 | + @Override |
| 145 | + public void execute() { |
| 146 | + new HttpMethod(name); |
| 147 | + } |
| 148 | + }; |
| 149 | + } |
| 150 | +} |