View Javadoc
1   package org.apache.maven.wagon.providers.http;
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.wagon.ResourceDoesNotExistException;
23  import org.apache.maven.wagon.StreamingWagon;
24  import org.apache.maven.wagon.TransferFailedException;
25  import org.apache.maven.wagon.WagonException;
26  import org.apache.maven.wagon.authorization.AuthorizationException;
27  import org.apache.maven.wagon.http.HttpWagonTestCase;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.FileNotFoundException;
32  import java.io.IOException;
33  import java.util.Properties;
34  
35  /**
36   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
37   *
38   */
39  public class LightweightHttpWagonTest
40      extends HttpWagonTestCase
41  {
42      protected String getProtocol()
43      {
44          return "http";
45      }
46  
47      protected String getTestRepositoryUrl()
48      {
49          return getProtocol() + "://localhost:" + getTestRepositoryPort() + "/";
50      }
51  
52      protected void setHttpConfiguration( StreamingWagon wagon, Properties headers, Properties params )
53      {
54          ( (LightweightHttpWagon) wagon ).setHttpHeaders( headers );
55      }
56  
57      @Override
58      protected boolean supportPreemptiveAuthenticationGet()
59      {
60          return false;
61      }
62  
63      @Override
64      protected boolean supportPreemptiveAuthenticationPut()
65      {
66          return false;
67      }
68  
69      @Override
70      protected boolean supportProxyPreemptiveAuthentication()
71      {
72          return false;
73      }
74  
75      @Override
76      protected void verifyWagonExceptionMessage( Exception e, int forStatusCode, String forUrl, String forReasonPhrase )
77      {
78  
79          // HttpUrlConnection prevents direct API access to the response code or reasonPhrase for any
80          // status code >= 400. So all we can do is check WagonException wraps the HttpUrlConnection
81          // thrown IOException / FileNotFoundException as a cause, if cause is not null
82  
83          assertNotNull( e );
84          try
85          {
86              assertTrue( "only verify instances of WagonException", e instanceof WagonException );
87  
88              String assertMessageForBadMessage = "exception message not described properly: ";
89              switch ( forStatusCode )
90              {
91                  case HttpServletResponse.SC_GONE:
92                  case HttpServletResponse.SC_NOT_FOUND:
93                      assertTrue( "404 or 410 should throw ResourceDoesNotExistException",
94                              e instanceof ResourceDoesNotExistException );
95  
96                      if ( e.getCause() != null )
97                      {
98                          assertTrue( "ResourceDoesNotExistException should have the expected cause",
99                                  e.getCause() instanceof FileNotFoundException );
100                         // the status code and reason phrase cannot always be learned due to implementation limitations
101                         // which means the message may not include them
102                         assertEquals( assertMessageForBadMessage, "resource missing at " + forUrl, e.getMessage() );
103                     }
104                     else
105                     {
106                         assertEquals( assertMessageForBadMessage, "resource missing at " + forUrl
107                                 + ", status: " + forStatusCode + " " + forReasonPhrase, e.getMessage() );
108                     }
109 
110                     break;
111 
112                 case HttpServletResponse.SC_FORBIDDEN:
113                     assertTrue( "403 Forbidden throws AuthorizationException",
114                             e instanceof AuthorizationException );
115 
116                     assertEquals( assertMessageForBadMessage, "authorization failed for " + forUrl + ", status: 403"
117                             + ( StringUtils.isEmpty( forReasonPhrase ) ? " Forbidden" : ( " " + forReasonPhrase ) ),
118                             e.getMessage() );
119                     break;
120 
121                 case HttpServletResponse.SC_UNAUTHORIZED:
122                     assertTrue( "401 Unauthorized throws AuthorizationException",
123                             e instanceof AuthorizationException );
124 
125                     assertEquals( assertMessageForBadMessage, "authentication failed for " + forUrl + ", status: 401"
126                                     + ( StringUtils.isEmpty( forReasonPhrase ) ? " Unauthorized" :
127                                     ( " " + forReasonPhrase ) ),
128                             e.getMessage() );
129                     break;
130 
131                 default:
132                     assertTrue( "general exception must be TransferFailedException",
133                             e instanceof TransferFailedException );
134                     assertTrue( "expected status code for transfer failures should be >= 400, but none of "
135                                     + " the already handled codes",
136                             forStatusCode >= HttpServletResponse.SC_BAD_REQUEST );
137 
138                     if ( e.getCause() != null )
139                     {
140                         assertTrue( "TransferFailedException should have the original cause for diagnosis",
141                                     e.getCause() instanceof IOException );
142                     }
143 
144                     // the status code and reason phrase cannot always be learned due to implementation limitations
145                     // so the message may not include them, but the implementation should use a consistent format
146                     assertTrue( "message should always include url",
147                             e.getMessage().startsWith( "transfer failed for " + forUrl ) );
148 
149                     if ( e.getMessage().length() > ( "transfer failed for " + forUrl ).length() )
150                     {
151                         assertTrue( "message should include url and status code",
152                                 e.getMessage().startsWith( "transfer failed for " + forUrl + ", status: " + forStatusCode ) );
153                     }
154 
155                     if ( e.getMessage().length() > ( "transfer failed for " + forUrl + ", status: " + forStatusCode ).length() )
156                     {
157                         assertEquals( "message should include url and status code and reason phrase",
158                                 "transfer failed for " + forUrl + ", status: " + forStatusCode + " " + forReasonPhrase,
159                                 e.getMessage() );
160                     }
161 
162                     break;
163             }
164 
165         }
166         catch ( AssertionError assertionError )
167         {
168             logger.error( "Exception which failed assertions: ", e );
169             throw assertionError;
170         }
171     }
172 
173 }