-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension2AcceptanceTest.java
More file actions
153 lines (131 loc) · 4.85 KB
/
Extension2AcceptanceTest.java
File metadata and controls
153 lines (131 loc) · 4.85 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package de.havemann.lukas.vanillahttp.acceptancetest;
import static org.assertj.core.api.Assertions.assertThat;
import de.havemann.lukas.vanillahttp.VanillaHttpServer;
import de.havemann.lukas.vanillahttp.protocol.specification.HttpStatusCode;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
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>
* Add proper HTTP/1.1 keep-alive behavior to your implementation based on the http-client's
* capabilities exposed through its request headers.
*/
@SpringBootTest(classes = VanillaHttpServer.class)
@TestPropertySource(properties = {
"vanilla.server.port=9997",
"vanilla.server.http.keepAliveTimeout: 500ms",
"vanilla.server.filesystem.basedir=./src/test/resources/sampledirectory"
})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Extension2AcceptanceTest {
private SimpleHttpTestClient client;
@BeforeEach
void beforeEach() {
client = new SimpleHttpTestClient(9997);
}
@AfterEach
void afterEach() throws IOException {
if (client != null) {
client.close();
}
}
@Test
void connectionStaysOpen() throws IOException {
assertThat(client.sendHeadRequest().readResponse())
.contains(HttpStatusCode.OK.getRepresentation());
assertThat(client.sendHeadRequest().readResponse())
.contains(HttpStatusCode.OK.getRepresentation());
assertThat(client.sendHeadRequest().readResponse())
.contains(HttpStatusCode.OK.getRepresentation());
}
@Test
void connectionClosesAfterClientTimeout() throws IOException, InterruptedException {
assertThat(client.sendHeadRequest().readResponse())
.contains(HttpStatusCode.OK.getRepresentation());
TimeUnit.SECONDS.sleep(1);
assertThat(client.readResponse()).contains(HttpStatusCode.REQUEST_TIMEOUT.getRepresentation());
}
@Test
void handlesInvalidHttpGraceful() throws IOException {
assertThat(client.send("HEAD / GarbageHTTP/1.1").readResponse())
.contains(HttpStatusCode.BAD_REQUEST.getRepresentation());
}
@Test
void handlesHttp1Correctly() throws IOException {
assertThat(client.send("HEAD / HTTP/1.0").readResponse())
.contains(HttpStatusCode.OK.getRepresentation());
try {
assertThat(client.send("HEAD / HTTP/1.0").readResponse()).isEqualTo("");
} catch (SocketException ignored) {
}
}
@Test
void respectsClientConnectionCloseHeader() throws IOException {
final String response = client.header("Connection", "close")
.send("HEAD / HTTP/1.1")
.readResponse();
assertThat(response).contains(HttpStatusCode.OK.getRepresentation());
try {
assertThat(client.send("HEAD / HTTP/1.0").readResponse()).isEqualTo("");
} catch (SocketException ignored) {
}
}
static class SimpleHttpTestClient {
private final int port;
private Socket clientSocket;
private DataOutputStream dataOutputStream;
private BufferedReader reader;
private String headerValue = "";
public SimpleHttpTestClient(int port) {
this.port = port;
}
private void initSocket() throws IOException {
if (clientSocket != null) {
return;
}
this.clientSocket = new Socket("127.0.0.1", port);
dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public SimpleHttpTestClient sendHeadRequest() throws IOException {
return send("HEAD / HTTP/1.1");
}
public SimpleHttpTestClient send(String string) throws IOException {
initSocket();
dataOutputStream.write((string + "\r\n").getBytes(StandardCharsets.UTF_8));
if (!headerValue.isEmpty()) {
dataOutputStream.write(headerValue.getBytes(StandardCharsets.UTF_8));
}
dataOutputStream.write("\r\n".getBytes(StandardCharsets.UTF_8));
dataOutputStream.flush();
return this;
}
public String readResponse() throws IOException {
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null && !line.isBlank()) {
builder.append(line).append("\r\n");
}
return builder.toString();
}
public void close() throws IOException {
clientSocket.close();
}
public SimpleHttpTestClient header(String key, String value) {
this.headerValue += (key + ": " + value + "\r\n");
return this;
}
}
}