1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.testing.nio.extension;
29
30 import java.util.function.Consumer;
31
32 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2MultiplexingRequester;
33 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2MultiplexingRequesterBootstrap;
34 import org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy;
35 import org.apache.hc.core5.io.CloseMode;
36 import org.apache.hc.core5.testing.SSLTestContexts;
37 import org.apache.hc.core5.testing.nio.LoggingExceptionCallback;
38 import org.apache.hc.core5.testing.nio.LoggingH2StreamListener;
39 import org.apache.hc.core5.testing.nio.LoggingIOSessionDecorator;
40 import org.apache.hc.core5.testing.nio.LoggingIOSessionListener;
41 import org.junit.jupiter.api.Assertions;
42 import org.junit.jupiter.api.extension.AfterEachCallback;
43 import org.junit.jupiter.api.extension.BeforeEachCallback;
44 import org.junit.jupiter.api.extension.ExtensionContext;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class H2MultiplexingRequesterResource implements BeforeEachCallback, AfterEachCallback {
49
50 private static final Logger LOG = LoggerFactory.getLogger(H2MultiplexingRequesterResource.class);
51
52 private final Consumer<H2MultiplexingRequesterBootstrap> bootstrapCustomizer;
53
54 private H2MultiplexingRequester requester;
55
56 public H2MultiplexingRequesterResource(final Consumer<H2MultiplexingRequesterBootstrap> bootstrapCustomizer) {
57 this.bootstrapCustomizer = bootstrapCustomizer;
58 }
59
60 @Override
61 public void beforeEach(final ExtensionContext extensionContext) throws Exception {
62 LOG.debug("Starting up test client");
63 final H2MultiplexingRequesterBootstrap bootstrap = H2MultiplexingRequesterBootstrap.bootstrap()
64 .setTlsStrategy(new H2ClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
65 .setStreamListener(LoggingH2StreamListener.INSTANCE)
66 .setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
67 .setExceptionCallback(LoggingExceptionCallback.INSTANCE)
68 .setIOSessionListener(LoggingIOSessionListener.INSTANCE);
69 bootstrapCustomizer.accept(bootstrap);
70 requester = bootstrap.create();
71 }
72
73 @Override
74 public void afterEach(final ExtensionContext extensionContext) throws Exception {
75 LOG.debug("Shutting down test client");
76 if (requester != null) {
77 try {
78 requester.close(CloseMode.GRACEFUL);
79 } catch (final Exception ignore) {
80 }
81 }
82 }
83
84 public H2MultiplexingRequester start() {
85 Assertions.assertNotNull(requester);
86 requester.start();
87 return requester;
88 }
89
90 }