1111import jakarta .servlet .http .HttpServletResponse ;
1212import org .eclipse .jetty .plus .webapp .EnvConfiguration ;
1313import org .eclipse .jetty .plus .webapp .PlusConfiguration ;
14+ import org .eclipse .jetty .server .ConnectionFactory ;
15+ import org .eclipse .jetty .server .Connector ;
16+ import org .eclipse .jetty .server .ForwardedRequestCustomizer ;
17+ import org .eclipse .jetty .server .HttpConfiguration ;
1418import org .eclipse .jetty .server .Request ;
1519import org .eclipse .jetty .server .Server ;
1620import org .eclipse .jetty .server .handler .AbstractHandler ;
@@ -69,7 +73,40 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
6973 });
7074 server .start ();
7175 try {
72- doHttpRequest ();
76+ HttpResponse <String > response = doHttpRequest ();
77+ assertThat (response .statusCode ()).isEqualTo (200 );
78+ assertThat (response .body ()).isEqualTo ("Hello world" );
79+ } finally {
80+ server .stop ();
81+ }
82+ }
83+
84+ @ Test
85+ void forwardHeaders () throws Exception {
86+ Server server = new Server (PORT );
87+ for (Connector connector : server .getConnectors ()) {
88+ for (ConnectionFactory connectionFactory : connector .getConnectionFactories ()) {
89+ if (connectionFactory instanceof HttpConfiguration .ConnectionFactory ) {
90+ ((HttpConfiguration .ConnectionFactory ) connectionFactory ).getHttpConfiguration ()
91+ .addCustomizer (new ForwardedRequestCustomizer ());
92+ }
93+ }
94+ }
95+ server .setHandler (new AbstractHandler () {
96+ @ Override
97+ public void handle (String target , Request baseRequest , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException {
98+ response .setStatus (200 );
99+ response .setHeader ("Content-Type" , "text/plain" );
100+ response .getWriter ().print ("I am " + request .getServerName () + ":" + request .getServerPort ());
101+ response .getWriter ().flush ();
102+ baseRequest .setHandled (true );
103+ }
104+ });
105+ server .start ();
106+ try {
107+ HttpResponse <String > response = doHttpRequest ("X-Forwarded-Host" , "some-host" , "X-Forwarded-Port" , "12345" );
108+ assertThat (response .statusCode ()).isEqualTo (200 );
109+ assertThat (response .body ()).isEqualTo ("I am some-host:12345" );
73110 } finally {
74111 server .stop ();
75112 }
@@ -84,7 +121,9 @@ void servlet() throws Exception {
84121 server .setHandler (handler );
85122 server .start ();
86123 try {
87- doHttpRequest ();
124+ HttpResponse <String > response = doHttpRequest ();
125+ assertThat (response .statusCode ()).isEqualTo (200 );
126+ assertThat (response .body ()).isEqualTo ("Hello world" );
88127 } finally {
89128 server .stop ();
90129 }
@@ -103,7 +142,9 @@ void webapp(@TempDir File tempDir) throws Exception {
103142 server .setHandler (context );
104143 server .start ();
105144 try {
106- doHttpRequest ();
145+ HttpResponse <String > response = doHttpRequest ();
146+ assertThat (response .statusCode ()).isEqualTo (200 );
147+ assertThat (response .body ()).isEqualTo ("Hello world" );
107148 } finally {
108149 server .stop ();
109150 }
@@ -171,12 +212,13 @@ private static void doBinaryWebsocketRequest() throws Exception {
171212 }
172213 }
173214
174- private static void doHttpRequest () throws IOException , InterruptedException {
215+ private static HttpResponse < String > doHttpRequest (String ... headers ) throws IOException , InterruptedException {
175216 HttpClient client = HttpClient .newBuilder ().connectTimeout (Duration .ofSeconds (1 )).build ();
176- HttpRequest request = HttpRequest .newBuilder (URI .create (String .format ("http://localhost:%d/" , PORT )))
177- .GET ().header ("Accept" , "text/plain" ).timeout (Duration .ofSeconds (1 )).build ();
178- HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
179- assertThat (response .statusCode ()).isEqualTo (200 );
180- assertThat (response .body ()).isEqualTo ("Hello world" );
217+ HttpRequest .Builder request = HttpRequest .newBuilder (URI .create (String .format ("http://localhost:%d/" , PORT )))
218+ .GET ().header ("Accept" , "text/plain" ).timeout (Duration .ofSeconds (1 ));
219+ if (headers .length > 0 ) {
220+ request .headers (headers );
221+ }
222+ return client .send (request .build (), HttpResponse .BodyHandlers .ofString ());
181223 }
182224}
0 commit comments