Debian Codegraph @

jetty-ee9/jetty-ee9-demos/jetty-ee9-demo-jetty-webapp/src/test/java/org/eclipse/jetty/ee9/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.ee9;
15
16 import org.eclipse.jetty.ee9.servlet.ServletContextHandler;
17 import org.eclipse.jetty.ee9.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(server, "/");
40 ServletHolder dispatch = context.addServlet(ChatServlet.class, "/chat/*");
41 dispatch.setInitParameter("asyncTimeout", "500");
42 server.start();
43 }
44
45 @AfterEach
46 public void tearDown() throws Exception
47 {
48 server.stop();
49 }
50
51 @Test
52 public void testLogin() throws Exception
53 {
54 assertResponse("user=test&join=true&message=has%20joined!", "{\"from\":\"test\",\"chat\":\"has joined!\"}");
55 }
56
57 @Test
58 public void testChat() throws Exception
59 {
60 assertResponse("user=test&join=true&message=has%20joined!", "{\"from\":\"test\",\"chat\":\"has joined!\"}");
61 String response = connector.getResponse(createRequestString("user=test&message=message"));
62 assertThat(response.contains("{"), is(false)); // make sure we didn't get a json body
63 }
64
65 @Test
66 public void testPoll() throws Exception
67 {
68 assertResponse("user=test", "{action:\"poll\"}");
69 }
70
71 private void assertResponse(String requestBody, String expectedResponse) throws Exception
72 {
73 String response = connector.getResponse(createRequestString(requestBody));
74 assertThat(response.contains(expectedResponse), is(true));
75 }
76
77 private String createRequestString(String body)
78 {
79 return "POST /chat/ HTTP/1.1\r\n" +
80 "Host: tester\r\n" +
81 "Content-length: " + body.length() + "\r\n" +
82 "Content-type: application/x-www-form-urlencoded\r\n" +
83 "Connection: close\r\n" +
84 "\r\n" +
85 body;
86 }
87 }