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