Source Code Cross Referenced for TestBasicAuth.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » auth » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache common HttpClient » org.apache.commons.httpclient.auth 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java,v 1.9 2004/11/20 17:56:40 olegk Exp $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         * ====================================================================
006:         *
007:         *  Licensed to the Apache Software Foundation (ASF) under one or more
008:         *  contributor license agreements.  See the NOTICE file distributed with
009:         *  this work for additional information regarding copyright ownership.
010:         *  The ASF licenses this file to You under the Apache License, Version 2.0
011:         *  (the "License"); you may not use this file except in compliance with
012:         *  the License.  You may obtain a copy of the License at
013:         *
014:         *      http://www.apache.org/licenses/LICENSE-2.0
015:         *
016:         *  Unless required by applicable law or agreed to in writing, software
017:         *  distributed under the License is distributed on an "AS IS" BASIS,
018:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019:         *  See the License for the specific language governing permissions and
020:         *  limitations under the License.
021:         * ====================================================================
022:         *
023:         * This software consists of voluntary contributions made by many
024:         * individuals on behalf of the Apache Software Foundation.  For more
025:         * information on the Apache Software Foundation, please see
026:         * <http://www.apache.org/>.
027:         *
028:         */
029:
030:        package org.apache.commons.httpclient.auth;
031:
032:        import java.io.IOException;
033:
034:        import junit.framework.Test;
035:        import junit.framework.TestSuite;
036:
037:        import org.apache.commons.codec.binary.Base64;
038:        import org.apache.commons.httpclient.EchoService;
039:        import org.apache.commons.httpclient.FeedbackService;
040:        import org.apache.commons.httpclient.Header;
041:        import org.apache.commons.httpclient.HttpClientTestBase;
042:        import org.apache.commons.httpclient.HttpState;
043:        import org.apache.commons.httpclient.HttpStatus;
044:        import org.apache.commons.httpclient.ProxyTestDecorator;
045:        import org.apache.commons.httpclient.UsernamePasswordCredentials;
046:        import org.apache.commons.httpclient.methods.GetMethod;
047:        import org.apache.commons.httpclient.methods.HeadMethod;
048:        import org.apache.commons.httpclient.methods.PostMethod;
049:        import org.apache.commons.httpclient.methods.PutMethod;
050:        import org.apache.commons.httpclient.methods.StringRequestEntity;
051:        import org.apache.commons.httpclient.server.AuthRequestHandler;
052:        import org.apache.commons.httpclient.server.HttpRequestHandlerChain;
053:        import org.apache.commons.httpclient.server.HttpServiceHandler;
054:        import org.apache.commons.httpclient.util.EncodingUtil;
055:
056:        /**
057:         * Basic authentication test cases.
058:         *
059:         * @author Oleg Kalnichevski
060:         * 
061:         * @version $Id: TestBasicAuth.java 480424 2006-11-29 05:56:49Z bayard $
062:         */
063:        public class TestBasicAuth extends HttpClientTestBase {
064:
065:            // ------------------------------------------------------------ Constructor
066:            public TestBasicAuth(final String testName) throws IOException {
067:                super (testName);
068:            }
069:
070:            // ------------------------------------------------------------------- Main
071:            public static void main(String args[]) {
072:                String[] testCaseName = { TestBasicAuth.class.getName() };
073:                junit.textui.TestRunner.main(testCaseName);
074:            }
075:
076:            // ------------------------------------------------------- TestCase Methods
077:
078:            public static Test suite() {
079:                TestSuite suite = new TestSuite(TestBasicAuth.class);
080:                ProxyTestDecorator.addTests(suite);
081:                return suite;
082:            }
083:
084:            public void testBasicAuthenticationWithNoCreds() throws IOException {
085:
086:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
087:                        "testuser", "testpass");
088:
089:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
090:                handlerchain.appendHandler(new AuthRequestHandler(creds));
091:                handlerchain.appendHandler(new HttpServiceHandler(
092:                        new FeedbackService()));
093:
094:                this .server.setRequestHandler(handlerchain);
095:                GetMethod httpget = new GetMethod("/test/");
096:                try {
097:                    this .client.executeMethod(httpget);
098:                    assertNotNull(httpget.getStatusLine());
099:                    assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget
100:                            .getStatusLine().getStatusCode());
101:                    AuthState authstate = httpget.getHostAuthState();
102:                    assertNotNull(authstate.getAuthScheme());
103:                    assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
104:                    assertEquals("test", authstate.getRealm());
105:                } finally {
106:                    httpget.releaseConnection();
107:                }
108:            }
109:
110:            public void testBasicAuthenticationWithNoCredsRetry()
111:                    throws IOException {
112:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
113:                        "testuser", "testpass");
114:
115:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
116:                handlerchain.appendHandler(new AuthRequestHandler(creds));
117:                handlerchain.appendHandler(new HttpServiceHandler(
118:                        new FeedbackService()));
119:
120:                this .server.setRequestHandler(handlerchain);
121:
122:                GetMethod httpget = new GetMethod("/test/");
123:                try {
124:                    this .client.executeMethod(httpget);
125:                    assertNotNull(httpget.getStatusLine());
126:                    assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget
127:                            .getStatusLine().getStatusCode());
128:                    AuthState authstate = httpget.getHostAuthState();
129:                    assertNotNull(authstate.getAuthScheme());
130:                    assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
131:                    assertEquals("test", authstate.getRealm());
132:                } finally {
133:                    httpget.releaseConnection();
134:                }
135:                // now try with credentials
136:                httpget = new GetMethod("/test/");
137:                try {
138:                    this .client.getState().setCredentials(AuthScope.ANY, creds);
139:                    this .client.executeMethod(httpget);
140:                    assertNotNull(httpget.getStatusLine());
141:                    assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
142:                            .getStatusCode());
143:                } finally {
144:                    httpget.releaseConnection();
145:                }
146:            }
147:
148:            public void testBasicAuthenticationWithNoRealm() {
149:                String challenge = "Basic";
150:                try {
151:                    AuthScheme authscheme = new BasicScheme();
152:                    authscheme.processChallenge(challenge);
153:                    fail("Should have thrown MalformedChallengeException");
154:                } catch (MalformedChallengeException e) {
155:                    // expected
156:                }
157:            }
158:
159:            public void testBasicAuthenticationWith88591Chars()
160:                    throws Exception {
161:                int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
162:                StringBuffer buffer = new StringBuffer();
163:                for (int i = 0; i < germanChars.length; i++) {
164:                    buffer.append((char) germanChars[i]);
165:                }
166:
167:                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
168:                        "dh", buffer.toString());
169:                assertEquals("Basic ZGg65C32Lfw=", BasicScheme.authenticate(
170:                        credentials, "ISO-8859-1"));
171:            }
172:
173:            public void testBasicAuthenticationWithDefaultCreds()
174:                    throws Exception {
175:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
176:                        "testuser", "testpass");
177:
178:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
179:                handlerchain.appendHandler(new AuthRequestHandler(creds));
180:                handlerchain.appendHandler(new HttpServiceHandler(
181:                        new FeedbackService()));
182:
183:                HttpState state = new HttpState();
184:                state.setCredentials(AuthScope.ANY, creds);
185:                this .client.setState(state);
186:
187:                this .server.setRequestHandler(handlerchain);
188:
189:                GetMethod httpget = new GetMethod("/test/");
190:                try {
191:                    this .client.executeMethod(httpget);
192:                } finally {
193:                    httpget.releaseConnection();
194:                }
195:                assertNotNull(httpget.getStatusLine());
196:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
197:                        .getStatusCode());
198:                Header auth = httpget.getRequestHeader("Authorization");
199:                assertNotNull(auth);
200:                String expected = "Basic "
201:                        + EncodingUtil.getAsciiString(Base64
202:                                .encodeBase64(EncodingUtil
203:                                        .getAsciiBytes("testuser:testpass")));
204:                assertEquals(expected, auth.getValue());
205:                AuthState authstate = httpget.getHostAuthState();
206:                assertNotNull(authstate.getAuthScheme());
207:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
208:                assertEquals("test", authstate.getRealm());
209:            }
210:
211:            public void testBasicAuthentication() throws Exception {
212:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
213:                        "testuser", "testpass");
214:
215:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
216:                handlerchain.appendHandler(new AuthRequestHandler(creds));
217:                handlerchain.appendHandler(new HttpServiceHandler(
218:                        new FeedbackService()));
219:
220:                HttpState state = new HttpState();
221:                AuthScope authscope = new AuthScope(this .server
222:                        .getLocalAddress(), this .server.getLocalPort(), "test");
223:                state.setCredentials(authscope, creds);
224:                this .client.setState(state);
225:
226:                this .server.setRequestHandler(handlerchain);
227:
228:                GetMethod httpget = new GetMethod("/test/");
229:                try {
230:                    this .client.executeMethod(httpget);
231:                } finally {
232:                    httpget.releaseConnection();
233:                }
234:                assertNotNull(httpget.getStatusLine());
235:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
236:                        .getStatusCode());
237:                Header auth = httpget.getRequestHeader("Authorization");
238:                assertNotNull(auth);
239:                String expected = "Basic "
240:                        + EncodingUtil.getAsciiString(Base64
241:                                .encodeBase64(EncodingUtil
242:                                        .getAsciiBytes("testuser:testpass")));
243:                assertEquals(expected, auth.getValue());
244:                AuthState authstate = httpget.getHostAuthState();
245:                assertNotNull(authstate.getAuthScheme());
246:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
247:                assertEquals("test", authstate.getRealm());
248:            }
249:
250:            public void testBasicAuthenticationWithInvalidCredentials()
251:                    throws Exception {
252:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
253:                        "testuser", "testpass");
254:
255:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
256:                handlerchain.appendHandler(new AuthRequestHandler(creds));
257:                handlerchain.appendHandler(new HttpServiceHandler(
258:                        new FeedbackService()));
259:
260:                HttpState state = new HttpState();
261:                AuthScope authscope = new AuthScope(this .server
262:                        .getLocalAddress(), this .server.getLocalPort(), "test");
263:                state.setCredentials(authscope,
264:                        new UsernamePasswordCredentials("test", "stuff"));
265:                this .client.setState(state);
266:
267:                this .server.setRequestHandler(handlerchain);
268:
269:                GetMethod httpget = new GetMethod("/test/");
270:                try {
271:                    this .client.executeMethod(httpget);
272:                } finally {
273:                    httpget.releaseConnection();
274:                }
275:                assertNotNull(httpget.getStatusLine());
276:                assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget
277:                        .getStatusLine().getStatusCode());
278:                AuthState authstate = httpget.getHostAuthState();
279:                assertNotNull(authstate.getAuthScheme());
280:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
281:                assertEquals("test", authstate.getRealm());
282:            }
283:
284:            public void testBasicAuthenticationWithMutlipleRealms1()
285:                    throws Exception {
286:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
287:                        "testuser", "testpass");
288:
289:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
290:                handlerchain.appendHandler(new AuthRequestHandler(creds));
291:                handlerchain.appendHandler(new HttpServiceHandler(
292:                        new FeedbackService()));
293:
294:                HttpState state = new HttpState();
295:                AuthScope realm1 = new AuthScope(this .server.getLocalAddress(),
296:                        this .server.getLocalPort(), "test");
297:                AuthScope realm2 = new AuthScope(this .server.getLocalAddress(),
298:                        this .server.getLocalPort(), "test2");
299:                state.setCredentials(realm1, new UsernamePasswordCredentials(
300:                        "testuser", "testpass"));
301:                state.setCredentials(realm2, new UsernamePasswordCredentials(
302:                        "testuser2", "testpass2"));
303:                this .client.setState(state);
304:
305:                this .server.setRequestHandler(handlerchain);
306:
307:                GetMethod httpget = new GetMethod("/test/");
308:                try {
309:                    this .client.executeMethod(httpget);
310:                } finally {
311:                    httpget.releaseConnection();
312:                }
313:                assertNotNull(httpget.getStatusLine());
314:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
315:                        .getStatusCode());
316:                Header auth = httpget.getRequestHeader("Authorization");
317:                assertNotNull(auth);
318:                String expected = "Basic "
319:                        + EncodingUtil.getAsciiString(Base64
320:                                .encodeBase64(EncodingUtil
321:                                        .getAsciiBytes("testuser:testpass")));
322:                assertEquals(expected, auth.getValue());
323:                AuthState authstate = httpget.getHostAuthState();
324:                assertNotNull(authstate.getAuthScheme());
325:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
326:                assertEquals("test", authstate.getRealm());
327:            }
328:
329:            public void testBasicAuthenticationWithMutlipleRealms2()
330:                    throws Exception {
331:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
332:                        "testuser2", "testpass2");
333:
334:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
335:                handlerchain.appendHandler(new AuthRequestHandler(creds,
336:                        "test2"));
337:                handlerchain.appendHandler(new HttpServiceHandler(
338:                        new FeedbackService()));
339:
340:                HttpState state = new HttpState();
341:                AuthScope realm1 = new AuthScope(this .server.getLocalAddress(),
342:                        this .server.getLocalPort(), "test");
343:                AuthScope realm2 = new AuthScope(this .server.getLocalAddress(),
344:                        this .server.getLocalPort(), "test2");
345:                state.setCredentials(realm1, new UsernamePasswordCredentials(
346:                        "testuser", "testpass"));
347:                state.setCredentials(realm2, new UsernamePasswordCredentials(
348:                        "testuser2", "testpass2"));
349:                this .client.setState(state);
350:
351:                this .server.setRequestHandler(handlerchain);
352:
353:                GetMethod httpget = new GetMethod("/test2/");
354:                try {
355:                    this .client.executeMethod(httpget);
356:                } finally {
357:                    httpget.releaseConnection();
358:                }
359:                assertNotNull(httpget.getStatusLine());
360:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
361:                        .getStatusCode());
362:                Header auth = httpget.getRequestHeader("Authorization");
363:                assertNotNull(auth);
364:                String expected = "Basic "
365:                        + EncodingUtil.getAsciiString(Base64
366:                                .encodeBase64(EncodingUtil
367:                                        .getAsciiBytes("testuser2:testpass2")));
368:                assertEquals(expected, auth.getValue());
369:                AuthState authstate = httpget.getHostAuthState();
370:                assertNotNull(authstate.getAuthScheme());
371:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
372:                assertEquals("test2", authstate.getRealm());
373:            }
374:
375:            public void testPreemptiveAuthorizationTrueWithCreds()
376:                    throws Exception {
377:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
378:                        "testuser", "testpass");
379:
380:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
381:                handlerchain.appendHandler(new AuthRequestHandler(creds));
382:                handlerchain.appendHandler(new HttpServiceHandler(
383:                        new FeedbackService()));
384:
385:                HttpState state = new HttpState();
386:                state.setCredentials(AuthScope.ANY, creds);
387:                this .client.setState(state);
388:                this .client.getParams().setAuthenticationPreemptive(true);
389:
390:                this .server.setRequestHandler(handlerchain);
391:
392:                GetMethod httpget = new GetMethod("/test/");
393:                try {
394:                    this .client.executeMethod(httpget);
395:                } finally {
396:                    httpget.releaseConnection();
397:                }
398:                assertNotNull(httpget.getStatusLine());
399:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
400:                        .getStatusCode());
401:                Header auth = httpget.getRequestHeader("Authorization");
402:                assertNotNull(auth);
403:                String expected = "Basic "
404:                        + EncodingUtil.getAsciiString(Base64
405:                                .encodeBase64(EncodingUtil
406:                                        .getAsciiBytes("testuser:testpass")));
407:                assertEquals(expected, auth.getValue());
408:                AuthState authstate = httpget.getHostAuthState();
409:                assertNotNull(authstate.getAuthScheme());
410:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
411:                assertNull(authstate.getRealm());
412:                assertTrue(authstate.isPreemptive());
413:            }
414:
415:            public void testPreemptiveAuthorizationTrueWithoutCreds()
416:                    throws Exception {
417:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
418:                        "testuser", "testpass");
419:
420:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
421:                handlerchain.appendHandler(new AuthRequestHandler(creds));
422:                handlerchain.appendHandler(new HttpServiceHandler(
423:                        new FeedbackService()));
424:
425:                HttpState state = new HttpState();
426:                this .client.setState(state);
427:                this .client.getParams().setAuthenticationPreemptive(true);
428:
429:                this .server.setRequestHandler(handlerchain);
430:
431:                GetMethod httpget = new GetMethod("/test/");
432:                try {
433:                    this .client.executeMethod(httpget);
434:                } finally {
435:                    httpget.releaseConnection();
436:                }
437:                assertNotNull(httpget.getStatusLine());
438:                assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget
439:                        .getStatusLine().getStatusCode());
440:                Header auth = httpget.getRequestHeader("Authorization");
441:                assertNull(auth);
442:                AuthState authstate = httpget.getHostAuthState();
443:                assertNotNull(authstate.getAuthScheme());
444:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
445:                assertNotNull(authstate.getRealm());
446:                assertTrue(authstate.isPreemptive());
447:            }
448:
449:            public void testCustomAuthorizationHeader() throws Exception {
450:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
451:                        "testuser", "testpass");
452:
453:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
454:                handlerchain.appendHandler(new AuthRequestHandler(creds));
455:                handlerchain.appendHandler(new HttpServiceHandler(
456:                        new FeedbackService()));
457:
458:                this .server.setRequestHandler(handlerchain);
459:
460:                GetMethod httpget = new GetMethod("/test/");
461:                String authResponse = "Basic "
462:                        + EncodingUtil.getAsciiString(Base64
463:                                .encodeBase64(EncodingUtil
464:                                        .getAsciiBytes("testuser:testpass")));
465:                httpget.addRequestHeader(new Header("Authorization",
466:                        authResponse));
467:                try {
468:                    this .client.executeMethod(httpget);
469:                } finally {
470:                    httpget.releaseConnection();
471:                }
472:                assertNotNull(httpget.getStatusLine());
473:                assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
474:                        .getStatusCode());
475:            }
476:
477:            public void testHeadBasicAuthentication() throws Exception {
478:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
479:                        "testuser", "testpass");
480:
481:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
482:                handlerchain.appendHandler(new AuthRequestHandler(creds));
483:                handlerchain.appendHandler(new HttpServiceHandler(
484:                        new FeedbackService()));
485:
486:                HttpState state = new HttpState();
487:                AuthScope authscope = new AuthScope(this .server
488:                        .getLocalAddress(), this .server.getLocalPort(), "test");
489:                state.setCredentials(authscope, creds);
490:                this .client.setState(state);
491:
492:                this .server.setRequestHandler(handlerchain);
493:
494:                HeadMethod head = new HeadMethod("/test/");
495:                try {
496:                    this .client.executeMethod(head);
497:                } finally {
498:                    head.releaseConnection();
499:                }
500:                assertNotNull(head.getStatusLine());
501:                assertEquals(HttpStatus.SC_OK, head.getStatusLine()
502:                        .getStatusCode());
503:                Header auth = head.getRequestHeader("Authorization");
504:                assertNotNull(auth);
505:                String expected = "Basic "
506:                        + EncodingUtil.getAsciiString(Base64
507:                                .encodeBase64(EncodingUtil
508:                                        .getAsciiBytes("testuser:testpass")));
509:                assertEquals(expected, auth.getValue());
510:                AuthState authstate = head.getHostAuthState();
511:                assertNotNull(authstate.getAuthScheme());
512:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
513:                assertEquals("test", authstate.getRealm());
514:            }
515:
516:            public void testPostBasicAuthentication() throws Exception {
517:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
518:                        "testuser", "testpass");
519:
520:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
521:                handlerchain.appendHandler(new AuthRequestHandler(creds));
522:                handlerchain.appendHandler(new HttpServiceHandler(
523:                        new EchoService()));
524:
525:                HttpState state = new HttpState();
526:                AuthScope authscope = new AuthScope(this .server
527:                        .getLocalAddress(), this .server.getLocalPort(), "test");
528:                state.setCredentials(authscope, creds);
529:                this .client.setState(state);
530:
531:                this .server.setRequestHandler(handlerchain);
532:
533:                PostMethod post = new PostMethod("/test/");
534:                post.setRequestEntity(new StringRequestEntity("Test body",
535:                        null, null));
536:                try {
537:                    this .client.executeMethod(post);
538:                    assertEquals("Test body", post.getResponseBodyAsString());
539:                } finally {
540:                    post.releaseConnection();
541:                }
542:                assertNotNull(post.getStatusLine());
543:                assertEquals(HttpStatus.SC_OK, post.getStatusLine()
544:                        .getStatusCode());
545:                Header auth = post.getRequestHeader("Authorization");
546:                assertNotNull(auth);
547:                String expected = "Basic "
548:                        + EncodingUtil.getAsciiString(Base64
549:                                .encodeBase64(EncodingUtil
550:                                        .getAsciiBytes("testuser:testpass")));
551:                assertEquals(expected, auth.getValue());
552:                AuthState authstate = post.getHostAuthState();
553:                assertNotNull(authstate.getAuthScheme());
554:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
555:                assertEquals("test", authstate.getRealm());
556:            }
557:
558:            public void testPutBasicAuthentication() throws Exception {
559:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
560:                        "testuser", "testpass");
561:
562:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
563:                handlerchain.appendHandler(new AuthRequestHandler(creds));
564:                handlerchain.appendHandler(new HttpServiceHandler(
565:                        new EchoService()));
566:
567:                HttpState state = new HttpState();
568:                AuthScope authscope = new AuthScope(this .server
569:                        .getLocalAddress(), this .server.getLocalPort(), "test");
570:                state.setCredentials(authscope, creds);
571:                this .client.setState(state);
572:
573:                this .server.setRequestHandler(handlerchain);
574:
575:                PutMethod put = new PutMethod("/test/");
576:                put.setRequestEntity(new StringRequestEntity("Test body", null,
577:                        null));
578:                try {
579:                    this .client.executeMethod(put);
580:                    assertEquals("Test body", put.getResponseBodyAsString());
581:                } finally {
582:                    put.releaseConnection();
583:                }
584:                assertNotNull(put.getStatusLine());
585:                assertEquals(HttpStatus.SC_OK, put.getStatusLine()
586:                        .getStatusCode());
587:                Header auth = put.getRequestHeader("Authorization");
588:                assertNotNull(auth);
589:                String expected = "Basic "
590:                        + EncodingUtil.getAsciiString(Base64
591:                                .encodeBase64(EncodingUtil
592:                                        .getAsciiBytes("testuser:testpass")));
593:                assertEquals(expected, auth.getValue());
594:                AuthState authstate = put.getHostAuthState();
595:                assertNotNull(authstate.getAuthScheme());
596:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
597:                assertEquals("test", authstate.getRealm());
598:            }
599:
600:            public void testPreemptiveAuthorizationFailure() throws Exception {
601:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
602:                        "testuser", "testpass");
603:                UsernamePasswordCredentials wrongcreds = new UsernamePasswordCredentials(
604:                        "testuser", "garbage");
605:
606:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
607:                handlerchain.appendHandler(new AuthRequestHandler(creds));
608:                handlerchain.appendHandler(new HttpServiceHandler(
609:                        new FeedbackService()));
610:
611:                HttpState state = new HttpState();
612:                state.setCredentials(AuthScope.ANY, wrongcreds);
613:                this .client.setState(state);
614:                this .client.getParams().setAuthenticationPreemptive(true);
615:
616:                this .server.setRequestHandler(handlerchain);
617:
618:                GetMethod httpget = new GetMethod("/test/");
619:                try {
620:                    this .client.executeMethod(httpget);
621:                } finally {
622:                    httpget.releaseConnection();
623:                }
624:                assertNotNull(httpget.getStatusLine());
625:                assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget
626:                        .getStatusLine().getStatusCode());
627:                AuthState authstate = httpget.getHostAuthState();
628:                assertNotNull(authstate.getAuthScheme());
629:                assertTrue(authstate.getAuthScheme() instanceof  BasicScheme);
630:                assertEquals("test", authstate.getRealm());
631:                assertTrue(authstate.isPreemptive());
632:            }
633:
634:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.