1+ /*
2+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+ *
4+ * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+ *
6+ * The contents of this file are subject to the terms of either the GNU
7+ * General Public License Version 2 only ("GPL") or the Common Development
8+ * and Distribution License("CDDL") (collectively, the "License"). You
9+ * may not use this file except in compliance with the License. You can
10+ * obtain a copy of the License at
11+ * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+ * or packager/legal/LICENSE.txt. See the License for the specific
13+ * language governing permissions and limitations under the License.
14+ *
15+ * When distributing the software, include this License Header Notice in each
16+ * file and include the License file at packager/legal/LICENSE.txt.
17+ *
18+ * GPL Classpath Exception:
19+ * Oracle designates this particular file as subject to the "Classpath"
20+ * exception as provided by Oracle in the GPL Version 2 section of the License
21+ * file that accompanied this code.
22+ *
23+ * Modifications:
24+ * If applicable, add the following below the License Header, with the fields
25+ * enclosed by brackets [] replaced by your own identifying information:
26+ * "Portions Copyright [year] [name of copyright owner]"
27+ *
28+ * Contributor(s):
29+ * If you wish your version of this file to be governed by only the CDDL or
30+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
31+ * elects to include this software in this distribution under the [CDDL or GPL
32+ * Version 2] license." If you don't indicate a single choice of license, a
33+ * recipient has the option to distribute your version of this file under
34+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
35+ * its licensees as provided above. However, if you add GPL Version 2 code
36+ * and therefore, elected the GPL Version 2 license, then the option applies
37+ * only if the new code is made subject to such option by the copyright
38+ * holder.
39+ */
40+ package org .glassfish .jersey .media .sse ;
41+
42+ import java .io .ByteArrayInputStream ;
43+ import java .io .IOException ;
44+ import java .io .InputStream ;
45+ import java .lang .annotation .Annotation ;
46+ import java .nio .charset .Charset ;
47+ import java .util .Collections ;
48+
49+ import javax .ws .rs .core .MediaType ;
50+
51+ import org .glassfish .jersey .internal .util .collection .MultivaluedStringMap ;
52+
53+ import org .glassfish .hk2 .api .ServiceLocator ;
54+ import org .glassfish .hk2 .utilities .ServiceLocatorUtilities ;
55+ import org .glassfish .hk2 .utilities .binding .AbstractBinder ;
56+
57+ import org .junit .Test ;
58+ import static org .junit .Assert .assertEquals ;
59+ import static org .junit .Assert .assertNull ;
60+
61+ /**
62+ * @author Petr Bouda (petr.bouda at oracle.com)
63+ */
64+ public class InboundEventReaderTest {
65+
66+ private static final MultivaluedStringMap headers ;
67+
68+ private static final ServiceLocator locator ;
69+
70+ static {
71+ headers = new MultivaluedStringMap ();
72+ headers .put ("Transfer-Encoding" , Collections .singletonList ("chunked" ));
73+ headers .put ("Content-Type" , Collections .singletonList ("text/event-stream" ));
74+
75+ locator = ServiceLocatorUtilities .bind (new TestBinder ());
76+ }
77+
78+ @ Test
79+ public void testReadWithStartsWithLF () throws Exception {
80+ InboundEvent event = parse (new ByteArrayInputStream ("\n event: custom-message" .getBytes ()));
81+ assertNull (event .getName ());
82+ assertEquals (0 , event .getRawData ().length );
83+ }
84+
85+ @ Test
86+ public void testReadWithStartsWithCR () throws Exception {
87+ InboundEvent event = parse (new ByteArrayInputStream ("\r event: custom-message" .getBytes ()));
88+ assertNull (event .getName ());
89+ assertEquals (0 , event .getRawData ().length );
90+ }
91+
92+ @ Test
93+ public void testReadWithLF () throws Exception {
94+ ByteArrayInputStream inputStream = new ByteArrayInputStream ("event: custom-message\n data: message 1" .getBytes ());
95+ InboundEvent event = parse (inputStream );
96+ assertEquals ("custom-message" , event .getName ());
97+ assertEquals ("message 1\n " , new String (event .getRawData (), Charset .defaultCharset ()));
98+ }
99+
100+ @ Test
101+ public void testReadWithCRLF () throws Exception {
102+ ByteArrayInputStream inputStream = new ByteArrayInputStream ("event: custom-message\r \n data: message 1" .getBytes ());
103+ InboundEvent event = parse (inputStream );
104+ assertEquals ("custom-message" , event .getName ());
105+ assertEquals ("message 1\n " , new String (event .getRawData (), Charset .defaultCharset ()));
106+ }
107+
108+ @ Test
109+ public void testReadWithCR () throws Exception {
110+ ByteArrayInputStream inputStream = new ByteArrayInputStream ("event: custom-message\r data: message 1" .getBytes ());
111+ InboundEvent event = parse (inputStream );
112+ assertEquals ("custom-message" , event .getName ());
113+ assertEquals ("message 1\n " , new String (event .getRawData (), Charset .defaultCharset ()));
114+ }
115+
116+ @ Test
117+ public void testReadWithMultipleSpaces () throws Exception {
118+ ByteArrayInputStream inputStream = new ByteArrayInputStream ("event: custom-message\r data: message 1" .getBytes ());
119+ InboundEvent event = parse (inputStream );
120+ assertEquals ("custom-message" , event .getName ());
121+ assertEquals ("message 1\n " , new String (event .getRawData (), Charset .defaultCharset ()));
122+ }
123+
124+ @ Test
125+ public void testReadWithMultipleEndingDelimiter () throws Exception {
126+ ByteArrayInputStream inputStream = new ByteArrayInputStream ("event: custom-message\r data: message 1\r " .getBytes ());
127+ InboundEvent event = parse (inputStream );
128+ assertEquals ("custom-message" , event .getName ());
129+ assertEquals ("message 1\n " , new String (event .getRawData (), Charset .defaultCharset ()));
130+ }
131+
132+ private static InboundEvent parse (InputStream stream ) throws IOException {
133+ return locator .getService (InboundEventReader .class ).readFrom (InboundEvent .class , InboundEvent .class , new Annotation [0 ],
134+ MediaType .valueOf (SseFeature .SERVER_SENT_EVENTS ), headers , stream );
135+ }
136+
137+ private static class TestBinder extends AbstractBinder {
138+
139+ @ Override
140+ protected void configure () {
141+ bind (InboundEventReader .class )
142+ .to (InboundEventReader .class );
143+ }
144+ }
145+ }
0 commit comments