-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_response.c
253 lines (191 loc) · 7.19 KB
/
test_response.c
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
242
243
244
245
246
247
248
249
250
251
252
253
#include "../vendor/unity/unity.h"
#include "../src/response.h"
response_t res;
char test_raw_response[206];
const int response_length = 180;
const char raw_response[] =
"HTTP/1.1 200 OK\r\n"
"Date: Tue, 13 Nov 2018 05:01:00 GMT\r\n"
"Server: Apache\r\n"
"Content-Length: 39\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<html><body><h1>Test</h1></body></html>";
const int chunked_response_length = 205;
const char chunked_raw_response[] =
"HTTP/1.1 200 OK\r\n"
"Date: Tue, 13 Nov 2018 05:01:00 GMT\r\n"
"Server: Apache\r\n"
"Connection: Keep-Alive\r\n"
"Transfer-Encoding: chunked\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"14\r\n"
"<html><body><h1>Test\r\n"
"13\r\n"
"</h1></body></html>\r\n"
"0\r\n"
"\r\n";
void setUp()
{
memset(test_raw_response, 0, sizeof(test_raw_response));
response_init(&res);
}
void tearDown()
{
response_destroy(&res);
}
static void verify_response()
{
char *date, *server, *clen, *conn, *ctype;
TEST_ASSERT_TRUE_MESSAGE(res.header.complete, "Header not complete");
TEST_ASSERT_EQUAL_STRING("HTTP/1.1 200 OK", res.header.status_line);
hashmap_get(&res.header.fields, "Date", &date);
TEST_ASSERT_EQUAL_STRING("Tue, 13 Nov 2018 05:01:00 GMT", date);
free(date);
hashmap_get(&res.header.fields, "Server", &server);
TEST_ASSERT_EQUAL_STRING("Apache", server);
free(server);
hashmap_get(&res.header.fields, "Content-Length", &clen);
TEST_ASSERT_EQUAL_INT(39, atoi(clen));
free(clen);
hashmap_get(&res.header.fields, "Connection", &conn);
TEST_ASSERT_EQUAL_STRING("Keep-Alive", conn);
free(conn);
hashmap_get(&res.header.fields, "Content-Type", &ctype);
TEST_ASSERT_EQUAL_STRING("text/html", ctype);
free(ctype);
TEST_ASSERT_TRUE_MESSAGE(res.complete, "Response not complete");
TEST_ASSERT_EQUAL_STRING(raw_response, res.raw);
TEST_ASSERT_EQUAL_STRING("<html><body><h1>Test</h1></body></html>",
res.content);
}
static void verify_chunked_response()
{
char *date, *server, *tenc, *conn, *ctype;
TEST_ASSERT_TRUE_MESSAGE(res.header.complete, "Header not complete");
TEST_ASSERT_EQUAL_STRING("HTTP/1.1 200 OK", res.header.status_line);
hashmap_get(&res.header.fields, "Date", &date);
TEST_ASSERT_EQUAL_STRING("Tue, 13 Nov 2018 05:01:00 GMT", date);
free(date);
hashmap_get(&res.header.fields, "Server", &server);
TEST_ASSERT_EQUAL_STRING("Apache", server);
free(server);
hashmap_get(&res.header.fields, "Transfer-Encoding", &tenc);
TEST_ASSERT_EQUAL_STRING("chunked", tenc);
free(tenc);
hashmap_get(&res.header.fields, "Connection", &conn);
TEST_ASSERT_EQUAL_STRING("Keep-Alive", conn);
free(conn);
hashmap_get(&res.header.fields, "Content-Type", &ctype);
TEST_ASSERT_EQUAL_STRING("text/html", ctype);
free(ctype);
TEST_ASSERT_TRUE_MESSAGE(res.complete, "Response not complete");
TEST_ASSERT_EQUAL_STRING(chunked_raw_response, res.raw);
TEST_ASSERT_EQUAL_STRING("14\r\n<html><body><h1>Test\r\n13\r\n</h1></body>"
"</html>\r\n0\r\n\r\n", res.content);
}
/* Test the response_ok helper function. */
void test_response_ok()
{
strcpy(test_raw_response, raw_response);
response_deserialize(&res, test_raw_response, response_length);
TEST_ASSERT_TRUE(response_ok(&res));
}
/* Test the response_chunked helper function. */
void test_response_chunked()
{
strcpy(test_raw_response, chunked_raw_response);
response_deserialize(&res, test_raw_response, response_length);
TEST_ASSERT_TRUE(response_chunked(&res));
}
/* Test that all information is parsed when full message is passed at once. */
void test_response_deserialize_whole()
{
int nunparsed;
strcpy(test_raw_response, raw_response);
nunparsed = response_deserialize(&res, test_raw_response, response_length);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
verify_response();
}
/* Test that all information is parsed when full message is passed at once. */
void test_response_deserialize_whole_chunked()
{
int nunparsed;
strcpy(test_raw_response, chunked_raw_response);
nunparsed = response_deserialize(&res, test_raw_response,
chunked_response_length);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
verify_chunked_response();
}
/* Test that parse of response split at partial header line succeeds. */
void test_split_response_partial_header_line()
{
int nunparsed;
/* Copy up to "...Server: Apache\r\nContent-Le" */
strncpy(test_raw_response, raw_response, 80);
nunparsed = response_deserialize(&res, test_raw_response, 80);
TEST_ASSERT_EQUAL_INT(10, nunparsed);
TEST_ASSERT_EQUAL_STRING("Content-Le", test_raw_response);
/* Place the rest of the response in the buffer */
strcat(test_raw_response, &raw_response[80]);
nunparsed = response_deserialize(&res, test_raw_response, 100 + nunparsed);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
verify_response();
}
/* Test that parse of response split at full header line succeeds. */
void test_split_response_full_header_line()
{
int nunparsed;
/* Copy up to "...Content-Length: 39\r\n" */
strncpy(test_raw_response, raw_response, 90);
nunparsed = response_deserialize(&res, test_raw_response, 90);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
/* Place the rest of the response in the buffer */
strcat(test_raw_response, &raw_response[90]);
nunparsed = response_deserialize(&res, test_raw_response, 90);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
verify_response();
}
/* Test that parse of response split at end of complete header succeeds. */
void test_split_response_complete_header()
{
int nunparsed;
/* Copy up to "...Content-Type: text/html\r\n\r\n" */
strncpy(test_raw_response, raw_response, 141);
nunparsed = response_deserialize(&res, test_raw_response, 141);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
/* Place the rest of the response in the buffer */
strcat(test_raw_response, &raw_response[141]);
nunparsed = response_deserialize(&res, test_raw_response, 39);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
verify_response();
}
/* Test that parse of response split at partial content succeeds. */
void test_split_response_partial_content()
{
int nunparsed;
/* Copy up to "...Content-Type: text/html\r\n\r\n" */
strncpy(test_raw_response, raw_response, 157);
nunparsed = response_deserialize(&res, test_raw_response, 157);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
/* Place the rest of the response in the buffer */
strcat(test_raw_response, &raw_response[157]);
nunparsed = response_deserialize(&res, test_raw_response, 23);
TEST_ASSERT_EQUAL_INT(0, nunparsed);
verify_response();
}
int main()
{
UNITY_BEGIN();
RUN_TEST(test_response_ok);
RUN_TEST(test_response_chunked);
RUN_TEST(test_response_deserialize_whole);
RUN_TEST(test_response_deserialize_whole_chunked);
RUN_TEST(test_split_response_partial_header_line);
RUN_TEST(test_split_response_full_header_line);
RUN_TEST(test_split_response_complete_header);
RUN_TEST(test_split_response_partial_content);
return UNITY_END();
}