|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.core5.http2.impl.nio; |
| 28 | + |
| 29 | +import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler; |
| 30 | +import org.apache.hc.core5.http.nio.AsyncPushConsumer; |
| 31 | +import org.apache.hc.core5.http.nio.AsyncPushProducer; |
| 32 | +import org.apache.hc.core5.http.nio.HandlerFactory; |
| 33 | +import org.apache.hc.core5.http.protocol.HttpProcessor; |
| 34 | +import org.apache.hc.core5.http2.H2ConnectionException; |
| 35 | +import org.apache.hc.core5.http2.H2Error; |
| 36 | +import org.apache.hc.core5.http2.config.H2Config; |
| 37 | +import org.apache.hc.core5.http2.config.H2Param; |
| 38 | +import org.apache.hc.core5.http2.frame.DefaultFrameFactory; |
| 39 | +import org.apache.hc.core5.reactor.ProtocolIOSession; |
| 40 | +import org.junit.jupiter.api.Assertions; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.mockito.Mockito; |
| 43 | + |
| 44 | +class TestClientH2StreamMultiplexer { |
| 45 | + |
| 46 | + private ClientH2StreamMultiplexer newMultiplexer() { |
| 47 | + final ProtocolIOSession ioSession = Mockito.mock(ProtocolIOSession.class); |
| 48 | + final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class); |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + final HandlerFactory<AsyncPushConsumer> pushHandlerFactory = |
| 51 | + (HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class); |
| 52 | + return new ClientH2StreamMultiplexer( |
| 53 | + ioSession, |
| 54 | + DefaultFrameFactory.INSTANCE, |
| 55 | + httpProcessor, |
| 56 | + pushHandlerFactory, |
| 57 | + H2Config.DEFAULT, |
| 58 | + null, |
| 59 | + null); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void validateSettingRejectsEnablePush() { |
| 64 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 65 | + final H2ConnectionException ex = Assertions.assertThrows(H2ConnectionException.class, () -> |
| 66 | + multiplexer.validateSetting(H2Param.ENABLE_PUSH, 1)); |
| 67 | + Assertions.assertEquals(H2Error.PROTOCOL_ERROR.getCode(), ex.getCode()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void acceptHeaderFrameRejected() { |
| 72 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 73 | + Assertions.assertThrows(H2ConnectionException.class, multiplexer::acceptHeaderFrame); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void outgoingRequestCreatesHandler() { |
| 78 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 79 | + final H2StreamHandler handler = multiplexer.outgoingRequest( |
| 80 | + Mockito.mock(H2StreamChannel.class), |
| 81 | + Mockito.mock(AsyncClientExchangeHandler.class), |
| 82 | + null, |
| 83 | + null); |
| 84 | + Assertions.assertNotNull(handler); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void incomingRequestRejected() { |
| 89 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 90 | + Assertions.assertThrows(H2ConnectionException.class, () -> |
| 91 | + multiplexer.incomingRequest(Mockito.mock(H2StreamChannel.class))); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void outgoingPushPromiseRejected() { |
| 96 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 97 | + Assertions.assertThrows(H2ConnectionException.class, () -> |
| 98 | + multiplexer.outgoingPushPromise(Mockito.mock(H2StreamChannel.class), Mockito.mock(AsyncPushProducer.class))); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void incomingPushPromiseCreatesHandler() { |
| 103 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 104 | + @SuppressWarnings("unchecked") |
| 105 | + final HandlerFactory<AsyncPushConsumer> pushHandlerFactory = |
| 106 | + (HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class); |
| 107 | + final H2StreamHandler handler = multiplexer.incomingPushPromise( |
| 108 | + Mockito.mock(H2StreamChannel.class), |
| 109 | + pushHandlerFactory); |
| 110 | + Assertions.assertNotNull(handler); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void allowGracefulAbortUsesRemoteClosed() { |
| 115 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 116 | + final H2Stream stream = Mockito.mock(H2Stream.class); |
| 117 | + Mockito.when(stream.isRemoteClosed()).thenReturn(true); |
| 118 | + Mockito.when(stream.isLocalClosed()).thenReturn(false); |
| 119 | + Assertions.assertTrue(multiplexer.allowGracefulAbort(stream)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void toStringContainsState() { |
| 124 | + final ClientH2StreamMultiplexer multiplexer = newMultiplexer(); |
| 125 | + final String text = multiplexer.toString(); |
| 126 | + Assertions.assertTrue(text.startsWith("[")); |
| 127 | + Assertions.assertTrue(text.endsWith("]")); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments