1
+ package org .json .junit ;
2
+
3
+ import org .json .HTTPTokener ;
4
+ import org .json .JSONException ;
5
+ import org .junit .Test ;
6
+
7
+ import static org .junit .Assert .*;
8
+ /**
9
+ * Tests for JSON-Java HTTPTokener.java
10
+ */
11
+ public class HTTPTokenerTest {
12
+
13
+ /**
14
+ * Test parsing a simple unquoted token.
15
+ */
16
+ @ Test
17
+ public void parseSimpleToken () {
18
+ HTTPTokener tokener = new HTTPTokener ("Content-Type" );
19
+ String token = tokener .nextToken ();
20
+ assertEquals ("Content-Type" , token );
21
+ }
22
+
23
+ /**
24
+ * Test parsing multiple tokens separated by whitespace.
25
+ */
26
+ @ Test
27
+ public void parseMultipleTokens () {
28
+ HTTPTokener tokener = new HTTPTokener ("Content-Type application/json" );
29
+ String token1 = tokener .nextToken ();
30
+ String token2 = tokener .nextToken ();
31
+ assertEquals ("Content-Type" , token1 );
32
+ assertEquals ("application/json" , token2 );
33
+ }
34
+
35
+ /**
36
+ * Test parsing a double-quoted token.
37
+ */
38
+ @ Test
39
+ public void parseDoubleQuotedToken () {
40
+ HTTPTokener tokener = new HTTPTokener ("\" application/json\" " );
41
+ String token = tokener .nextToken ();
42
+ assertEquals ("application/json" , token );
43
+ }
44
+
45
+ /**
46
+ * Test parsing a single-quoted token.
47
+ */
48
+ @ Test
49
+ public void parseSingleQuotedToken () {
50
+ HTTPTokener tokener = new HTTPTokener ("'application/json'" );
51
+ String token = tokener .nextToken ();
52
+ assertEquals ("application/json" , token );
53
+ }
54
+
55
+ /**
56
+ * Test parsing a quoted token that includes spaces and semicolons.
57
+ */
58
+ @ Test
59
+ public void parseQuotedTokenWithSpaces () {
60
+ HTTPTokener tokener = new HTTPTokener ("\" text/html; charset=UTF-8\" " );
61
+ String token = tokener .nextToken ();
62
+ assertEquals ("text/html; charset=UTF-8" , token );
63
+ }
64
+
65
+ /**
66
+ * Test that unterminated quoted strings throw a JSONException.
67
+ */
68
+ @ Test
69
+ public void throwExceptionOnUnterminatedString () {
70
+ HTTPTokener tokener = new HTTPTokener ("\" incomplete" );
71
+ JSONException exception = assertThrows (JSONException .class , tokener ::nextToken );
72
+ assertTrue (exception .getMessage ().contains ("Unterminated string" ));
73
+ }
74
+
75
+ /**
76
+ * Test behavior with empty input string.
77
+ */
78
+ @ Test
79
+ public void parseEmptyInput () {
80
+ HTTPTokener tokener = new HTTPTokener ("" );
81
+ String token = tokener .nextToken ();
82
+ assertEquals ("" , token );
83
+ }
84
+
85
+ /**
86
+ * Test behavior with input consisting only of whitespace.
87
+ */
88
+ @ Test
89
+ public void parseWhitespaceOnly () {
90
+ HTTPTokener tokener = new HTTPTokener (" \t \n " );
91
+ String token = tokener .nextToken ();
92
+ assertEquals ("" , token );
93
+ }
94
+
95
+ /**
96
+ * Test parsing tokens separated by multiple whitespace characters.
97
+ */
98
+ @ Test
99
+ public void parseTokensWithMultipleWhitespace () {
100
+ HTTPTokener tokener = new HTTPTokener ("GET /index.html" );
101
+ String method = tokener .nextToken ();
102
+ String path = tokener .nextToken ();
103
+ assertEquals ("GET" , method );
104
+ assertEquals ("/index.html" , path );
105
+ }
106
+
107
+ }
0 commit comments