|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.grid.web; |
| 19 | + |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.rules.TemporaryFolder; |
| 24 | +import org.openqa.selenium.remote.http.Contents; |
| 25 | +import org.openqa.selenium.remote.http.HttpHandler; |
| 26 | +import org.openqa.selenium.remote.http.HttpRequest; |
| 27 | +import org.openqa.selenium.remote.http.HttpResponse; |
| 28 | +import org.openqa.selenium.remote.http.Route; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.file.Files; |
| 33 | +import java.nio.file.Path; |
| 34 | + |
| 35 | +import static java.net.HttpURLConnection.HTTP_MOVED_TEMP; |
| 36 | +import static java.net.HttpURLConnection.HTTP_OK; |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.openqa.selenium.remote.http.HttpMethod.GET; |
| 40 | + |
| 41 | +public class ResourceHandlerTest { |
| 42 | + |
| 43 | + @Rule |
| 44 | + public TemporaryFolder temp = new TemporaryFolder(); |
| 45 | + private Path base; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void getPath() throws IOException { |
| 49 | + File folder = temp.newFolder(); |
| 50 | + this.base = folder.toPath(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldLoadContent() throws IOException { |
| 55 | + Files.write(base.resolve("content.txt"), "I like cheese".getBytes(UTF_8)); |
| 56 | + |
| 57 | + HttpHandler handler = new ResourceHandler(new PathResource(base)); |
| 58 | + HttpResponse res = handler.execute(new HttpRequest(GET, "/content.txt")); |
| 59 | + |
| 60 | + assertThat(Contents.string(res)).isEqualTo("I like cheese"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldRedirectIfDirectoryButPathDoesNotEndInASlash() throws IOException { |
| 65 | + Path dir = base.resolve("cheese"); |
| 66 | + |
| 67 | + Files.createDirectories(dir); |
| 68 | + |
| 69 | + HttpHandler handler = new ResourceHandler(new PathResource(base)); |
| 70 | + HttpResponse res = handler.execute(new HttpRequest(GET, "/cheese")); |
| 71 | + |
| 72 | + assertThat(res.getStatus()).isEqualTo(HTTP_MOVED_TEMP); |
| 73 | + assertThat(res.getHeader("Location")).endsWith("/cheese/"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void shouldLoadAnIndexPage() throws IOException { |
| 78 | + Path subdir = base.resolve("subdir"); |
| 79 | + Files.createDirectories(subdir); |
| 80 | + |
| 81 | + Files.write(subdir.resolve("1.txt"), new byte[0]); |
| 82 | + Files.write(subdir.resolve("2.txt"), new byte[0]); |
| 83 | + |
| 84 | + HttpHandler handler = new ResourceHandler(new PathResource(base)); |
| 85 | + HttpResponse res = handler.execute(new HttpRequest(GET, "/subdir/")); |
| 86 | + |
| 87 | + String text = Contents.string(res); |
| 88 | + assertThat(text).contains("1.txt"); |
| 89 | + assertThat(text).contains("2.txt"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void canBeNestedWithinARoute() throws IOException { |
| 94 | + Path contents = base.resolve("cheese").resolve("cake.txt"); |
| 95 | + |
| 96 | + Files.createDirectories(contents.getParent()); |
| 97 | + Files.write(contents, "delicious".getBytes(UTF_8)); |
| 98 | + |
| 99 | + HttpHandler handler = Route.prefix("/peas").to(Route.combine(new ResourceHandler(new PathResource(base)))); |
| 100 | + |
| 101 | + // Check redirect works as expected |
| 102 | + HttpResponse res = handler.execute(new HttpRequest(GET, "/peas/cheese")); |
| 103 | + assertThat(res.getStatus()).isEqualTo(HTTP_MOVED_TEMP); |
| 104 | + assertThat(res.getHeader("Location")).isEqualTo("/peas/cheese/"); |
| 105 | + |
| 106 | + // And now that content can be read |
| 107 | + res = handler.execute(new HttpRequest(GET, "/peas/cheese/cake.txt")); |
| 108 | + assertThat(res.getStatus()).isEqualTo(HTTP_OK); |
| 109 | + assertThat(Contents.string(res)).isEqualTo("delicious"); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments