Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 10467ebdde3e8a52989c1f076fb5924b31dad541 (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//


package org.eclipse.jetty.servlets;


import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


/**
 * TransparentProxyTest
 *
 *
 */
public class TransparentProxyTest
{
  

        protected Server server;
        protected Server proxyServer;
        
        public static class ServletA extends HttpServlet {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                resp.setContentType("text/plain");
                resp.getWriter().println("ok");
            }
        }
        
        @Before
        public void setUp () throws Exception
        {
            //set up the target server
            server = new Server();
            SelectChannelConnector connector = new SelectChannelConnector();
            connector.setPort(8080);
            server.addConnector(connector);
            ServletContextHandler handler = new ServletContextHandler(server, "/");
            handler.addServlet(ServletA.class, "/a");
            server.setHandler(handler);
            server.start();


            //set up the server that proxies to the target server
            proxyServer = new Server();
            SelectChannelConnector proxyConnector = new SelectChannelConnector();
            proxyConnector.setPort(8081);
            proxyServer.addConnector(proxyConnector);           
            ServletContextHandler proxyHandler = new ServletContextHandler(proxyServer, "/");
            proxyHandler.addServlet(new ServletHolder(new ProxyServlet.Transparent("/", "http", "127.0.0.1", 8080, "/")), "/");
            proxyServer.setHandler(proxyHandler);
            proxyServer.start();

        }
        
        
        @After
        public void tearDown() throws Exception
        {
            server.stop();
            proxyServer.stop();
        }


        @Test
        public void testDirectNoContentType() throws Exception
        {
            // Direct request without Content-Type set works
            URL url = new URL("http://localhost:8080/a");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            assertEquals(200, con.getResponseCode());
        }

        
        @Test
        public void testDirectWithContentType() throws Exception
        {
            // Direct request with Content-Type works
            URL url = new URL("http://localhost:8080/a");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            assertEquals(200, con.getResponseCode());
        }

        @Test
        public void testProxiedWithoutContentType() throws Exception
        {
            // Proxied request without Content-Type set works
            URL url = new URL("http://localhost:8081/a");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            assertEquals(200, con.getResponseCode());
            System.err.println (con.getContentType());
        }

        @Test
        public void testProxiedWithContentType() throws Exception
        {
            // Proxied request with Content-Type set fails

            URL url = new URL("http://localhost:8081/a");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            assertEquals(200, con.getResponseCode());
            System.err.println(con.getContentType());
            
        }
}

Back to the top