1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+ package org .apache .felix .http .itest .servletapi3 ;
20
+
21
+ import static org .junit .Assert .assertEquals ;
22
+ import static org .junit .Assert .assertTrue ;
23
+ import static org .osgi .service .http .whiteboard .HttpWhiteboardConstants .HTTP_WHITEBOARD_SERVLET_PATTERN ;
24
+
25
+ import java .io .IOException ;
26
+ import java .io .InputStream ;
27
+ import java .io .PrintWriter ;
28
+ import java .net .HttpURLConnection ;
29
+ import java .util .ArrayList ;
30
+ import java .util .Dictionary ;
31
+ import java .util .Hashtable ;
32
+ import java .util .List ;
33
+ import java .util .concurrent .CountDownLatch ;
34
+ import java .util .concurrent .TimeUnit ;
35
+
36
+ import javax .servlet .Servlet ;
37
+ import javax .servlet .http .HttpServletRequest ;
38
+ import javax .servlet .http .HttpServletResponse ;
39
+
40
+ import org .junit .After ;
41
+ import org .junit .Test ;
42
+ import org .junit .runner .RunWith ;
43
+ import org .ops4j .pax .exam .junit .PaxExam ;
44
+ import org .ops4j .pax .exam .spi .reactors .ExamReactorStrategy ;
45
+ import org .ops4j .pax .exam .spi .reactors .PerMethod ;
46
+ import org .osgi .framework .ServiceRegistration ;
47
+
48
+ @ RunWith (PaxExam .class )
49
+ @ ExamReactorStrategy (PerMethod .class )
50
+ public class ServletContentTest extends Servlet3BaseIntegrationTest {
51
+
52
+ private static final String CONTENT = "myservletcontent" ;
53
+
54
+ private List <ServiceRegistration <?>> registrations = new ArrayList <>();
55
+
56
+ private CountDownLatch initLatch ;
57
+ private CountDownLatch destroyLatch ;
58
+
59
+ public void setupLatches (int count ) {
60
+ initLatch = new CountDownLatch (count );
61
+ destroyLatch = new CountDownLatch (count );
62
+ }
63
+
64
+ public void setupServlet (final String path ) throws Exception {
65
+ Dictionary <String , Object > servletProps = new Hashtable <>();
66
+ servletProps .put (HTTP_WHITEBOARD_SERVLET_PATTERN , path );
67
+
68
+ TestServlet servletWithErrorCode = new TestServlet (initLatch , destroyLatch ) {
69
+ private static final long serialVersionUID = 1L ;
70
+
71
+ @ Override
72
+ protected void doGet (HttpServletRequest req , HttpServletResponse resp )
73
+ throws IOException {
74
+ resp .setContentType ("text/plain" );
75
+ resp .setCharacterEncoding ("utf-8" );
76
+ PrintWriter writer = resp .getWriter ();
77
+ resp .setCharacterEncoding ("utf-16" );
78
+ writer .print (CONTENT );
79
+ }
80
+ };
81
+
82
+ registrations .add (m_context .registerService (Servlet .class .getName (), servletWithErrorCode , servletProps ));
83
+ }
84
+
85
+ @ After
86
+ public void unregisterServices () throws InterruptedException {
87
+ for (ServiceRegistration <?> serviceRegistration : registrations ) {
88
+ serviceRegistration .unregister ();
89
+ }
90
+
91
+ assertTrue (destroyLatch .await (5 , TimeUnit .SECONDS ));
92
+
93
+ Thread .sleep (500 );
94
+ }
95
+
96
+ protected void assertContent (final String path ) throws IOException {
97
+ HttpURLConnection conn = (HttpURLConnection ) createURL (path ).openConnection ();
98
+
99
+ int rc = conn .getResponseCode ();
100
+ assertEquals ("Unexpected response code," , 200 , rc );
101
+
102
+ assertEquals ("text/plain;charset=utf-8" , conn .getHeaderField ("content-type" ));
103
+ try (InputStream is = conn .getInputStream ()) {
104
+ assertEquals (CONTENT , slurpAsString (is ));
105
+ } finally {
106
+ conn .disconnect ();
107
+ }
108
+ }
109
+
110
+ @ Test
111
+ public void testContentAndHeaders () throws Exception {
112
+ setupLatches (1 );
113
+
114
+ setupServlet ("/myservlet" );
115
+
116
+ assertTrue (initLatch .await (5 , TimeUnit .SECONDS ));
117
+
118
+ assertContent ("/myservlet" );
119
+ }
120
+ }
121
+
0 commit comments