2525import org .archive .net .UURIFactory ;
2626import org .archive .spring .ConfigPath ;
2727import org .archive .util .Recorder ;
28- import org .junit .jupiter .api .Test ;
28+ import org .eclipse .jetty .proxy .ProxyHandler ;
29+ import org .eclipse .jetty .server .Server ;
30+ import org .eclipse .jetty .server .ServerConnector ;
31+ import org .junit .jupiter .api .*;
2932import org .junit .jupiter .api .io .TempDir ;
3033
34+ import java .io .IOException ;
3135import java .net .Inet4Address ;
3236import java .net .InetAddress ;
3337import java .net .InetSocketAddress ;
3438import java .nio .file .Files ;
3539import java .nio .file .Path ;
3640
37- import static org .junit .jupiter .api .Assertions .assertEquals ;
38- import static org .junit .jupiter .api .Assertions .assertTrue ;
41+ import static org .junit .jupiter .api .Assertions .*;
3942
4043public class FetchHTTP2Test {
44+ private static HttpServer server ;
45+
4146 @ TempDir
4247 Path tempDir ;
48+ private BdbModule bdb ;
49+ private BdbCookieStore cookieStore ;
50+ private FetchHTTP2 fetcher ;
51+ private static String baseUrl ;
52+ private Recorder recorder ;
4353
44- @ Test
45- public void test () throws Exception {
54+ @ BeforeAll
55+ public static void beforeAll () throws IOException {
4656 InetAddress loopbackAddress = Inet4Address .getLoopbackAddress ();
47- var server = HttpServer .create (new InetSocketAddress (loopbackAddress , 0 ), -1 );
57+ server = HttpServer .create (new InetSocketAddress (loopbackAddress , 0 ), -1 );
4858 server .createContext ("/" , exchange -> {
59+ if (exchange .getRequestHeaders ().containsKey ("Via" )) {
60+ exchange .getResponseHeaders ().add ("Used-Proxy" , "true" );
61+ }
4962 exchange .getResponseHeaders ().add ("Content-Type" , "text/html; charset=UTF-8" );
5063 exchange .getResponseHeaders ().add ("Set-Cookie" , "foo=bar; Path=/" );
5164 byte [] body = "Hello World!" .getBytes ();
@@ -54,40 +67,77 @@ public void test() throws Exception {
5467 exchange .close ();
5568 });
5669 server .start ();
57- BdbCookieStore cookieStore = new BdbCookieStore ();
58- BdbModule bdb = new BdbModule ();
70+ baseUrl = "http://" + server .getAddress ().getHostString () + ":" + server .getAddress ().getPort () + "/" ;
71+ }
72+
73+ @ AfterAll
74+ public static void afterAll () {
75+ if (server != null ) server .stop (0 );
76+ }
77+
78+ @ BeforeEach
79+ public void beforeEach () throws IOException {
80+ cookieStore = new BdbCookieStore ();
81+ bdb = new BdbModule ();
5982 Path cookies = tempDir .resolve ("cookies" );
6083 Files .createDirectories (cookies );
6184 bdb .setDir (new ConfigPath ("cookies" , cookies .toString ()));
6285 cookieStore .setBdbModule (bdb );
63- try (var serverCache = new DefaultServerCache ()) {
64- bdb .start ();
65- cookieStore .start ();
66- var fetcher = new FetchHTTP2 (serverCache , cookieStore );
67- fetcher .setUserAgentProvider (new CrawlMetadata ());
68- fetcher .start ();
69- try {
70- String url = "http://" + server .getAddress ().getHostString () + ":" + server .getAddress ().getPort () + "/" ;
71- var curi = new CrawlURI (UURIFactory .getInstance (url ));
72- curi .setRecorder (new Recorder (tempDir .toFile (), "temp" ));
73- fetcher .innerProcess (curi );
86+ var serverCache = new DefaultServerCache ();
87+ bdb .start ();
88+ cookieStore .start ();
89+ fetcher = new FetchHTTP2 (serverCache , cookieStore );
90+ fetcher .setUserAgentProvider (new CrawlMetadata ());
91+ recorder = new Recorder (tempDir .toFile (), "temp" );
92+ }
7493
75- assertEquals (200 , curi .getFetchStatus ());
76- assertEquals (CrawlURI .FetchType .HTTP_GET , curi .getFetchType ());
77- assertEquals (12 , curi .getContentLength ());
78- assertEquals ("text/html; charset=UTF-8" , curi .getContentType ());
79- assertEquals ("UTF-8" , curi .getRecorder ().getCharset ().name ());
80- assertEquals (loopbackAddress .getHostAddress (), curi .getServerIP ());
81- assertEquals ("Hello World!" , curi .getRecorder ().getContentReplayPrefixString (100 ));
82- assertEquals ("foo=bar; Path=/" , curi .getHttpResponseHeader ("Set-Cookie" ));
83- assertTrue (curi .getFetchBeginTime () > 1 );
84- assertTrue (curi .getFetchCompletedTime () >= curi .getFetchBeginTime ());
85- } finally {
86- fetcher .stop ();
87- server .stop (0 );
88- cookieStore .stop ();
89- bdb .stop ();
90- }
94+ @ AfterEach
95+ public void afterEach () {
96+ fetcher .stop ();
97+ cookieStore .stop ();
98+ bdb .stop ();
99+ recorder .cleanup ();
100+ }
101+
102+ @ Test
103+ public void test () throws Exception {
104+ fetcher .start ();
105+ var curi = new CrawlURI (UURIFactory .getInstance (baseUrl ));
106+ curi .setRecorder (recorder );
107+ fetcher .innerProcess (curi );
108+
109+ assertEquals (200 , curi .getFetchStatus ());
110+ assertEquals (CrawlURI .FetchType .HTTP_GET , curi .getFetchType ());
111+ assertEquals (12 , curi .getContentLength ());
112+ assertEquals ("text/html; charset=UTF-8" , curi .getContentType ());
113+ assertEquals ("UTF-8" , curi .getRecorder ().getCharset ().name ());
114+ assertEquals (Inet4Address .getLoopbackAddress ().getHostAddress (), curi .getServerIP ());
115+ assertEquals ("Hello World!" , curi .getRecorder ().getContentReplayPrefixString (100 ));
116+ assertEquals ("foo=bar; Path=/" , curi .getHttpResponseHeader ("Set-Cookie" ));
117+ assertTrue (curi .getFetchBeginTime () > 1 );
118+ assertTrue (curi .getFetchCompletedTime () >= curi .getFetchBeginTime ());
119+ assertNull (curi .getHttpResponseHeader ("Used-Proxy" ));
120+ curi .getRecorder ().cleanup ();
121+ }
122+
123+ @ Test
124+ public void testHttpProxy () throws Exception {
125+ Server proxyServer = new Server (new InetSocketAddress (Inet4Address .getLoopbackAddress (), 0 ));
126+ proxyServer .setHandler (new ProxyHandler .Forward ());
127+ proxyServer .start ();
128+ try {
129+ var proxyPort = ((ServerConnector ) proxyServer .getConnectors ()[0 ]).getLocalPort ();
130+ fetcher .setHttpProxyHost (Inet4Address .getLoopbackAddress ().getHostAddress ());
131+ fetcher .setHttpProxyPort (proxyPort );
132+ fetcher .start ();
133+ var curi = new CrawlURI (UURIFactory .getInstance (baseUrl ));
134+ curi .setRecorder (recorder );
135+ fetcher .innerProcess (curi );
136+ assertEquals ("true" , curi .getHttpResponseHeader ("Used-Proxy" ));
137+ assertEquals (200 , curi .getFetchStatus ());
138+ assertEquals ("Hello World!" , curi .getRecorder ().getContentReplayPrefixString (100 ));
139+ } finally {
140+ proxyServer .stop ();
91141 }
92142 }
93143}
0 commit comments