1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.proxy;
21
22 import java.util.HashMap;
23
24 import junit.framework.TestCase;
25
26 import org.apache.mina.core.session.DummySession;
27 import org.apache.mina.proxy.handlers.http.basic.HttpBasicAuthLogicHandler;
28 import org.apache.mina.proxy.handlers.http.digest.DigestUtilities;
29
30
31
32
33
34
35
36
37 public class HttpAuthTest extends TestCase {
38
39
40
41
42 private final static String CHARSET_IN_USE = "ISO-8859-1";
43
44
45
46
47 public void testBasicAuthResponse() {
48 String USER = "Aladdin";
49 String PWD = "open sesame";
50
51 assertEquals("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", HttpBasicAuthLogicHandler
52 .createAuthorization(USER, PWD));
53 }
54
55
56
57
58 public void testDigestAuthResponse() {
59 String USER = "Mufasa";
60 String PWD = "Circle Of Life";
61 String METHOD = "GET";
62
63 HashMap<String, String> map = new HashMap<String, String>();
64
65 map.put("realm", "testrealm@host.com");
66 map.put("qop", "auth");
67 map.put("nc", "00000001");
68
69 map.put("cnonce", "0a4f113b");
70
71 map.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093");
72 map.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41");
73 map.put("uri", "/dir/index.html");
74 map.put("username", USER);
75
76 String response = null;
77 try {
78 response = DigestUtilities.computeResponseValue(new DummySession(),
79 map, METHOD, PWD, CHARSET_IN_USE, null);
80 assertEquals("6629fae49393a05397450978507c4ef1", response);
81 writeResponse(map, response);
82 } catch (Exception e) {
83
84 }
85 }
86
87
88
89
90
91
92
93 private void writeResponse(HashMap<String, String> map, String response) {
94 map.put("response", response);
95 StringBuilder sb = new StringBuilder("Digest ");
96 boolean addSeparator = false;
97
98 for (String key : map.keySet()) {
99
100 if (addSeparator) {
101 sb.append(",\n\t\t\t ");
102 } else {
103 addSeparator = true;
104 }
105
106 boolean quotedValue = !"qop".equals(key) && !"nc".equals(key);
107 sb.append(key);
108 if (quotedValue) {
109 sb.append("=\"").append(map.get(key)).append('\"');
110 } else {
111 sb.append('=').append(map.get(key));
112 }
113 }
114
115
116 }
117 }