|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. |
| 4 | +// ------------------------------------------------------------------------ |
| 5 | +// All rights reserved. This program and the accompanying materials |
| 6 | +// are made available under the terms of the Eclipse Public License v1.0 |
| 7 | +// and Apache License v2.0 which accompanies this distribution. |
| 8 | +// |
| 9 | +// The Eclipse Public License is available at |
| 10 | +// http://www.eclipse.org/legal/epl-v10.html |
| 11 | +// |
| 12 | +// The Apache License v2.0 is available at |
| 13 | +// http://www.opensource.org/licenses/apache2.0.php |
| 14 | +// |
| 15 | +// You may elect to redistribute this code under either of these licenses. |
| 16 | +// ======================================================================== |
| 17 | +// |
| 18 | + |
| 19 | +package org.eclipse.jetty.server.handler; |
| 20 | + |
| 21 | +import static org.hamcrest.Matchers.containsString; |
| 22 | +import static org.hamcrest.Matchers.not; |
| 23 | + |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.net.Socket; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | + |
| 28 | +import org.eclipse.jetty.http.HttpHeader; |
| 29 | +import org.eclipse.jetty.http.HttpStatus; |
| 30 | +import org.eclipse.jetty.http.HttpTester; |
| 31 | +import org.eclipse.jetty.server.Handler; |
| 32 | +import org.eclipse.jetty.server.Server; |
| 33 | +import org.eclipse.jetty.server.ServerConnector; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +public class DefaultHandlerTest |
| 40 | +{ |
| 41 | + private Server server; |
| 42 | + private ServerConnector connector; |
| 43 | + private DefaultHandler handler; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void before() throws Exception |
| 47 | + { |
| 48 | + server = new Server(); |
| 49 | + connector = new ServerConnector(server); |
| 50 | + server.addConnector(connector); |
| 51 | + |
| 52 | + |
| 53 | + ContextHandlerCollection contexts = new ContextHandlerCollection(); |
| 54 | + handler = new DefaultHandler(); |
| 55 | + HandlerCollection handlers = new HandlerCollection(); |
| 56 | + handlers.setHandlers(new Handler[] { contexts, handler }); |
| 57 | + server.setHandler(handlers); |
| 58 | + |
| 59 | + handler.setServeIcon(true); |
| 60 | + handler.setShowContexts(true); |
| 61 | + |
| 62 | + contexts.addHandler(new ContextHandler("/foo")); |
| 63 | + contexts.addHandler(new ContextHandler("/bar")); |
| 64 | + |
| 65 | + server.start(); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void after() throws Exception |
| 70 | + { |
| 71 | + server.stop(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testRoot() throws Exception |
| 76 | + { |
| 77 | + try (Socket socket = new Socket("localhost", connector.getLocalPort())) |
| 78 | + { |
| 79 | + String request = "" + |
| 80 | + "GET / HTTP/1.1\r\n" + |
| 81 | + "Host: localhost\r\n" + |
| 82 | + "\r\n"; |
| 83 | + OutputStream output = socket.getOutputStream(); |
| 84 | + output.write(request.getBytes(StandardCharsets.UTF_8)); |
| 85 | + output.flush(); |
| 86 | + |
| 87 | + HttpTester.Input input = HttpTester.from(socket.getInputStream()); |
| 88 | + HttpTester.Response response = HttpTester.parseResponse(input); |
| 89 | + |
| 90 | + Assert.assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); |
| 91 | + Assert.assertEquals("text/html;charset=ISO-8859-1", response.get(HttpHeader.CONTENT_TYPE)); |
| 92 | + |
| 93 | + String content = new String(response.getContentBytes(),StandardCharsets.ISO_8859_1); |
| 94 | + Assert.assertThat(content,containsString("Contexts known to this server are:")); |
| 95 | + Assert.assertThat(content,containsString("/foo")); |
| 96 | + Assert.assertThat(content,containsString("/bar")); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testSomePath() throws Exception |
| 102 | + { |
| 103 | + try (Socket socket = new Socket("localhost", connector.getLocalPort())) |
| 104 | + { |
| 105 | + String request = "" + |
| 106 | + "GET /some/path HTTP/1.1\r\n" + |
| 107 | + "Host: localhost\r\n" + |
| 108 | + "\r\n"; |
| 109 | + OutputStream output = socket.getOutputStream(); |
| 110 | + output.write(request.getBytes(StandardCharsets.UTF_8)); |
| 111 | + output.flush(); |
| 112 | + |
| 113 | + HttpTester.Input input = HttpTester.from(socket.getInputStream()); |
| 114 | + HttpTester.Response response = HttpTester.parseResponse(input); |
| 115 | + |
| 116 | + Assert.assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); |
| 117 | + Assert.assertEquals("text/html;charset=ISO-8859-1", response.get(HttpHeader.CONTENT_TYPE)); |
| 118 | + |
| 119 | + String content = new String(response.getContentBytes(),StandardCharsets.ISO_8859_1); |
| 120 | + Assert.assertThat(content,not(containsString("Contexts known to this server are:"))); |
| 121 | + Assert.assertThat(content,not(containsString("/foo"))); |
| 122 | + Assert.assertThat(content,not(containsString("/bar"))); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testFavIcon() throws Exception |
| 128 | + { |
| 129 | + try (Socket socket = new Socket("localhost", connector.getLocalPort())) |
| 130 | + { |
| 131 | + String request = "" + |
| 132 | + "GET /favicon.ico HTTP/1.1\r\n" + |
| 133 | + "Host: localhost\r\n" + |
| 134 | + "\r\n"; |
| 135 | + OutputStream output = socket.getOutputStream(); |
| 136 | + output.write(request.getBytes(StandardCharsets.UTF_8)); |
| 137 | + output.flush(); |
| 138 | + |
| 139 | + HttpTester.Input input = HttpTester.from(socket.getInputStream()); |
| 140 | + HttpTester.Response response = HttpTester.parseResponse(input); |
| 141 | + |
| 142 | + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); |
| 143 | + Assert.assertEquals("image/x-icon", response.get(HttpHeader.CONTENT_TYPE)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments