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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.api.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.junit.Assert;
import org.junit.Test;
/**
* Test QuoteUtil
*/
public class QuoteUtilTest
{
private void assertSplitAt(Iterator<String> iter, String... expectedParts)
{
int len = expectedParts.length;
for (int i = 0; i < len; i++)
{
String expected = expectedParts[i];
Assert.assertThat("Split[" + i + "].hasNext()",iter.hasNext(),is(true));
Assert.assertThat("Split[" + i + "].next()",iter.next(),is(expected));
}
}
@Test
public void testSplitAt_PreserveQuoting()
{
Iterator<String> iter = QuoteUtil.splitAt("permessage-compress; method=\"foo, bar\"",";");
assertSplitAt(iter,"permessage-compress","method=\"foo, bar\"");
}
@Test
public void testSplitAt_PreserveQuotingWithNestedDelim()
{
Iterator<String> iter = QuoteUtil.splitAt("permessage-compress; method=\"foo; x=10\"",";");
assertSplitAt(iter,"permessage-compress","method=\"foo; x=10\"");
}
@Test(expected = NoSuchElementException.class)
public void testSplitAtAllWhitespace()
{
Iterator<String> iter = QuoteUtil.splitAt(" ","=");
Assert.assertThat("Has Next",iter.hasNext(),is(false));
iter.next(); // should trigger NoSuchElementException
}
@Test(expected = NoSuchElementException.class)
public void testSplitAtEmpty()
{
Iterator<String> iter = QuoteUtil.splitAt("","=");
Assert.assertThat("Has Next",iter.hasNext(),is(false));
iter.next(); // should trigger NoSuchElementException
}
@Test
public void testSplitAtHelloWorld()
{
Iterator<String> iter = QuoteUtil.splitAt("Hello World"," =");
assertSplitAt(iter,"Hello","World");
}
@Test
public void testSplitAtKeyValue_Message()
{
Iterator<String> iter = QuoteUtil.splitAt("method=\"foo, bar\"","=");
assertSplitAt(iter,"method","foo, bar");
}
@Test
public void testSplitAtQuotedDelim()
{
// test that split ignores delimiters that occur within a quoted
// part of the sequence.
Iterator<String> iter = QuoteUtil.splitAt("A,\"B,C\",D",",");
assertSplitAt(iter,"A","B,C","D");
}
@Test
public void testSplitAtSimple()
{
Iterator<String> iter = QuoteUtil.splitAt("Hi","=");
assertSplitAt(iter,"Hi");
}
@Test
public void testSplitKeyValue_Quoted()
{
Iterator<String> iter = QuoteUtil.splitAt("Key = \"Value\"","=");
assertSplitAt(iter,"Key","Value");
}
@Test
public void testSplitKeyValue_QuotedValueList()
{
Iterator<String> iter = QuoteUtil.splitAt("Fruit = \"Apple, Banana, Cherry\"","=");
assertSplitAt(iter,"Fruit","Apple, Banana, Cherry");
}
@Test
public void testSplitKeyValue_QuotedWithDelim()
{
Iterator<String> iter = QuoteUtil.splitAt("Key = \"Option=Value\"","=");
assertSplitAt(iter,"Key","Option=Value");
}
@Test
public void testSplitKeyValue_Simple()
{
Iterator<String> iter = QuoteUtil.splitAt("Key=Value","=");
assertSplitAt(iter,"Key","Value");
}
@Test
public void testSplitKeyValue_WithWhitespace()
{
Iterator<String> iter = QuoteUtil.splitAt("Key = Value","=");
assertSplitAt(iter,"Key","Value");
}
@Test
public void testQuoteIfNeeded()
{
StringBuilder buf = new StringBuilder();
QuoteUtil.quoteIfNeeded(buf, "key",",");
assertThat("key",buf.toString(),is("key"));
}
@Test
public void testQuoteIfNeeded_null()
{
StringBuilder buf = new StringBuilder();
QuoteUtil.quoteIfNeeded(buf, null,";=");
assertThat("<null>",buf.toString(),is(""));
}
}
|