forked from codesquad-members-2021/java-was
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestHeaderTest.java
More file actions
241 lines (222 loc) · 14.3 KB
/
RequestHeaderTest.java
File metadata and controls
241 lines (222 loc) · 14.3 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package webserver.http.header;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import webserver.Const;
import webserver.http.attribute.Attributes;
import webserver.http.startline.RequestLine;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
class RequestHeaderTest {
@ParameterizedTest
@MethodSource("getAttributes")
void getAttributes(String headerText, Attributes expectedAttributes) {
assertThat(RequestHeader.from(headerText).getAttributes())
.isEqualTo(expectedAttributes);
}
static Stream<Arguments> getAttributes() {
return Stream.of(
Arguments.of(
"GET / HTTP/1.1" + Const.CRLF +
"Host: localhost:8080" + Const.CRLF +
"Connection: keep-alive" + Const.CRLF +
"Cache-Control: max-age=0" + Const.CRLF +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + Const.CRLF +
"sec-ch-ua-mobile: ?0" + Const.CRLF +
"Upgrade-Insecure-Requests: 1" + Const.CRLF +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + Const.CRLF +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + Const.CRLF +
"Sec-Fetch-Site: none" + Const.CRLF +
"Sec-Fetch-Mode: navigate" + Const.CRLF +
"Sec-Fetch-User: ?1" + Const.CRLF +
"Sec-Fetch-Dest: document" + Const.CRLF +
"Accept-Encoding: gzip, deflate, br" + Const.CRLF +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + Const.CRLF +
"Cookie: Idea-1c77831=5ced54c8-cabd-4355-ae5a-97b17f9d7443" + Const.CRLF +
Const.CRLF,
Attributes.from(new LinkedHashMap<String, String>() {{
put("Host", "localhost:8080");
put("Connection", "keep-alive");
put("Cache-Control", "max-age=0");
put("sec-ch-ua", "\"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"");
put("sec-ch-ua-mobile", "?0");
put("Upgrade-Insecure-Requests", "1");
put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36");
put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
put("Sec-Fetch-Site", "none");
put("Sec-Fetch-Mode", "navigate");
put("Sec-Fetch-User", "?1");
put("Sec-Fetch-Dest", "document");
put("Accept-Encoding", "gzip, deflate, br");
put("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7");
put("Cookie", "Idea-1c77831=5ced54c8-cabd-4355-ae5a-97b17f9d7443");
}}
)
)
);
}
@ParameterizedTest
@MethodSource("getMethod")
void getMethod(String headerText, String expectedMethod) {
assertThat(RequestHeader.from(headerText).getMethod())
.isEqualTo(expectedMethod);
}
static Stream<Arguments> getMethod() {
return Stream.of(
Arguments.of(
"GET / HTTP/1.1" + Const.CRLF +
"Host: localhost:8080" + Const.CRLF +
"Connection: keep-alive" + Const.CRLF +
"Cache-Control: max-age=0" + Const.CRLF +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + Const.CRLF +
"sec-ch-ua-mobile: ?0" + Const.CRLF +
"Upgrade-Insecure-Requests: 1" + Const.CRLF +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + Const.CRLF +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + Const.CRLF +
"Sec-Fetch-Site: none" + Const.CRLF +
"Sec-Fetch-Mode: navigate" + Const.CRLF +
"Sec-Fetch-User: ?1" + Const.CRLF +
"Sec-Fetch-Dest: document" + Const.CRLF +
"Accept-Encoding: gzip, deflate, br" + Const.CRLF +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + Const.CRLF +
"Cookie: Idea-1c77831=5ced54c8-cabd-4355-ae5a-97b17f9d7443" + Const.CRLF +
Const.CRLF,
"GET"
), Arguments.of(
"POST /user/create HTTP/1.1" + Const.CRLF +
"Host: localhost:8080" + Const.CRLF +
"Connection: keep-alive" + Const.CRLF +
"Content-Length: 59" + Const.CRLF +
"Content-Type: application/x-www-form-urlencoded" + Const.CRLF +
"Accept: */*" + Const.CRLF +
"" + Const.CRLF +
"userId=javajigi&password=password&name=%EB%B0%95%EC%9E%AC%EC%84%B1&email=javajigi%40slipp.net",
"POST"
)
);
}
@ParameterizedTest
@MethodSource("getRequestLineAttributes")
void getRequestLineAttributes(String headerText, RequestLine expectedRequestLine) {
assertThat(RequestHeader.from(headerText))
.extracting("requestLine")
.usingRecursiveFieldByFieldElementComparator()
.contains(expectedRequestLine);
}
static Stream<Arguments> getRequestLineAttributes() {
return Stream.of(
Arguments.of("GET / HTTP/1.1" + Const.CRLF +
"Host: localhost:8080" + Const.CRLF +
"Connection: keep-alive" + Const.CRLF +
"Cache-Control: max-age=0" + Const.CRLF +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + Const.CRLF +
"sec-ch-ua-mobile: ?0" + Const.CRLF +
"Upgrade-Insecure-Requests: 1" + Const.CRLF +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + Const.CRLF +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + Const.CRLF +
"Sec-Fetch-Site: none" + Const.CRLF +
"Sec-Fetch-Mode: navigate" + Const.CRLF +
"Sec-Fetch-User: ?1" + Const.CRLF +
"Sec-Fetch-Dest: document" + Const.CRLF +
"Accept-Encoding: gzip, deflate, br" + Const.CRLF +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + Const.CRLF +
"Cookie: Idea-1c77831=5ced54c8-cabd-4355-ae5a-97b17f9d7443" + Const.CRLF +
Const.CRLF,
new RequestLine(
Attributes.from(new HashMap<String, String>() {{
put("method", "GET");
put("path", "/");
put("protocolVersion", "HTTP/1.1");
}})
)
)
);
}
@ParameterizedTest
@MethodSource("getBytes")
void getBytes(String headerText, byte[] expectedHeaderByte) {
byte[] headerByte = RequestHeader.from(headerText).getBytes();
assertAll(
() -> assertThat(headerByte).isEqualTo(expectedHeaderByte),
() -> assertThat(new String(headerByte)).isEqualTo(new String(expectedHeaderByte))
);
}
static Stream<Arguments> getBytes() {
return Stream.of(
Arguments.of(
"GET / HTTP/1.1" + Const.CRLF +
"Host: localhost:8080" + Const.CRLF +
"Connection: keep-alive" + Const.CRLF +
"Cache-Control: max-age=0" + Const.CRLF +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + Const.CRLF +
"sec-ch-ua-mobile: ?0" + Const.CRLF +
"Upgrade-Insecure-Requests: 1" + Const.CRLF +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + Const.CRLF +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + Const.CRLF +
"Sec-Fetch-Site: none" + Const.CRLF +
"Sec-Fetch-Mode: navigate" + Const.CRLF +
"Sec-Fetch-User: ?1" + Const.CRLF +
"Sec-Fetch-Dest: document" + Const.CRLF +
"Accept-Encoding: gzip, deflate, br" + Const.CRLF +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + Const.CRLF +
"Cookie: Idea-1c77831=5ced54c8-cabd-4355-ae5a-97b17f9d7443" + Const.CRLF +
Const.CRLF,
("GET / HTTP/1.1" + Const.CRLF +
"Host: localhost:8080" + Const.CRLF +
"Connection: keep-alive" + Const.CRLF +
"Cache-Control: max-age=0" + Const.CRLF +
"sec-ch-ua: \"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"" + Const.CRLF +
"sec-ch-ua-mobile: ?0" + Const.CRLF +
"Upgrade-Insecure-Requests: 1" + Const.CRLF +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" + Const.CRLF +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + Const.CRLF +
"Sec-Fetch-Site: none" + Const.CRLF +
"Sec-Fetch-Mode: navigate" + Const.CRLF +
"Sec-Fetch-User: ?1" + Const.CRLF +
"Sec-Fetch-Dest: document" + Const.CRLF +
"Accept-Encoding: gzip, deflate, br" + Const.CRLF +
"Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" + Const.CRLF +
"Cookie: Idea-1c77831=5ced54c8-cabd-4355-ae5a-97b17f9d7443" + Const.CRLF +
Const.CRLF).getBytes(StandardCharsets.UTF_8)
)
);
}
@ParameterizedTest
@MethodSource("getPath")
void getPath(String desc, RequestLine statusLine, String expectedPath) {
RequestHeader requestHeader = new RequestHeader(statusLine, Attributes.from(new HashMap<>()));
String actualPath = requestHeader.getPath();
assertThat(actualPath).as("리퀘스트 헤더에서 path 가져오기 : %s", desc)
.isEqualTo(expectedPath);
}
static Stream<Arguments> getPath() {
return Stream.of(
Arguments.of(
"쿼리스트링이 없는 GET 메세지",
new RequestLine(
new Attributes() {{
add("path", "/user/create");
add("method", "GET");
add("protocolVersion", "HTTP/1.1");
}}
),
"/user/create"
),
Arguments.of(
"쿼리스트링이 포함된 GET 메세지 path를 출력할때도 쿼리스트링이 포함되지 않아야 함",
new RequestLine(
new Attributes() {{
add("path", "/user/create?userId=javajigi&password=password&name=%EB%B0%95%EC%9E%AC%EC%84%B1&email=javajigi%40slipp.net");
add("method", "GET");
add("protocolVersion", "HTTP/1.1");
}}
),
"/user/create"
)
);
}
}