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 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   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3599">MNG-3599</a>.
37   *
38   * @author Brett Porter
39   * @author John Casey
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                   * 2008-09-29 Oleg: "Proxy-Connection" is not part of http spec, but an extended header, and
97                   * as such cannot be expected from all the clients.
98                   * Changing the code to test for more generalized case: local proxy receives a request with
99                   * correct server url and resource uri
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          * NOTE: Make sure the WebDAV extension required by the test project has been pulled down into the local
159          * repo before the actual test installs Jetty as a mirror for everything. Otherwise, we will get garbage
160          * for the JAR/POM of the extension and its dependencies when run against a vanilla repo.
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      * Test that HTTP proxy is used for HTTP and for WebDAV.
193      *
194      * @throws Exception in case of failure
195      */
196     public void testitUseHttpProxyForWebDAV()
197         throws Exception
198     {
199         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3599-mk2" );
200 
201         /*
202          * NOTE: Make sure the WebDAV extension required by the test project has been pulled down into the local
203          * repo before the actual test installs Jetty as a mirror for everything. Otherwise, we will get garbage
204          * for the JAR/POM of the extension and its dependencies when run against a vanilla repo.
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 }