-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension1AcceptanceTest.java
More file actions
152 lines (118 loc) · 6.2 KB
/
Extension1AcceptanceTest.java
File metadata and controls
152 lines (118 loc) · 6.2 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
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.HttpHeaderField;
import de.havemann.lukas.vanillahttp.protocol.specification.HttpStatusCode;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
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 handling of HTTP ETag, If-Match, If-None-Match, If-Modified-Since headers.
*/
@SpringBootTest(classes = VanillaHttpServer.class)
@TestPropertySource(properties = {
"vanilla.server.port=9998",
"vanilla.server.filesystem.basedir=./src/test/resources/sampledirectory"
})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Extension1AcceptanceTest {
private static final String BASE_URL = "http://localhost:9998/";
private static final ZonedDateTime LAST_MODIFIED_OF_TEST_FILE =
ZonedDateTime
.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse("Thu, 15 Apr 2021 18:26:56 GMT"));
@Test
void ifLastModifiedIsRespectedTest() throws IOException {
final Connection connection = Jsoup.connect(BASE_URL + "fileonfirstlevel.txt")
.method(Connection.Method.GET);
final Connection.Response firstResponse = connection.execute();
final String firstResponseLastModified = firstResponse
.header(HttpHeaderField.LAST_MODIFIED.getRepresentation());
assertThat(firstResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
final Connection.Response secondResponse = connection
.header(HttpHeaderField.IF_MODIFIED_SINCE.getRepresentation(), firstResponseLastModified)
.execute();
assertThat(secondResponse.statusCode()).isEqualTo(HttpStatusCode.NOT_MODIFIED.getCode());
}
@Test
void ifLastModifiedIsRespectedOnHeadRequestTooTest() throws IOException {
final Connection connection = Jsoup.connect(BASE_URL + "fileonfirstlevel.txt")
.method(Connection.Method.HEAD);
final Connection.Response firstResponse = connection.execute();
final String firstResponseLastModified = firstResponse
.header(HttpHeaderField.LAST_MODIFIED.getRepresentation());
assertThat(firstResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
final Connection.Response secondResponse = connection
.header(HttpHeaderField.IF_MODIFIED_SINCE.getRepresentation(), firstResponseLastModified)
.execute();
assertThat(secondResponse.statusCode()).isEqualTo(HttpStatusCode.NOT_MODIFIED.getCode());
}
@Test
void oldLastModifiedTest() throws IOException {
final Connection connection = Jsoup.connect(BASE_URL + "fileonfirstlevel.txt")
.method(Connection.Method.GET);
final Connection.Response firstResponse = connection.execute();
assertThat(firstResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
final Connection.Response secondResponse = connection
.header(HttpHeaderField.IF_MODIFIED_SINCE.getRepresentation(),
DateTimeFormatter.RFC_1123_DATE_TIME.format(LAST_MODIFIED_OF_TEST_FILE.minusDays(10)))
.execute();
assertThat(secondResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
}
@Test
void ifMatchIsRespectedTest() throws IOException {
final Connection connection = Jsoup.connect(BASE_URL + "fileonfirstlevel.txt")
.method(Connection.Method.GET);
final Connection.Response firstResponse = connection.execute();
final String firstResponseEtag = firstResponse
.header(HttpHeaderField.E_TAG.getRepresentation());
assertThat(firstResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
final Connection.Response secondResponse = connection
.header(HttpHeaderField.IF_MATCH.getRepresentation(), firstResponseEtag)
.execute();
assertThat(secondResponse.statusCode()).isEqualTo(HttpStatusCode.NOT_MODIFIED.getCode());
final Connection.Response thirdResponse = connection
.header(HttpHeaderField.IF_MATCH.getRepresentation(), "wrongETag")
.execute();
assertThat(thirdResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
}
@Test
void eTagShouldHavePrecedenceBeforeLastModifiedTest() throws IOException {
final Connection connection = Jsoup.connect(BASE_URL + "fileonfirstlevel.txt")
.method(Connection.Method.GET);
final Connection.Response firstResponse = connection.execute();
final String firstResponseEtag = firstResponse
.header(HttpHeaderField.E_TAG.getRepresentation());
assertThat(firstResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
final Connection.Response secondResponse = connection
.header(HttpHeaderField.IF_MATCH.getRepresentation(), firstResponseEtag)
.header(HttpHeaderField.LAST_MODIFIED.getRepresentation(),
DateTimeFormatter.RFC_1123_DATE_TIME.format(LAST_MODIFIED_OF_TEST_FILE.plusDays(10)))
.execute();
assertThat(secondResponse.statusCode()).isEqualTo(HttpStatusCode.NOT_MODIFIED.getCode());
}
@Test
void ifNoneMatchIsRespectedTest() throws IOException {
final Connection connection = Jsoup.connect(BASE_URL + "fileonfirstlevel.txt")
.method(Connection.Method.GET);
final Connection.Response firstResponse = connection.execute();
final String firstResponseEtag = firstResponse
.header(HttpHeaderField.E_TAG.getRepresentation());
assertThat(firstResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
final Connection.Response secondResponse = connection
.header(HttpHeaderField.IF_NONE_MATCH.getRepresentation(), firstResponseEtag)
.execute();
assertThat(secondResponse.statusCode()).isEqualTo(HttpStatusCode.NOT_MODIFIED.getCode());
final Connection.Response thirdResponse = connection
.header(HttpHeaderField.IF_NONE_MATCH.getRepresentation(), "wrongETag")
.execute();
assertThat(thirdResponse.statusCode()).isEqualTo(HttpStatusCode.OK.getCode());
}
}