-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicRequirementsAcceptanceTest.java
133 lines (115 loc) · 4.47 KB
/
BasicRequirementsAcceptanceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package de.havemann.lukas.vanillahttp.acceptancetest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import de.havemann.lukas.vanillahttp.VanillaHttpServer;
import java.io.IOException;
import java.util.stream.Collectors;
import org.assertj.core.api.SoftAssertions;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
/**
* Testing the fulfilment of the following requirements:
* <p>
* The server should serve static files and directories from a user-specified root directory. - It
* should be possible to discover subdirectories as well. - The server must at least handle HTTP GET
* and HEAD requests.
*/
@SpringBootTest(classes = VanillaHttpServer.class)
@TestPropertySource(properties = {
"vanilla.server.port=9999",
"vanilla.server.filesystem.basedir=./src/test/resources/sampledirectory"
})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class BasicRequirementsAcceptanceTest {
private static final String BASE_URL = "http://localhost:9999/";
/**
* Server must handle get request and its is possible to discover subdirectories
*/
@Test
void httpGetRequestToBaseDirTest() throws IOException {
final Document document = Jsoup.connect(BASE_URL).method(Connection.Method.GET).execute()
.parse();
assertThat(document.select("li").stream().map(Element::text).collect(Collectors.toList()))
.hasSize(4)
.contains("dirwithhtml/")
.contains("subdirectory/");
}
/**
* Server must prevent access outside of base dir.
*/
@Test
void httpGetOutsideFromBaseDirIsPreventedTest() throws IOException {
try {
Jsoup.connect(BASE_URL + "../../").method(Connection.Method.GET).execute();
fail("jsoup should have thrown exception");
} catch (org.jsoup.HttpStatusException ex) {
assertThat(ex.getStatusCode()).isEqualTo(403);
}
}
/**
* Server must handle head request
*/
@Test
void httpHeadRequestToBaseDirTest() throws IOException {
final Connection.Response response = Jsoup.connect(BASE_URL).method(Connection.Method.HEAD)
.execute();
final SoftAssertions softly = new SoftAssertions();
softly.assertThat(response.statusCode()).isEqualTo(200);
softly.assertThat(response.body()).isBlank();
softly.assertAll();
}
/**
* Server should be able to handle sub directories and files with spaces
*/
@Test
void httpGetRequestToSubdirectoryWithSpacesTest() throws IOException {
final Connection.Response response = Jsoup
.connect(BASE_URL + "sub%20with%20space/and%20file.txt")
.method(Connection.Method.GET)
.execute();
final SoftAssertions softly = new SoftAssertions();
softly.assertThat(response.header("ContentType")).isEqualTo("text/plain");
softly.assertThat(response.body()).isEqualTo("file with spaces");
softly.assertAll();
}
@Test
void httpGetRequestToJpgTest() throws IOException {
final Connection.Response response = Jsoup.connect(BASE_URL + "subdirectory/hamburg.jpg")
.method(Connection.Method.GET)
.execute();
final SoftAssertions softly = new SoftAssertions();
softly.assertThat(response.header("ContentType")).isEqualTo("image/jpeg");
softly.assertThat(response.body()).contains("https://flickr.com/");
softly.assertAll();
}
/**
* Server must handle get request and its is possible to discover subdirectories
*/
@Test
void httpGetToDirectoryWithIndexFileIsDisplayed() throws IOException {
final Connection.Response response = Jsoup.connect(BASE_URL + "dirwithhtml")
.method(Connection.Method.GET).execute();
final SoftAssertions softly = new SoftAssertions();
softly.assertThat(response.parse().select("h1").text()).isEqualTo("Welcome to the sample site");
softly.assertThat(response.header("ContentType")).isEqualTo("text/html");
softly.assertAll();
}
/**
* Server must handle request to unknown files
*/
@Test
void httpGetRequestToUnknownFile() throws IOException {
try {
Jsoup.connect(BASE_URL + "unknown").method(Connection.Method.GET).execute();
fail("jsoup should have thrown exception");
} catch (org.jsoup.HttpStatusException ex) {
assertThat(ex.getStatusCode()).isEqualTo(404);
}
}
}