View Javadoc
1   package org.apache.maven.it;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.it.util.ResourceExtractor;
23  import org.eclipse.jetty.server.Handler;
24  import org.eclipse.jetty.server.NetworkConnector;
25  import org.eclipse.jetty.server.Request;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.server.handler.AbstractHandler;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.List;
34  
35  /**
36   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3652">MNG-3652</a>.
37   */
38  public class MavenITmng3652UserAgentHeaderTest
39      extends AbstractMavenIntegrationTestCase
40  {
41      private Server server;
42  
43      private int port;
44  
45      private String userAgent;
46  
47      private String customHeader;
48  
49      public MavenITmng3652UserAgentHeaderTest()
50      {
51          super( "[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)" ); // 2.1.0-M1+
52      }
53  
54      @Override
55      protected void setUp()
56          throws Exception
57      {
58          Handler handler = new AbstractHandler()
59          {
60              @Override
61              public void handle( String target, Request baseRequest, HttpServletRequest request,
62                                  HttpServletResponse response )
63                  throws IOException
64              {
65                  System.out.println( "Handling URL: '" + request.getRequestURL() + "'" );
66  
67                  userAgent = request.getHeader( "User-Agent" );
68  
69                  customHeader = request.getHeader( "Custom-Header" );
70  
71                  System.out.println( "Got User-Agent: '" + userAgent + "'" );
72  
73                  response.setContentType( "text/plain" );
74                  response.setStatus( HttpServletResponse.SC_OK );
75                  response.getWriter().println( "some content" );
76                  response.getWriter().println();
77  
78                  ( (Request) request ).setHandled( true );
79              }
80          };
81  
82          server = new Server( 0 );
83          server.setHandler( handler );
84          server.start();
85          if ( server.isFailed() )
86          {
87              fail( "Couldn't bind the server socket to a free port!" );
88          }
89          port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
90          System.out.println( "Bound server socket to the port " + port );
91      }
92  
93      @Override
94      protected void tearDown()
95          throws Exception
96      {
97          if ( server != null)
98          {
99              server.stop();
100             server.join();
101         }
102     }
103 
104     /**
105      * Test that the user agent header is configured in the wagon manager.
106      *
107      * @throws Exception in case of failure
108      */
109     public void testmng3652_UnConfiguredHttp()
110         throws Exception
111     {
112         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
113         File pluginDir = new File( testDir, "test-plugin" );
114         File projectDir = new File( testDir, "test-project" );
115 
116         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
117         verifier.executeGoal( "install" );
118         verifier.verifyErrorFreeLog();
119         verifier.resetStreams();
120 
121         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
122 
123         verifier.addCliOption( "-DtestPort=" + port );
124         verifier.addCliOption( "-X" );
125 
126         verifier.setLogFileName( "log-unConfiguredHttp.txt" );
127         verifier.executeGoal( "validate" );
128         verifier.verifyErrorFreeLog();
129         verifier.resetStreams();
130 
131         String userAgent = this.userAgent;
132         assertNotNull( userAgent );
133 
134         File touchFile = new File( projectDir, "target/touch.txt" );
135         assertTrue( touchFile.exists() );
136 
137         List<String> lines = verifier.loadFile( touchFile, false );
138 
139         // NOTE: system property for maven.version may not exist if you use -Dtest
140         // surefire parameter to run this single test. Therefore, the plugin writes
141         // the maven version into the check file.
142         String mavenVersion = getMavenUAVersion( lines.get( 0 ) );
143         String javaVersion = lines.get( 1 );
144         String os = lines.get( 2 ) + " " + lines.get( 3 );
145         String artifactVersion = lines.get( 4 );
146 
147         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
148         {
149             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
150                 + javaVersion + "; " + os + ")" + " maven-artifact/" + artifactVersion, userAgent );
151         }
152         else
153         {
154             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
155                 + javaVersion + "; " + os + ")", userAgent );
156         }
157     }
158 
159     public void testmng3652_UnConfiguredDAV()
160         throws Exception
161     {
162         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
163         File pluginDir = new File( testDir, "test-plugin" );
164         File projectDir = new File( testDir, "test-project" );
165 
166         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
167         verifier.executeGoal( "install" );
168         verifier.verifyErrorFreeLog();
169         verifier.resetStreams();
170 
171         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
172 
173         // test webdav
174         verifier.addCliOption( "-DtestPort=" + port );
175         verifier.addCliOption( "-DtestProtocol=dav:http" );
176         verifier.addCliOption( "-X" );
177 
178         verifier.setLogFileName( "log-unConfiguredDAV.txt" );
179         verifier.executeGoal( "validate" );
180         verifier.verifyErrorFreeLog();
181         verifier.resetStreams();
182 
183         File touchFile = new File( projectDir, "target/touch.txt" );
184         assertTrue( touchFile.exists() );
185 
186         List<String> lines = verifier.loadFile( touchFile, false );
187 
188         // NOTE: system property for maven.version may not exist if you use -Dtest
189         // surefire parameter to run this single test. Therefore, the plugin writes
190         // the maven version into the check file.
191         String mavenVersion = getMavenUAVersion( lines.get( 0 ) );
192         String javaVersion = lines.get( 1 );
193         String os = lines.get( 2 ) + " " + lines.get( 3 );
194         String artifactVersion = lines.get( 4 );
195 
196         String userAgent = this.userAgent;
197         assertNotNull( userAgent );
198 
199         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
200         {
201             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
202                 + javaVersion + "; " + os + ")" + " maven-artifact/" + artifactVersion, userAgent );
203         }
204         else
205         {
206             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
207                 + javaVersion + "; " + os + ")", userAgent );
208         }
209     }
210 
211     public void testmng3652_ConfigurationInSettingsWithoutUserAgent()
212         throws Exception
213     {
214         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
215         File pluginDir = new File( testDir, "test-plugin" );
216         File projectDir = new File( testDir, "test-project" );
217 
218         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
219         verifier.executeGoal( "install" );
220         verifier.verifyErrorFreeLog();
221         verifier.resetStreams();
222 
223         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
224 
225         // test settings with no config
226 
227         verifier.addCliOption( "-DtestPort=" + port );
228         verifier.addCliOption( "--settings" );
229         verifier.addCliOption( "settings-no-config.xml" );
230         verifier.addCliOption( "-X" );
231 
232         verifier.setLogFileName( "log-configWithoutUserAgent.txt" );
233         verifier.executeGoal( "validate" );
234         verifier.verifyErrorFreeLog();
235         verifier.resetStreams();
236 
237         File touchFile = new File( projectDir, "target/touch.txt" );
238         assertTrue( touchFile.exists() );
239 
240         List<String> lines = verifier.loadFile( touchFile, false );
241 
242         // NOTE: system property for maven.version may not exist if you use -Dtest
243         // surefire parameter to run this single test. Therefore, the plugin writes
244         // the maven version into the check file.
245         String mavenVersion = getMavenUAVersion( lines.get( 0 ) );
246         String javaVersion = lines.get( 1 );
247         String os = lines.get( 2 ) + " " + lines.get( 3 );
248         String artifactVersion = lines.get( 4 );
249 
250         String userAgent = this.userAgent;
251         assertNotNull( userAgent );
252 
253         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
254         {
255             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
256                 + javaVersion + "; " + os + ")" + " maven-artifact/" + artifactVersion, userAgent );
257         }
258         else
259         {
260             assertEquals( "Comparing User-Agent '" + userAgent + "'", "Apache-Maven/" + mavenVersion + " (Java "
261                 + javaVersion + "; " + os + ")", userAgent );
262         }
263     }
264 
265     public void testmng3652_UserAgentConfiguredInSettings()
266         throws Exception
267     {
268         // customizing version not supported in Maven 3
269         //requiresMavenVersion( "(,3.0-beta-3)" );
270         requiresMavenVersion("[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)");
271 
272         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
273         File pluginDir = new File( testDir, "test-plugin" );
274         File projectDir = new File( testDir, "test-project" );
275 
276         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
277         verifier.executeGoal( "install" );
278         verifier.verifyErrorFreeLog();
279         verifier.resetStreams();
280 
281         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
282 
283         // test settings with config
284 
285         verifier.addCliOption( "-DtestPort=" + port );
286         verifier.addCliOption( "--settings" );
287         verifier.addCliOption( "settings.xml" );
288         verifier.addCliOption( "-X" );
289 
290         verifier.setLogFileName( "log-configWithUserAgent.txt" );
291         verifier.executeGoal( "validate" );
292         verifier.verifyErrorFreeLog();
293         verifier.resetStreams();
294 
295         String userAgent = this.userAgent;
296         assertNotNull( userAgent );
297 
298         assertEquals( "Maven Fu", userAgent );
299         assertEquals( "My wonderful header", customHeader );
300     }
301 
302     public void testmng3652_AdditionnalHttpHeaderConfiguredInSettings()
303         throws Exception
304     {
305         // customizing version not supported in Maven 3
306         requiresMavenVersion("[2.1.0-M1,3.0-alpha-1),[3.0-beta-3,)");
307 
308         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3652" );
309         File pluginDir = new File( testDir, "test-plugin" );
310         File projectDir = new File( testDir, "test-project" );
311 
312         Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" );
313         verifier.executeGoal( "install" );
314         verifier.verifyErrorFreeLog();
315         verifier.resetStreams();
316 
317         verifier = newVerifier( projectDir.getAbsolutePath(), "remote" );
318 
319         // test settings with config
320 
321         verifier.addCliOption( "-DtestPort=" + port );
322         verifier.addCliOption( "--settings" );
323         verifier.addCliOption( "settings.xml" );
324         verifier.addCliOption( "-X" );
325 
326         verifier.setLogFileName( "log-configWithUserAgent.txt" );
327         verifier.executeGoal( "validate" );
328         verifier.verifyErrorFreeLog();
329         verifier.resetStreams();
330 
331         String userAgent = this.userAgent;
332         assertNotNull( userAgent );
333 
334         assertEquals( "Maven Fu", userAgent );
335         assertEquals( "My wonderful header", customHeader );
336     }
337 
338     private String getMavenUAVersion( String mavenVersion )
339     {
340         if ( matchesVersionRange( "(,3.0-beta-3)" ) )
341         {
342             return mavenVersion.substring( 0, 3 );
343         }
344         else
345         {
346             // Maven 3 returns the whole version
347             return mavenVersion;
348         }
349     }
350 }