1 30 31 package org.apache.commons.httpclient; 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 36 import org.apache.commons.httpclient.methods.GetMethod; 37 import org.apache.commons.httpclient.methods.HeadMethod; 38 import org.apache.commons.httpclient.methods.PostMethod; 39 import org.apache.commons.httpclient.methods.PutMethod; 40 41 60 public class TestWebappBasicAuth extends TestWebappBase { 61 62 public TestWebappBasicAuth(String testName) { 63 super(testName); 64 } 65 66 public static Test suite() { 67 TestSuite suite = new TestSuite(TestWebappBasicAuth.class); 68 return suite; 69 } 70 71 public static void main(String args[]) { 72 String [] testCaseName = { TestWebappBasicAuth.class.getName() }; 73 junit.textui.TestRunner.main(testCaseName); 74 } 75 76 78 public void testSimpleAuthGet() throws Exception { 79 HttpClient client = createHttpClient(); 80 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons")); 81 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic"); 82 83 try { 84 client.executeMethod(method); 85 } catch (Throwable t) { 86 t.printStackTrace(); 87 fail("Unable to execute method : " + t.toString()); 88 } 89 assertEquals(200,method.getStatusCode()); 90 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0); 91 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 92 93 method.recycle(); 94 method.setPath("/" + getWebappContext() + "/auth/basic"); 95 try { 96 client.executeMethod(method); 97 } catch (Throwable t) { 98 t.printStackTrace(); 99 fail("Unable to execute method : " + t.toString()); 100 } 101 assertEquals(200,method.getStatusCode()); 102 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0); 103 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 104 } 105 106 public void testSimpleAuthPost() throws Exception { 107 HttpClient client = createHttpClient(); 108 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons")); 109 PostMethod method = new PostMethod("/" + getWebappContext() + "/auth/basic"); 110 method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } ); 111 112 try { 113 client.executeMethod(method); 114 } catch (Throwable t) { 115 t.printStackTrace(); 116 fail("Unable to execute method : " + t.toString()); 117 } 118 assertEquals(200,method.getStatusCode()); 119 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0); 120 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 121 122 method.recycle(); 123 method.setPath("/" + getWebappContext() + "/auth/basic"); 124 method.setRequestBody(new NameValuePair[] { new NameValuePair("testing","one") } ); 125 try { 126 client.executeMethod(method); 127 } catch (Throwable t) { 128 t.printStackTrace(); 129 fail("Unable to execute method : " + t.toString()); 130 } 131 assertEquals(200,method.getStatusCode()); 132 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: POST</title>") >= 0); 133 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 134 } 135 136 public void testSimpleAuthPut() throws Exception { 137 HttpClient client = createHttpClient(); 138 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons")); 139 PutMethod method = new PutMethod("/" + getWebappContext() + "/auth/basic"); 140 method.setRequestBody("testing one two three"); 141 try { 142 client.executeMethod(method); 143 } catch (Throwable t) { 144 t.printStackTrace(); 145 fail("Unable to execute method : " + t.toString()); 146 } 147 assertEquals(200,method.getStatusCode()); 148 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0); 149 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 150 151 method.recycle(); 152 method.setPath("/" + getWebappContext() + "/auth/basic"); 153 try { 154 client.executeMethod(method); 155 } catch (Throwable t) { 156 t.printStackTrace(); 157 fail("Unable to execute method : " + t.toString()); 158 } 159 assertEquals(200,method.getStatusCode()); 160 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: PUT</title>") >= 0); 161 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 162 } 163 164 public void testNoCredAuthRetry() throws Exception { 165 HttpClient client = createHttpClient(); 166 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic"); 167 168 try { 169 client.executeMethod(method); 170 } catch (Throwable t) { 171 t.printStackTrace(); 172 fail("Unable to execute method : " + t.toString()); 173 } 174 assertEquals(401,method.getStatusCode()); 175 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0); 176 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0); 177 178 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("jakarta","commons")); 179 180 method.recycle(); 181 method.setPath("/" + getWebappContext() + "/auth/basic"); 182 try { 183 client.executeMethod(method); 184 } catch (Throwable t) { 185 t.printStackTrace(); 186 fail("Unable to execute method : " + t.toString()); 187 } 188 assertEquals(200,method.getStatusCode()); 189 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0); 190 assertTrue(method.getResponseBodyAsString().indexOf("<p>You have authenticated as \"jakarta:commons\"</p>") >= 0); 191 } 192 193 public void testBadCredFails() throws Exception { 194 HttpClient client = createHttpClient(); 195 GetMethod method = new GetMethod("/" + getWebappContext() + "/auth/basic"); 196 197 try { 198 client.executeMethod(method); 199 } catch (Throwable t) { 200 t.printStackTrace(); 201 fail("Unable to execute method : " + t.toString()); 202 } 203 assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode()); 204 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0); 205 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized.</p>") >= 0); 206 207 client.getState().setCredentials("BasicAuthServlet",new UsernamePasswordCredentials("bad","creds")); 208 209 method.recycle(); 210 method.setPath("/" + getWebappContext() + "/auth/basic"); 211 try { 212 client.executeMethod(method); 213 } catch (Throwable t) { 214 t.printStackTrace(); 215 fail("Unable to execute method : " + t.toString()); 216 } 217 assertEquals(HttpStatus.SC_UNAUTHORIZED,method.getStatusCode()); 218 assertTrue(method.getResponseBodyAsString().indexOf("<title>BasicAuth Servlet: GET</title>") >= 0); 219 assertTrue(method.getResponseBodyAsString().indexOf("<p>Not authorized. \"Basic YmFkOmNyZWRz\" not recognized.</p>") >= 0); 220 } 221 222 public void testHeadAuth() throws Exception { 223 HttpClient client = new HttpClient(); 224 HttpState state = client.getState(); 225 Credentials cred = new UsernamePasswordCredentials("jakarta", "commons"); 226 state.setCredentials(null, cred); 227 HostConfiguration hc = new HostConfiguration(); 228 hc.setHost(getHost(), getPort(), getProtocol()); 229 client.setHostConfiguration(hc); 230 client.setState(state); 231 HeadMethod method = new HeadMethod("/"+ getWebappContext() +"/auth/basic"); 232 client.executeMethod(method); 233 method.releaseConnection(); 234 assertEquals(200, method.getStatusCode()); 235 } 236 237 } 238 239 240 241 242 | Popular Tags |