1 package org.apache.maven.wagon.providers.http;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
80
81
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
101
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
145
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 }