Debian Codegraph @

crt/aws-c-http/tests/test_message.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/string.h>
7 #include <aws/http/private/request_response_impl.h>
8 #include <aws/http/request_response.h>
9 #include <aws/http/status_code.h>
10 #include <aws/testing/aws_test_harness.h>
11
12 #define TEST_CASE(NAME) \
13 AWS_TEST_CASE(NAME, s_test_##NAME); \
14 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
15 #define DEFINE_HEADER(NAME, VALUE) \
16 { \
17 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
18 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
19 }
20
21 TEST_CASE(message_sanity_check) {
22 (void)ctx;
23 struct aws_http_message *request = aws_http_message_new_request(allocator);
24 ASSERT_NOT_NULL(request);
25 aws_http_message_destroy(request);
26
27 struct aws_http_message *response = aws_http_message_new_response(allocator);
28 ASSERT_NOT_NULL(response);
29 aws_http_message_destroy(response);
30
31 return AWS_OP_SUCCESS;
32 }
33
34 TEST_CASE(message_request_path) {
35 (void)ctx;
36 struct aws_http_message *request = aws_http_message_new_request(allocator);
37 ASSERT_NOT_NULL(request);
38
39 /* Assert that query fails when there's no data */
40 struct aws_byte_cursor get;
41 ASSERT_ERROR(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE, aws_http_message_get_request_path(request, &get));
42
43 /* Test simple set/get */
44 char path1[] = "/";
45 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str(path1)));
46 ASSERT_SUCCESS(aws_http_message_get_request_path(request, &get));
47 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, path1));
48
49 /* Mutilate the original string to be sure request wasn't referencing its memory */
50 path1[0] = 'z';
51 struct aws_byte_cursor path1_repro = aws_byte_cursor_from_c_str("/");
52 ASSERT_TRUE(aws_byte_cursor_eq(&path1_repro, &get));
53
54 /* Set a new path */
55 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/index.html")));
56 ASSERT_SUCCESS(aws_http_message_get_request_path(request, &get));
57 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "/index.html"));
58
59 aws_http_message_destroy(request);
60 return AWS_OP_SUCCESS;
61 }
62 TEST_CASE(message_request_method) {
63 (void)ctx;
64 struct aws_http_message *request = aws_http_message_new_request(allocator);
65 ASSERT_NOT_NULL(request);
66
67 /* Assert that query fails when there's no data */
68 struct aws_byte_cursor get;
69 ASSERT_ERROR(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE, aws_http_message_get_request_method(request, &get));
70
71 /* Test simple set/get */
72 char method1[] = "GET";
73 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str(method1)));
74 ASSERT_SUCCESS(aws_http_message_get_request_method(request, &get));
75 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, method1));
76
77 /* Mutilate the original string to be sure request wasn't referencing its memory */
78 method1[0] = 'B';
79 ASSERT_TRUE(aws_byte_cursor_eq(&aws_http_method_get, &get));
80
81 /* Set a new method */
82 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_http_method_post));
83 ASSERT_SUCCESS(aws_http_message_get_request_method(request, &get));
84 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "POST"));
85
86 aws_http_message_destroy(request);
87 return AWS_OP_SUCCESS;
88 }
89
90 TEST_CASE(message_response_status) {
91 (void)ctx;
92 struct aws_http_message *response = aws_http_message_new_response(allocator);
93 ASSERT_NOT_NULL(response);
94
95 /* Assert that query fails when there's no data */
96 int get;
97 ASSERT_ERROR(AWS_ERROR_HTTP_DATA_NOT_AVAILABLE, aws_http_message_get_response_status(response, &get));
98
99 /* Test simple set/get */
100 ASSERT_SUCCESS(aws_http_message_set_response_status(response, AWS_HTTP_STATUS_CODE_200_OK));
101 ASSERT_SUCCESS(aws_http_message_get_response_status(response, &get));
102 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_200_OK, get);
103
104 /* Set a new status */
105 ASSERT_SUCCESS(aws_http_message_set_response_status(response, AWS_HTTP_STATUS_CODE_404_NOT_FOUND));
106 ASSERT_SUCCESS(aws_http_message_get_response_status(response, &get));
107 ASSERT_INT_EQUALS(AWS_HTTP_STATUS_CODE_404_NOT_FOUND, get);
108
109 aws_http_message_destroy(response);
110 return AWS_OP_SUCCESS;
111 }
112
113 static struct aws_http_header s_make_header(const char *name, const char *value) {
114 return (struct aws_http_header){
115 .name = aws_byte_cursor_from_c_str(name),
116 .value = aws_byte_cursor_from_c_str(value),
117 };
118 }
119
120 static int s_check_headers_eq(struct aws_http_header a, struct aws_http_header b) {
121 ASSERT_TRUE(aws_byte_cursor_eq(&a.name, &b.name));
122 ASSERT_TRUE(aws_byte_cursor_eq(&a.value, &b.value));
123 return AWS_OP_SUCCESS;
124 }
125
126 static int s_check_header_eq(struct aws_http_header header, const char *name, const char *value) {
127 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&header.name, name));
128 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&header.value, value));
129 return AWS_OP_SUCCESS;
130 }
131
132 static int s_check_value_eq(struct aws_byte_cursor cursor, const char *value) {
133 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&cursor, value));
134 return AWS_OP_SUCCESS;
135 }
136
137 TEST_CASE(headers_add) {
138 (void)ctx;
139 struct aws_http_headers *headers = aws_http_headers_new(allocator);
140 ASSERT_NOT_NULL(headers);
141
142 char name_src[] = "Host";
143 char value_src[] = "example.com";
144
145 ASSERT_SUCCESS(
146 aws_http_headers_add(headers, aws_byte_cursor_from_c_str(name_src), aws_byte_cursor_from_c_str(value_src)));
147
148 ASSERT_UINT_EQUALS(1, aws_http_headers_count(headers));
149
150 /* Mutilate source strings to be sure the datastructure isn't referencing their memory */
151 name_src[0] = 0;
152 value_src[0] = 0;
153
154 /* get-by-index */
155 struct aws_http_header get;
156 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 0, &get));
157 ASSERT_SUCCESS(s_check_header_eq(get, "Host", "example.com"));
158
159 /* get-by-name (ignore case) */
160 struct aws_byte_cursor value_get;
161 ASSERT_SUCCESS(aws_http_headers_get(headers, aws_byte_cursor_from_c_str("host"), &value_get)); /* ignore case */
162 ASSERT_SUCCESS(s_check_value_eq(value_get, "example.com"));
163
164 aws_http_headers_release(headers);
165 return AWS_OP_SUCCESS;
166 }
167
168 TEST_CASE(headers_add_array) {
169 (void)ctx;
170 struct aws_http_headers *headers = aws_http_headers_new(allocator);
171 ASSERT_NOT_NULL(headers);
172
173 const struct aws_http_header src_headers[] = {
174 s_make_header("Cookie", "a=1"),
175 s_make_header("COOKIE", "b=2"),
176 };
177
178 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
179 ASSERT_UINT_EQUALS(AWS_ARRAY_SIZE(src_headers), aws_http_headers_count(headers));
180
181 for (size_t i = 0; i < AWS_ARRAY_SIZE(src_headers); ++i) {
182 struct aws_http_header get;
183 ASSERT_SUCCESS(aws_http_headers_get_index(headers, i, &get));
184 ASSERT_SUCCESS(s_check_headers_eq(src_headers[i], get));
185 }
186
187 /* check that get-by-name returns first one it sees */
188 struct aws_byte_cursor get;
189 ASSERT_SUCCESS(aws_http_headers_get(headers, aws_byte_cursor_from_c_str("COOKIE"), &get));
190 ASSERT_SUCCESS(s_check_value_eq(get, "a=1"));
191
192 aws_http_headers_release(headers);
193 return AWS_OP_SUCCESS;
194 }
195
196 TEST_CASE(headers_set) {
197 (void)ctx;
198 struct aws_http_headers *headers = aws_http_headers_new(allocator);
199 ASSERT_NOT_NULL(headers);
200
201 /* Check that set() can add a new header */
202 ASSERT_SUCCESS(
203 aws_http_headers_set(headers, aws_byte_cursor_from_c_str("Cookie"), aws_byte_cursor_from_c_str("a=1")));
204
205 struct aws_http_header get;
206 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 0, &get));
207 ASSERT_SUCCESS(s_check_header_eq(get, "Cookie", "a=1"));
208
209 /* Add more headers with same name, then check that set() replaces them ALL */
210 const struct aws_http_header src_headers[] = {
211 s_make_header("Cookie", "b=2"),
212 s_make_header("COOKIE", "c=3"),
213 };
214 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
215
216 ASSERT_SUCCESS(
217 aws_http_headers_set(headers, aws_byte_cursor_from_c_str("Cookie"), aws_byte_cursor_from_c_str("d=4")));
218
219 ASSERT_UINT_EQUALS(1, aws_http_headers_count(headers));
220
221 struct aws_byte_cursor value_get;
222 ASSERT_SUCCESS(aws_http_headers_get(headers, aws_byte_cursor_from_c_str("cookie"), &value_get));
223 ASSERT_SUCCESS(s_check_value_eq(value_get, "d=4"));
224
225 aws_http_headers_release(headers);
226 return AWS_OP_SUCCESS;
227 }
228
229 TEST_CASE(headers_erase_index) {
230 (void)ctx;
231 struct aws_http_headers *headers = aws_http_headers_new(allocator);
232 ASSERT_NOT_NULL(headers);
233
234 const struct aws_http_header src_headers[] = {
235 s_make_header("Cookie", "a=1"),
236 s_make_header("Cookie", "b=2"),
237 };
238 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
239
240 /* Ensure bad attempts to erase data are detected */
241 ASSERT_ERROR(AWS_ERROR_INVALID_INDEX, aws_http_headers_erase_index(headers, 99));
242
243 /* Erase by index */
244 ASSERT_SUCCESS(aws_http_headers_erase_index(headers, 0));
245
246 ASSERT_UINT_EQUALS(1, aws_http_headers_count(headers));
247
248 struct aws_http_header get;
249 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 0, &get));
250 ASSERT_SUCCESS(s_check_header_eq(get, "Cookie", "b=2"));
251
252 aws_http_headers_release(headers);
253 return AWS_OP_SUCCESS;
254 }
255
256 TEST_CASE(headers_erase) {
257 (void)ctx;
258 struct aws_http_headers *headers = aws_http_headers_new(allocator);
259 ASSERT_NOT_NULL(headers);
260
261 const struct aws_http_header src_headers[] = {
262 s_make_header("cookie", "a=1"),
263 s_make_header("CoOkIe", "b=2"),
264 };
265 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
266
267 /* Ensure bad attempts to erase data are detected */
268 ASSERT_ERROR(AWS_ERROR_HTTP_HEADER_NOT_FOUND, aws_http_headers_erase(headers, aws_byte_cursor_from_c_str("asdf")));
269
270 ASSERT_SUCCESS(aws_http_headers_erase(headers, aws_byte_cursor_from_c_str("COOKIE")));
271
272 ASSERT_UINT_EQUALS(0, aws_http_headers_count(headers));
273
274 aws_http_headers_release(headers);
275 return AWS_OP_SUCCESS;
276 }
277
278 TEST_CASE(headers_erase_value) {
279 (void)ctx;
280 struct aws_http_headers *headers = aws_http_headers_new(allocator);
281 ASSERT_NOT_NULL(headers);
282
283 const struct aws_http_header src_headers[] = {
284 s_make_header("Cookie", "a=1"),
285 s_make_header("CoOkIe", "b=2"),
286 s_make_header("COOKIE", "b=2"),
287 };
288 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
289
290 /* Ensure bad attempts to erase data are detected */
291 ASSERT_ERROR(
292 AWS_ERROR_HTTP_HEADER_NOT_FOUND,
293 aws_http_headers_erase_value(
294 headers, aws_byte_cursor_from_c_str("cookie"), aws_byte_cursor_from_c_str("asdf")));
295
296 /* Pluck out the first instance of b=2 */
297 ASSERT_SUCCESS(
298 aws_http_headers_erase_value(headers, aws_byte_cursor_from_c_str("cookie"), aws_byte_cursor_from_c_str("b=2")));
299
300 ASSERT_UINT_EQUALS(2, aws_http_headers_count(headers));
301
302 struct aws_http_header get;
303 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 0, &get));
304 ASSERT_SUCCESS(s_check_header_eq(get, "Cookie", "a=1"));
305
306 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 1, &get));
307 ASSERT_SUCCESS(s_check_header_eq(get, "COOKIE", "b=2"));
308
309 aws_http_headers_release(headers);
310 return AWS_OP_SUCCESS;
311 }
312
313 TEST_CASE(headers_clear) {
314 (void)ctx;
315 struct aws_http_headers *headers = aws_http_headers_new(allocator);
316 ASSERT_NOT_NULL(headers);
317
318 const struct aws_http_header src_headers[] = {
319 s_make_header("Host", "example.com"),
320 s_make_header("Cookie", "a=1"),
321 };
322 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
323
324 aws_http_headers_clear(headers);
325 ASSERT_UINT_EQUALS(0, aws_http_headers_count(headers));
326
327 aws_http_headers_release(headers);
328 return AWS_OP_SUCCESS;
329 }
330
331 TEST_CASE(headers_get_all) {
332 (void)ctx;
333
334 struct aws_http_headers *headers = aws_http_headers_new(allocator);
335
336 /* Check when no such headers exist */
337 aws_http_headers_clear(headers);
338 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com"));
339 ASSERT_NULL(aws_http_headers_get_all(headers, aws_byte_cursor_from_c_str("X-My-List")));
340 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_HEADER_NOT_FOUND, aws_last_error());
341
342 /* Check getting a single value */
343 aws_http_headers_clear(headers);
344 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com"));
345 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("X-My-List"), aws_byte_cursor_from_c_str("A"));
346 struct aws_string *value = aws_http_headers_get_all(headers, aws_byte_cursor_from_c_str("X-My-List"));
347 ASSERT_NOT_NULL(value);
348 ASSERT_TRUE(aws_string_eq_c_str(value, "A"));
349 aws_string_destroy(value);
350
351 /* Check getting a blank-string value */
352 aws_http_headers_clear(headers);
353 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com"));
354 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("X-My-List"), aws_byte_cursor_from_c_str(""));
355 value = aws_http_headers_get_all(headers, aws_byte_cursor_from_c_str("X-My-List"));
356 ASSERT_NOT_NULL(value);
357 ASSERT_TRUE(aws_string_eq_c_str(value, ""));
358 aws_string_destroy(value);
359
360 /* Check getting multiple values */
361 aws_http_headers_clear(headers);
362 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com"));
363 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("X-My-List"), aws_byte_cursor_from_c_str("A"));
364 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("X-My-List"), aws_byte_cursor_from_c_str("B"));
365 value = aws_http_headers_get_all(headers, aws_byte_cursor_from_c_str("X-My-List"));
366 ASSERT_NOT_NULL(value);
367 ASSERT_TRUE(aws_string_eq_c_str(value, "A, B"));
368 aws_string_destroy(value);
369
370 /* Check more edge cases */
371 aws_http_headers_clear(headers);
372 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com"));
373 /* some fields have single entry, some fields have multiple entries */
374 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("X-My-List"), aws_byte_cursor_from_c_str("A, B"));
375 /* preserve whitespace within middle of value. also name is different case */
376 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("x-my-list"), aws_byte_cursor_from_c_str("C,D"));
377 aws_http_headers_add(
378 headers, aws_byte_cursor_from_c_str("X-My-List-"), aws_byte_cursor_from_c_str("BAD-EXTRA-DASH"));
379 /* name is different case, and blank value*/
380 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("X-MY-LIST"), aws_byte_cursor_from_c_str(""));
381 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("x-my-list"), aws_byte_cursor_from_c_str("E"));
382 value = aws_http_headers_get_all(headers, aws_byte_cursor_from_c_str("X-My-List"));
383 ASSERT_NOT_NULL(value);
384 ASSERT_TRUE(aws_string_eq_c_str(value, "A, B, C,D, , E"));
385 aws_string_destroy(value);
386
387 /* Done */
388 aws_http_headers_release(headers);
389 return AWS_OP_SUCCESS;
390 }
391
392 TEST_CASE(h2_headers_request_pseudos_get_set) {
393 (void)ctx;
394 struct aws_http_headers *headers = aws_http_headers_new(allocator);
395 ASSERT_NOT_NULL(headers);
396 const struct aws_http_header src_headers[] = {
397 s_make_header("Host", "example.com"),
398 s_make_header("Cookie", "a=1"),
399 };
400 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
401
402 ASSERT_SUCCESS(aws_http2_headers_set_request_method(headers, aws_byte_cursor_from_c_str("GET")));
403 ASSERT_SUCCESS(aws_http2_headers_set_request_scheme(headers, aws_byte_cursor_from_c_str("https")));
404 ASSERT_SUCCESS(aws_http2_headers_set_request_authority(headers, aws_byte_cursor_from_c_str("www.amazon.com")));
405 ASSERT_SUCCESS(aws_http2_headers_set_request_path(headers, aws_byte_cursor_from_c_str("/")));
406
407 /* pseudo headers should be in the front of headers */
408 struct aws_byte_cursor get;
409 ASSERT_SUCCESS(aws_http2_headers_get_request_method(headers, &get));
410 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "GET"));
411 ASSERT_SUCCESS(aws_http2_headers_get_request_scheme(headers, &get));
412 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "https"));
413 ASSERT_SUCCESS(aws_http2_headers_get_request_authority(headers, &get));
414 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "www.amazon.com"));
415 ASSERT_SUCCESS(aws_http2_headers_get_request_path(headers, &get));
416 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "/"));
417 /* normal headers should be the end of the headers list */
418 struct aws_http_header get_header;
419 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 4, &get_header));
420 ASSERT_SUCCESS(s_check_header_eq(get_header, "Host", "example.com"));
421 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 5, &get_header));
422 ASSERT_SUCCESS(s_check_header_eq(get_header, "Cookie", "a=1"));
423
424 /* overwrite method should not change the normal headers */
425 ASSERT_SUCCESS(aws_http2_headers_set_request_method(headers, aws_byte_cursor_from_c_str("PUT")));
426 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 4, &get_header));
427 ASSERT_SUCCESS(s_check_header_eq(get_header, "Host", "example.com"));
428 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 5, &get_header));
429 ASSERT_SUCCESS(s_check_header_eq(get_header, "Cookie", "a=1"));
430 ASSERT_SUCCESS(aws_http2_headers_get_request_method(headers, &get));
431 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&get, "PUT"));
432
433 aws_http_headers_release(headers);
434 return AWS_OP_SUCCESS;
435 }
436
437 TEST_CASE(h2_headers_response_pseudos_get_set) {
438 (void)ctx;
439 struct aws_http_headers *headers = aws_http_headers_new(allocator);
440 ASSERT_NOT_NULL(headers);
441 const struct aws_http_header src_headers[] = {
442 s_make_header("Host", "example.com"),
443 s_make_header("Cookie", "a=1"),
444 };
445 ASSERT_SUCCESS(aws_http_headers_add_array(headers, src_headers, AWS_ARRAY_SIZE(src_headers)));
446
447 ASSERT_SUCCESS(aws_http2_headers_set_response_status(headers, 200));
448
449 /* pseudo headers should be in the front of headers */
450 int get;
451 ASSERT_SUCCESS(aws_http2_headers_get_response_status(headers, &get));
452 ASSERT_INT_EQUALS(get, 200);
453 /* normal headers should be the end of the headers list */
454 struct aws_http_header get_header;
455 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 1, &get_header));
456 ASSERT_SUCCESS(s_check_header_eq(get_header, "Host", "example.com"));
457 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 2, &get_header));
458 ASSERT_SUCCESS(s_check_header_eq(get_header, "Cookie", "a=1"));
459
460 /* overwrite method should not change the normal headers */
461 ASSERT_SUCCESS(aws_http2_headers_set_response_status(headers, 404));
462 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 1, &get_header));
463 ASSERT_SUCCESS(s_check_header_eq(get_header, "Host", "example.com"));
464 ASSERT_SUCCESS(aws_http_headers_get_index(headers, 2, &get_header));
465 ASSERT_SUCCESS(s_check_header_eq(get_header, "Cookie", "a=1"));
466 ASSERT_SUCCESS(aws_http2_headers_get_response_status(headers, &get));
467 ASSERT_INT_EQUALS(get, 404);
468
469 aws_http_headers_release(headers);
470 return AWS_OP_SUCCESS;
471 }
472
473 TEST_CASE(message_refcounts) {
474 (void)ctx;
475 struct aws_http_message *message = aws_http_message_new_request(allocator);
476 ASSERT_NOT_NULL(message);
477
478 struct aws_http_headers *headers = aws_http_message_get_headers(message);
479 ASSERT_NOT_NULL(headers);
480
481 /* assert message is still valid after acquire/release */
482 aws_http_message_acquire(message);
483 aws_http_message_release(message);
484 ASSERT_SUCCESS(aws_http_message_set_request_path(message, aws_byte_cursor_from_c_str("PATCH")));
485
486 /* keep headers alive after message is destroyed */
487 aws_http_headers_acquire(headers);
488 aws_http_message_release(message);
489 ASSERT_FALSE(aws_http_headers_has(headers, aws_byte_cursor_from_c_str("Host")));
490 ASSERT_SUCCESS(
491 aws_http_headers_add(headers, aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com")));
492 ASSERT_TRUE(aws_http_headers_has(headers, aws_byte_cursor_from_c_str("Host")));
493
494 aws_http_headers_release(headers);
495 return AWS_OP_SUCCESS;
496 }
497
498 TEST_CASE(message_with_existing_headers) {
499 (void)ctx;
500 struct aws_http_headers *headers = aws_http_headers_new(allocator);
501 ASSERT_NOT_NULL(headers);
502
503 struct aws_http_message *message = aws_http_message_new_request_with_headers(allocator, headers);
504 ASSERT_NOT_NULL(message);
505
506 ASSERT_PTR_EQUALS(headers, aws_http_message_get_headers(message));
507
508 /* assert message has acquired hold on headers */
509 aws_http_headers_release(headers);
510
511 /* still valid, right? */
512 struct aws_http_header new_header = {aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com")};
513 ASSERT_SUCCESS(aws_http_message_add_header(message, new_header));
514
515 /* clean up*/
516 aws_http_message_release(message);
517 return AWS_OP_SUCCESS;
518 }