1 package org.apache.maven.it;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.maven.it.util.ResourceExtractor;
29 import org.apache.maven.shared.utils.StringUtils;
30 import org.apache.maven.shared.utils.io.FileUtils;
31 import org.eclipse.jetty.server.Handler;
32 import org.eclipse.jetty.server.NetworkConnector;
33 import org.eclipse.jetty.server.Request;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.server.handler.AbstractHandler;
36
37
38
39
40
41
42
43
44 public class MavenITmng3599useHttpProxyForWebDAVTest
45 extends AbstractMavenIntegrationTestCase
46 {
47 private Server server;
48
49 private int port;
50
51 private static final String CONTENT = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
52 " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" +
53 " <modelVersion>4.0.0</modelVersion>\n" +
54 " <groupId>org.apache.maven.its.mng3599</groupId>\n" +
55 " <artifactId>test</artifactId>\n" +
56 " <version>1.0-SNAPSHOT</version>\n" +
57 " <name>MNG-3599</name>\n" +
58 "</project>";
59
60 public MavenITmng3599useHttpProxyForWebDAVTest()
61 {
62 super( "(2.0.9,3.3.9)" );
63 }
64
65 @Override
66 protected void setUp()
67 throws Exception
68 {
69 Handler handler = new AbstractHandler()
70 {
71 @Override
72 public void handle( String target, Request baseRequest, HttpServletRequest request,
73 HttpServletResponse response )
74 throws IOException
75 {
76 System.out.println( "Got request for URL: '" + request.getRequestURL() + "'" );
77 System.out.flush();
78
79 response.setContentType( "text/plain" );
80
81 System.out.println( "Checking for 'Proxy-Connection' header..." );
82 if ( request.getHeader( "Proxy-Connection" ) != null )
83 {
84 response.setStatus( HttpServletResponse.SC_OK );
85 response.getWriter().print( CONTENT );
86
87 System.out.println( "Proxy-Connection found." );
88 }
89
90
91
92
93
94
95 else if
96 (
97 request.getRequestURI().startsWith( "/org/apache/maven/its/mng3599/test-dependency" )
98 && request.getRequestURL().toString().startsWith( "http://www.example.com" )
99 )
100 {
101 response.setStatus( HttpServletResponse.SC_OK );
102 response.getWriter().print( CONTENT );
103
104 System.out.println( "Correct proxied request 'http://www.example.com' for resource '/org/apache/maven/its/mng3599/test-dependency' found." );
105 }
106 else
107 {
108 response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
109
110 System.out.println( "Proxy-Connection not found." );
111 }
112
113 ( (Request) request ).setHandled( true );
114 }
115 };
116
117 server = new Server( 0 );
118 server.setHandler( handler );
119 server.start();
120 if ( server.isFailed() )
121 {
122 fail( "Couldn't bind the server socket to a free port!" );
123 }
124 port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
125 System.out.println( "Bound server socket to the port " + port );
126 }
127
128 @Override
129 protected void tearDown()
130 throws Exception
131 {
132 if ( server != null )
133 {
134 server.stop();
135 server.join();
136 }
137 }
138
139 public void testitUseHttpProxyForHttp()
140 throws Exception
141 {
142 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3599" );
143
144
145
146
147
148
149 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
150 verifier.executeGoal( "validate" );
151 verifier.verifyErrorFreeLog();
152 verifier.resetStreams();
153
154 String settings = FileUtils.fileRead( new File( testDir, "settings-template.xml" ) );
155 settings = StringUtils.replace( settings, "@port@", Integer.toString( port ) );
156 String newSettings = StringUtils.replace( settings, "@protocol@", "http" );
157
158 FileUtils.fileWrite( new File( testDir, "settings.xml" ).getAbsolutePath(), newSettings );
159
160 verifier = newVerifier( testDir.getAbsolutePath() );
161
162 verifier.addCliOption( "--settings" );
163 verifier.addCliOption( "settings.xml" );
164 verifier.addCliOption( "-X" );
165
166 verifier.deleteArtifacts( "org.apache.maven.its.mng3599" );
167
168 verifier.setLogFileName( "logHttp.txt" );
169 verifier.executeGoal( "compile" );
170 verifier.verifyErrorFreeLog();
171 verifier.resetStreams();
172
173 verifier.assertArtifactPresent( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar" );
174 verifier.assertArtifactContents( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar",
175 CONTENT );
176 }
177
178
179
180
181
182
183 public void testitUseHttpProxyForWebDAV()
184 throws Exception
185 {
186 requiresMavenVersion( "[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,3.3.9)" );
187
188 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3599" );
189
190
191
192
193
194
195 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
196 verifier.executeGoal( "validate" );
197 verifier.verifyErrorFreeLog();
198 verifier.resetStreams();
199
200 String settings = FileUtils.fileRead( new File( testDir, "settings-template.xml" ) );
201 settings = StringUtils.replace( settings, "@port@", Integer.toString( port ) );
202 String newSettings = StringUtils.replace( settings, "@protocol@", "dav" );
203
204 FileUtils.fileWrite( new File( testDir, "settings.xml" ).getAbsolutePath(), newSettings );
205
206 verifier = newVerifier( testDir.getAbsolutePath() );
207
208 verifier.addCliOption( "--settings" );
209 verifier.addCliOption( "settings.xml" );
210 verifier.addCliOption( "-X" );
211
212 verifier.deleteArtifacts( "org.apache.maven.its.mng3599" );
213
214 verifier.setLogFileName( "logDAV.txt" );
215 verifier.executeGoal( "compile" );
216 verifier.verifyErrorFreeLog();
217 verifier.resetStreams();
218
219 verifier.assertArtifactPresent( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar" );
220 verifier.assertArtifactContents( "org.apache.maven.its.mng3599", "test-dependency", "1.0", "jar",
221 CONTENT );
222 }
223 }