|
| 1 | +/* |
| 2 | + * Copyright 2020 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.incubator.codec.http3; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class Http3HeadersSinkTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testHeaderSizeExceeded() { |
| 25 | + Http3HeadersSink sink = new Http3HeadersSink(new DefaultHttp3Headers(), 32, false); |
| 26 | + sink.accept("name", "value"); |
| 27 | + sink.accept("someothername", "someothervalue"); |
| 28 | + |
| 29 | + try { |
| 30 | + sink.finish(); |
| 31 | + Assert.fail(); |
| 32 | + } catch (Http3Exception e) { |
| 33 | + Http3TestUtils.assertException(Http3ErrorCode.H3_EXCESSIVE_LOAD, e); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testHeaderSizeNotExceed() throws Exception { |
| 39 | + Http3Headers headers = new DefaultHttp3Headers(); |
| 40 | + Http3HeadersSink sink = new Http3HeadersSink(headers, 64, false); |
| 41 | + sink.accept("name", "value"); |
| 42 | + Assert.assertEquals("value", headers.get("name")); |
| 43 | + sink.finish(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test(expected = Http3HeadersValidationException.class) |
| 47 | + public void testPseudoHeaderFollowsNormalHeader() throws Exception { |
| 48 | + Http3HeadersSink sink = new Http3HeadersSink(new DefaultHttp3Headers(), 512, true); |
| 49 | + sink.accept("name", "value"); |
| 50 | + sink.accept(Http3Headers.PseudoHeaderName.AUTHORITY.value(), "value"); |
| 51 | + sink.finish(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test(expected = Http3HeadersValidationException.class) |
| 55 | + public void testInvalidatePseudoHeader() throws Exception { |
| 56 | + Http3HeadersSink sink = new Http3HeadersSink(new DefaultHttp3Headers(), 512, true); |
| 57 | + sink.accept(":invalid", "value"); |
| 58 | + sink.finish(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test(expected = Http3HeadersValidationException.class) |
| 62 | + public void testMixRequestResponsePseudoHeaders() throws Exception { |
| 63 | + Http3HeadersSink sink = new Http3HeadersSink(new DefaultHttp3Headers(), 512, true); |
| 64 | + sink.accept(Http3Headers.PseudoHeaderName.METHOD.value(), "value"); |
| 65 | + sink.accept(Http3Headers.PseudoHeaderName.STATUS.value(), "value"); |
| 66 | + sink.finish(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testValidPseudoHeader() throws Exception { |
| 71 | + Http3Headers headers = new DefaultHttp3Headers(); |
| 72 | + Http3HeadersSink sink = new Http3HeadersSink(headers, 512, true); |
| 73 | + sink.accept(Http3Headers.PseudoHeaderName.AUTHORITY.value(), "value"); |
| 74 | + sink.finish(); |
| 75 | + Assert.assertEquals("value", headers.get(Http3Headers.PseudoHeaderName.AUTHORITY.value())); |
| 76 | + } |
| 77 | +} |