Debian Codegraph @

jetty-ee10/jetty-ee10-demos/jetty-ee10-demo-jetty-webapp/src/test/java/org/eclipse/jetty/ee10/ChatServletTest.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4 //
5 // This program and the accompanying materials are made available under the
6 // terms of the Eclipse Public License v. 2.0 which is available at
7 // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8 // which is available at https://www.apache.org/licenses/LICENSE-2.0.
9 //
10 // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11 // ========================================================================
12 //
13
14 package org.eclipse.jetty.ee10;
15
16 import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
17 import org.eclipse.jetty.ee10.servlet.ServletHolder;
18 import org.eclipse.jetty.server.LocalConnector;
19 import org.eclipse.jetty.server.Server;
20 import org.example.ChatServlet;
21 import org.junit.jupiter.api.AfterEach;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
27
28 public class ChatServletTest
29 {
30 private Server server;
31 private LocalConnector connector;
32
33 @BeforeEach
34 public void setUp() throws Exception
35 {
36 server = new Server();
37 connector = new LocalConnector(server);
38 server.addConnector(connector);
39 ServletContextHandler context = new ServletContextHandler("/");
40 server.setHandler(context);
41 ServletHolder dispatch = context.addServlet(ChatServlet.class, "/chat/*");
42 dispatch.setInitParameter("asyncTimeout", "500");
43 server.start();
44 }
45
46 @AfterEach
47 public void tearDown() throws Exception
48 {
49 server.stop();
50 }
51
52 @Test
53 public void testLogin() throws Exception
54 {
55 assertResponse("user=test&join=true&message=has%20joined!", "{\"from\":\"test\",\"chat\":\"has joined!\"}");
56 }
57
58 @Test
59 public void testChat() throws Exception
60 {
61 assertResponse("user=test&join=true&message=has%20joined!", "{\"from\":\"test\",\"chat\":\"has joined!\"}");
62 String response = connector.getResponse(createRequestString("user=test&message=message"));
63 assertThat(response.contains("{"), is(false)); // make sure we didn't get a json body
64 }
65
66 @Test
67 public void testPoll() throws Exception
68 {
69 assertResponse("user=test", "{action:\"poll\"}");
70 }
71
72 private void assertResponse(String requestBody, String expectedResponse) throws Exception
73 {
74 String response = connector.getResponse(createRequestString(requestBody));
75 assertThat(response.contains(expectedResponse), is(true));
76 }
77
78 private String createRequestString(String body)
79 {
80 return "POST /chat/ HTTP/1.1\r\n" +
81 "Host: tester\r\n" +
82 "Content-length: " + body.length() + "\r\n" +
83 "Content-type: application/x-www-form-urlencoded\r\n" +
84 "Connection: close\r\n" +
85 "\r\n" +
86 body;
87 }
88 }