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  
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   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3599">MNG-3599</a>.
39   * 
40   * @author Brett Porter
41   * @author John Casey
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                   * 2008-09-29 Oleg: "Proxy-Connection" is not part of http spec, but an extended header, and 
91                   * as such cannot be expected from all the clients.
92                   * Changing the code to test for more generalized case: local proxy receives a request with
93                   * correct server url and resource uri
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          * NOTE: Make sure the WebDAV extension required by the test project has been pulled down into the local
146          * repo before the actual test installs Jetty as a mirror for everything. Otherwise, we will get garbage
147          * for the JAR/POM of the extension and its dependencies when run against a vanilla repo.
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      * Test that HTTP proxy is used for HTTP and for WebDAV.
180      *
181      * @throws Exception in case of failure
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          * NOTE: Make sure the WebDAV extension required by the test project has been pulled down into the local
192          * repo before the actual test installs Jetty as a mirror for everything. Otherwise, we will get garbage
193          * for the JAR/POM of the extension and its dependencies when run against a vanilla repo.
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 }