Skip to content

Commit

Permalink
Make http-api:testFixtures usable in other modules (#1570)
Browse files Browse the repository at this point in the history
Motivation:

`testFixtures` are expected to be used by other modules.
After #1521 refactoring, visibility of some methods was reduced.

Modifications:

- Revert `protected` modifiers for `AbstractHttpRequesterFilterTest` and
`AbstractHttpServiceFilterTest` methods;
- Do not override `sslSession()` without a special need;

Result:

Modules that depend on these testFixtures classes can continue to work.
  • Loading branch information
idelpivnitskiy authored May 21, 2021
1 parent 3a20890 commit 253c34d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.servicetalk.concurrent.api.Single;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down Expand Up @@ -47,18 +46,6 @@
*/
public class SimpleHttpRequesterFilterTest extends AbstractHttpRequesterFilterTest {

private SSLSession session;

@BeforeEach
void setUp() {
session = mock(SSLSession.class);
}

@Override
protected SSLSession sslSession() {
return session;
}

private static final class HeaderEnrichingRequestFilter implements StreamingHttpClientFilterFactory,
StreamingHttpConnectionFilterFactory {
@Override
Expand Down Expand Up @@ -285,7 +272,7 @@ void unauthorizedConnectionRefusingFilterWithValidPrincipal(final RequesterType
setUp(security);
final Principal principal = mock(Principal.class);
lenient().when(principal.getName()).thenReturn("unit.test.auth");
lenient().when(session.getPeerPrincipal()).thenReturn(principal);
lenient().when(sslSession().getPeerPrincipal()).thenReturn(principal);

BlockingHttpRequester filter = asBlockingRequester(createFilter(type, new SecurityEnforcingFilter()));
HttpResponse resp = filter.request(defaultStrategy(), filter.get("/"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public enum SecurityType { Secure, Insecure }

public enum RequesterType { Client, Connection, ReservedConnection }

private final SSLSession sslSession = mock(SSLSession.class);
private final CompositeCloseable closeables = AsyncCloseables.newCompositeCloseable();


@Mock
private HttpExecutionContext mockExecutionContext;

Expand All @@ -82,7 +82,7 @@ protected static Stream<Arguments> requesterTypes() {
}

@BeforeEach
public final void setupContext() {
final void setupContext() {
lenient().when(mockConnectionContext.remoteAddress()).thenAnswer(__ -> remoteAddress());
lenient().when(mockConnectionContext.localAddress()).thenAnswer(__ -> localAddress());
}
Expand All @@ -105,20 +105,20 @@ protected InetSocketAddress remoteAddress() {
}

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
private InetSocketAddress localAddress() {
protected InetSocketAddress localAddress() {
return InetSocketAddress.createUnresolved("127.0.1.2", 28080);
}

protected SSLSession sslSession() {
return mock(SSLSession.class);
return sslSession;
}

protected Publisher<Object> loadbalancerEvents() {
return Publisher.empty();
}

@AfterEach
public final void closeClients() throws Exception {
final void closeClients() throws Exception {
closeables.close();
}

Expand All @@ -129,7 +129,7 @@ public final void closeClients() throws Exception {
* @param <FF> type capture for the filter factory
* @return a filtered {@link StreamingHttpRequester}
*/
final <FF extends StreamingHttpClientFilterFactory & StreamingHttpConnectionFilterFactory>
protected final <FF extends StreamingHttpClientFilterFactory & StreamingHttpConnectionFilterFactory>
StreamingHttpRequester createFilter(RequesterType type, FF filterFactory) {
return createFilter(type, RequestHandler.ok(), ok(), filterFactory);
}
Expand All @@ -156,7 +156,7 @@ StreamingHttpRequester createFilter(RequesterType type, RequestHandler rh, FF fi
* @param <FF> type capture for the filter factory
* @return a filtered {@link StreamingHttpRequester}
*/
final <FF extends StreamingHttpClientFilterFactory & StreamingHttpConnectionFilterFactory>
protected final <FF extends StreamingHttpClientFilterFactory & StreamingHttpConnectionFilterFactory>
StreamingHttpRequester createFilter(RequesterType type, RequestHandler rh,
RequestWithContextHandler rwch, FF filterFactory) {
switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

import static io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR;
import static io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* This parameterized test facilitates running HTTP service filter tests under all calling variations:
Expand All @@ -52,14 +52,14 @@ public enum SecurityType { Secure, Insecure }
private HttpServiceContext mockConnectionContext;

@BeforeEach
public void setUp() {
when(mockConnectionContext.executionContext()).thenReturn(executionContext);
when(mockConnectionContext.remoteAddress()).thenAnswer(__ -> remoteAddress());
when(mockConnectionContext.localAddress()).thenAnswer(__ -> localAddress());
void setUp() {
lenient().when(mockConnectionContext.executionContext()).thenReturn(executionContext);
lenient().when(mockConnectionContext.remoteAddress()).thenAnswer(__ -> remoteAddress());
lenient().when(mockConnectionContext.localAddress()).thenAnswer(__ -> localAddress());
}

protected void setUp(SecurityType security) {
when(mockConnectionContext.sslSession()).thenAnswer(__ -> {
lenient().when(mockConnectionContext.sslSession()).thenAnswer(__ -> {
switch (security) {
case Secure:
return sslSession();
Expand All @@ -71,16 +71,16 @@ protected void setUp(SecurityType security) {
}

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
private InetSocketAddress remoteAddress() {
protected InetSocketAddress remoteAddress() {
return InetSocketAddress.createUnresolved("127.0.1.2", 28080);
}

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
private InetSocketAddress localAddress() {
protected InetSocketAddress localAddress() {
return InetSocketAddress.createUnresolved("127.0.1.1", 80);
}

private SSLSession sslSession() {
protected SSLSession sslSession() {
return mock(SSLSession.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@
import io.servicetalk.transport.api.HostAndPort;

import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import javax.net.ssl.SSLSession;

import static io.servicetalk.concurrent.api.Single.succeeded;
import static io.servicetalk.http.api.HttpExecutionStrategies.noOffloadsStrategy;
import static io.servicetalk.http.api.HttpHeaderNames.HOST;
Expand All @@ -43,26 +40,13 @@
import static io.servicetalk.transport.netty.internal.AddressUtils.hostHeader;
import static java.lang.String.format;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;

/**
* This test-case is for integration testing the {@link RedirectingHttpRequesterFilter} with the various types
* of {@link HttpClient} and {@link HttpConnection} builders.
*/
public final class RedirectingClientAndConnectionFilterTest extends AbstractHttpRequesterFilterTest {

private SSLSession session;

@BeforeEach
void setUp() {
session = mock(SSLSession.class);
}

@Override
protected SSLSession sslSession() {
return session;
}

@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterNoHostHeaderRelativeLocation(final RequesterType type,
Expand Down

0 comments on commit 253c34d

Please sign in to comment.