forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCookie.java
More file actions
138 lines (127 loc) · 4.15 KB
/
TestCookie.java
File metadata and controls
138 lines (127 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* File: TestCookie.java
* Author: JSON.org
*/
package org.json.tests;
import org.json.Cookie;
import org.json.JSONException;
import org.json.JSONObject;
import junit.framework.TestCase;
/**
* The Class TestCookie.
*/
public class TestCookie extends TestCase
{
JSONObject jsonobject;
/**
* Tests the toJsonObject method using random cookie data.
*/
public static void testToJsonObject_RandomCookieData()
{
try
{
JSONObject jsonobject = new JSONObject();
jsonobject = Cookie
.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
assertEquals("{\n" + " \"expires\": \"April 24, 2002\",\n"
+ " \"name\": \"f%oo\",\n" + " \"secure\": true,\n"
+ " \"value\": \"blah\"\n" + "}", jsonobject.toString(2));
assertEquals("f%25oo=blah;expires=April 24, 2002;secure",
Cookie.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the escape and unescape method.
*/
public static void testEscape()
{
StringBuilder testString = new StringBuilder();
testString.append('h');
for(int i = 0; i < ' '; i++)
testString.append((char)i);
testString.append('\n');
testString.append('\t');
testString.append('\b');
testString.append('%');
testString.append('+');
testString.append('=');
testString.append(';');
String result = "h%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%0a%09%08%25%2b%3d%3b";
assertEquals(result, Cookie.escape(testString.toString()));
}
/**
* Tests the unescape method.
*/
public static void testUnescape()
{
StringBuilder testString = new StringBuilder();
testString.append('h');
for(int i = 0; i < ' '; i++)
testString.append((char)i);
testString.append('\n');
testString.append('\t');
testString.append('\b');
testString.append('%');
testString.append('+');
testString.append('%');
testString.append('0');
testString.append('\r');
testString.append(' ');
testString.append(' ');
testString.append('%');
testString.append('\n');
testString.append('z');
testString.append('z');
testString.append('=');
testString.append(';');
testString.append('%');
String result = "h%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%0a%09%08%25%2b%0\r +%\nzz%3d%3b%";
assertEquals(testString.toString(), Cookie.unescape(result));
}
/**
* Tests the toJsonObject method using value without equals.
*/
public void testToJsonObject_ValueWithoutEquals()
{
try
{
jsonobject = Cookie
.toJSONObject("f%oo=blah; notsecure ;expires = April 24, 2002");
fail("Should have thrown exception.");
} catch (JSONException e)
{
assertEquals("Missing '=' in cookie parameter. at 22 [character 23 line 1]", e.getMessage());
}
}
/**
* Tests the toString method.
*/
public static void testToString()
{
try
{
JSONObject jsonobject = new JSONObject();
jsonobject.put("secure", true);
jsonobject.put("expires", "string1");
jsonobject.put("domain", "string2");
jsonobject.put("path", "string3");
jsonobject.put("name", "foo");
jsonobject.put("value", "bar");
assertEquals("foo=bar;expires=string1;domain=string2;path=string3;secure", Cookie.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the constructor method.
*/
public static void testConstructor()
{
Cookie cookie = new Cookie();
assertEquals("Cookie", cookie.getClass().getSimpleName());
}
}