Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.core5.http2.impl.nio;

import java.nio.ByteBuffer;

import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
import org.apache.hc.core5.http.impl.BasicHttpTransportMetrics;
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.HandlerFactory;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class TestClientH2StreamHandler {

private ClientH2StreamHandler newHandler() {
final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
final AsyncClientExchangeHandler exchangeHandler = Mockito.mock(AsyncClientExchangeHandler.class);
@SuppressWarnings("unchecked")
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
(HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
return new ClientH2StreamHandler(channel, httpProcessor, metrics, exchangeHandler, pushHandlerFactory,
HttpCoreContext.create());
}

@Test
void defaults() {
final ClientH2StreamHandler handler = newHandler();
Assertions.assertNotNull(handler.getPushHandlerFactory());
Assertions.assertTrue(handler.isOutputReady());
}

@Test
void consumePromiseRejected() {
final ClientH2StreamHandler handler = newHandler();
Assertions.assertThrows(ProtocolException.class, () -> handler.consumePromise(null));
}

@Test
void consumeDataRejectedBeforeHeaders() {
final ClientH2StreamHandler handler = newHandler();
Assertions.assertThrows(ProtocolException.class, () ->
handler.consumeData(ByteBuffer.allocate(0), true));
}

@Test
void updateCapacityDelegates() throws Exception {
final H2StreamChannel channel = Mockito.mock(H2StreamChannel.class);
final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
final BasicHttpConnectionMetrics metrics = new BasicHttpConnectionMetrics(
new BasicHttpTransportMetrics(), new BasicHttpTransportMetrics());
final AsyncClientExchangeHandler exchangeHandler = Mockito.mock(AsyncClientExchangeHandler.class);
@SuppressWarnings("unchecked")
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
(HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
final ClientH2StreamHandler handler = new ClientH2StreamHandler(
channel, httpProcessor, metrics, exchangeHandler, pushHandlerFactory, HttpCoreContext.create());

handler.updateInputCapacity();

Mockito.verify(exchangeHandler).updateCapacity(channel);
}

@Test
void toStringIncludesStates() {
final ClientH2StreamHandler handler = newHandler();
final String text = handler.toString();
Assertions.assertTrue(text.contains("requestState"));
Assertions.assertTrue(text.contains("responseState"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.core5.http2.impl.nio;

import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.AsyncPushProducer;
import org.apache.hc.core5.http.nio.HandlerFactory;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.http2.H2ConnectionException;
import org.apache.hc.core5.http2.H2Error;
import org.apache.hc.core5.http2.config.H2Config;
import org.apache.hc.core5.http2.config.H2Param;
import org.apache.hc.core5.http2.frame.DefaultFrameFactory;
import org.apache.hc.core5.reactor.ProtocolIOSession;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class TestClientH2StreamMultiplexer {

private ClientH2StreamMultiplexer newMultiplexer() {
final ProtocolIOSession ioSession = Mockito.mock(ProtocolIOSession.class);
final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
@SuppressWarnings("unchecked")
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
(HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
return new ClientH2StreamMultiplexer(
ioSession,
DefaultFrameFactory.INSTANCE,
httpProcessor,
pushHandlerFactory,
H2Config.DEFAULT,
null,
null);
}

@Test
void validateSettingRejectsEnablePush() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
final H2ConnectionException ex = Assertions.assertThrows(H2ConnectionException.class, () ->
multiplexer.validateSetting(H2Param.ENABLE_PUSH, 1));
Assertions.assertEquals(H2Error.PROTOCOL_ERROR.getCode(), ex.getCode());
}

@Test
void acceptHeaderFrameRejected() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
Assertions.assertThrows(H2ConnectionException.class, multiplexer::acceptHeaderFrame);
}

@Test
void outgoingRequestCreatesHandler() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
final H2StreamHandler handler = multiplexer.outgoingRequest(
Mockito.mock(H2StreamChannel.class),
Mockito.mock(AsyncClientExchangeHandler.class),
null,
null);
Assertions.assertNotNull(handler);
}

@Test
void incomingRequestRejected() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
Assertions.assertThrows(H2ConnectionException.class, () ->
multiplexer.incomingRequest(Mockito.mock(H2StreamChannel.class)));
}

@Test
void outgoingPushPromiseRejected() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
Assertions.assertThrows(H2ConnectionException.class, () ->
multiplexer.outgoingPushPromise(Mockito.mock(H2StreamChannel.class), Mockito.mock(AsyncPushProducer.class)));
}

@Test
void incomingPushPromiseCreatesHandler() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
@SuppressWarnings("unchecked")
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
(HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
final H2StreamHandler handler = multiplexer.incomingPushPromise(
Mockito.mock(H2StreamChannel.class),
pushHandlerFactory);
Assertions.assertNotNull(handler);
}

@Test
void allowGracefulAbortUsesRemoteClosed() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
final H2Stream stream = Mockito.mock(H2Stream.class);
Mockito.when(stream.isRemoteClosed()).thenReturn(true);
Mockito.when(stream.isLocalClosed()).thenReturn(false);
Assertions.assertTrue(multiplexer.allowGracefulAbort(stream));
}

@Test
void toStringContainsState() {
final ClientH2StreamMultiplexer multiplexer = newMultiplexer();
final String text = multiplexer.toString();
Assertions.assertTrue(text.startsWith("["));
Assertions.assertTrue(text.endsWith("]"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.core5.http2.impl.nio;

import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.HandlerFactory;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.reactor.ProtocolIOSession;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class TestClientH2StreamMultiplexerFactory {

@Test
void createUsesDefaults() {
final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
@SuppressWarnings("unchecked")
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
(HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
final ClientH2StreamMultiplexerFactory factory = new ClientH2StreamMultiplexerFactory(
httpProcessor, pushHandlerFactory, null, null, null);

final ProtocolIOSession ioSession = Mockito.mock(ProtocolIOSession.class);
final ClientH2StreamMultiplexer multiplexer = factory.create(ioSession);

Assertions.assertNotNull(multiplexer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.core5.http2.impl.nio;

import java.util.concurrent.locks.ReentrantLock;

import org.apache.hc.core5.function.Callback;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.HandlerFactory;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.reactor.ProtocolIOSession;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class TestClientH2UpgradeHandler {

@Test
void upgradeRegistersPrefaceHandler() throws Exception {
final HttpProcessor httpProcessor = Mockito.mock(HttpProcessor.class);
@SuppressWarnings("unchecked")
final HandlerFactory<AsyncPushConsumer> pushHandlerFactory =
(HandlerFactory<AsyncPushConsumer>) Mockito.mock(HandlerFactory.class);
final ClientH2StreamMultiplexerFactory factory = new ClientH2StreamMultiplexerFactory(
httpProcessor, pushHandlerFactory, null, null, null);

final ProtocolIOSession ioSession = Mockito.mock(ProtocolIOSession.class);
Mockito.when(ioSession.getLock()).thenReturn(new ReentrantLock());

final ClientH2UpgradeHandler handler = new ClientH2UpgradeHandler(factory, (Callback<Exception>) ex -> {
});
handler.upgrade(ioSession, null);

Mockito.verify(ioSession).upgrade(Mockito.any());
}

}
Loading
Loading