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