| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "stream_test_helper.h" |
| 6 |
|
| 7 |
#include <aws/http/connection.h> |
| 8 |
#include <aws/http/private/request_response_impl.h> |
| 9 |
#include <aws/http/request_response.h> |
| 10 |
#include <aws/http/status_code.h> |
| 11 |
#include <aws/testing/aws_test_harness.h> |
| 12 |
|
| 13 |
#define UNKNOWN_HEADER_BLOCK ((enum aws_http_header_block) - 1) |
| 14 |
|
| 15 |
static int s_on_headers( |
| 16 |
struct aws_http_stream *stream, |
| 17 |
enum aws_http_header_block header_block, |
| 18 |
const struct aws_http_header *header_array, |
| 19 |
size_t num_headers, |
| 20 |
void *user_data) { |
| 21 |
|
| 22 |
(void)stream; |
| 23 |
struct client_stream_tester *tester = user_data; |
| 24 |
ASSERT_FALSE(tester->complete); |
| 25 |
|
| 26 |
if (tester->current_header_block == UNKNOWN_HEADER_BLOCK) { |
| 27 |
tester->current_header_block = header_block; |
| 28 |
} else { |
| 29 |
ASSERT_INT_EQUALS(tester->current_header_block, header_block); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
switch (header_block) { |
| 37 |
case AWS_HTTP_HEADER_BLOCK_INFORMATIONAL: |
| 38 |
ASSERT_SUCCESS(aws_http_headers_add_array(tester->current_info_headers, header_array, num_headers)); |
| 39 |
break; |
| 40 |
|
| 41 |
case AWS_HTTP_HEADER_BLOCK_MAIN: |
| 42 |
ASSERT_SUCCESS(aws_http_headers_add_array(tester->response_headers, header_array, num_headers)); |
| 43 |
break; |
| 44 |
|
| 45 |
case AWS_HTTP_HEADER_BLOCK_TRAILING: |
| 46 |
ASSERT_SUCCESS(aws_http_headers_add_array(tester->response_trailer, header_array, num_headers)); |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
return AWS_OP_SUCCESS; |
| 51 |
} |
| 52 |
|
| 53 |
static int s_on_header_block_done( |
| 54 |
struct aws_http_stream *stream, |
| 55 |
enum aws_http_header_block header_block, |
| 56 |
void *user_data) { |
| 57 |
|
| 58 |
struct client_stream_tester *tester = user_data; |
| 59 |
ASSERT_FALSE(tester->complete); |
| 60 |
|
| 61 |
if (tester->current_header_block != UNKNOWN_HEADER_BLOCK) { |
| 62 |
ASSERT_INT_EQUALS(tester->current_header_block, header_block); |
| 63 |
} |
| 64 |
tester->current_header_block = UNKNOWN_HEADER_BLOCK; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
switch (header_block) { |
| 71 |
case AWS_HTTP_HEADER_BLOCK_INFORMATIONAL: { |
| 72 |
ASSERT_FALSE(tester->response_headers_done); |
| 73 |
ASSERT_FALSE(tester->response_trailer_done); |
| 74 |
ASSERT_UINT_EQUALS(0, tester->response_body.len); |
| 75 |
|
| 76 |
|
| 77 |
struct aws_http_message *info_response = aws_http_message_new_response(tester->alloc); |
| 78 |
ASSERT_NOT_NULL(info_response); |
| 79 |
tester->info_responses[tester->num_info_responses++] = info_response; |
| 80 |
|
| 81 |
int status_code; |
| 82 |
ASSERT_SUCCESS(aws_http_stream_get_incoming_response_status(stream, &status_code)); |
| 83 |
ASSERT_SUCCESS(aws_http_message_set_response_status(info_response, status_code)); |
| 84 |
|
| 85 |
for (size_t i = 0; i < aws_http_headers_count(tester->current_info_headers); ++i) { |
| 86 |
struct aws_http_header header; |
| 87 |
ASSERT_SUCCESS(aws_http_headers_get_index(tester->current_info_headers, i, &header)); |
| 88 |
ASSERT_SUCCESS(aws_http_message_add_header(info_response, header)); |
| 89 |
} |
| 90 |
|
| 91 |
aws_http_headers_clear(tester->current_info_headers); |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
case AWS_HTTP_HEADER_BLOCK_MAIN: |
| 96 |
ASSERT_FALSE(tester->response_headers_done); |
| 97 |
ASSERT_FALSE(tester->response_trailer_done); |
| 98 |
ASSERT_UINT_EQUALS(0, tester->response_body.len); |
| 99 |
|
| 100 |
tester->response_headers_done = true; |
| 101 |
break; |
| 102 |
|
| 103 |
case AWS_HTTP_HEADER_BLOCK_TRAILING: |
| 104 |
ASSERT_FALSE(tester->response_trailer_done); |
| 105 |
ASSERT_TRUE(tester->response_headers_done || aws_http_headers_count(tester->response_headers) == 0); |
| 106 |
tester->response_trailer_done = true; |
| 107 |
break; |
| 108 |
} |
| 109 |
|
| 110 |
return AWS_OP_SUCCESS; |
| 111 |
} |
| 112 |
|
| 113 |
static int s_on_body(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) { |
| 114 |
(void)stream; |
| 115 |
struct client_stream_tester *tester = user_data; |
| 116 |
ASSERT_FALSE(tester->complete); |
| 117 |
ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&tester->response_body, data)); |
| 118 |
return AWS_OP_SUCCESS; |
| 119 |
} |
| 120 |
static void s_on_metrics( |
| 121 |
struct aws_http_stream *stream, |
| 122 |
const struct aws_http_stream_metrics *metrics, |
| 123 |
void *user_data) { |
| 124 |
(void)stream; |
| 125 |
struct client_stream_tester *tester = user_data; |
| 126 |
tester->metrics = *metrics; |
| 127 |
|
| 128 |
AWS_FATAL_ASSERT(metrics->stream_id == stream->id); |
| 129 |
if (metrics->receive_end_timestamp_ns > 0) { |
| 130 |
AWS_FATAL_ASSERT( |
| 131 |
metrics->receiving_duration_ns == metrics->receive_end_timestamp_ns - metrics->receive_start_timestamp_ns); |
| 132 |
} |
| 133 |
if (metrics->send_end_timestamp_ns > 0) { |
| 134 |
AWS_FATAL_ASSERT( |
| 135 |
metrics->sending_duration_ns == metrics->send_end_timestamp_ns - metrics->send_start_timestamp_ns); |
| 136 |
} |
| 137 |
if (metrics->receiving_duration_ns != -1) { |
| 138 |
AWS_FATAL_ASSERT(metrics->receive_end_timestamp_ns > 0); |
| 139 |
} |
| 140 |
if (metrics->sending_duration_ns != -1) { |
| 141 |
AWS_FATAL_ASSERT(metrics->send_end_timestamp_ns > 0); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static void s_on_complete(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 146 |
struct client_stream_tester *tester = user_data; |
| 147 |
|
| 148 |
|
| 149 |
AWS_FATAL_ASSERT(!tester->complete); |
| 150 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 151 |
AWS_FATAL_ASSERT(tester->current_header_block == UNKNOWN_HEADER_BLOCK); |
| 152 |
AWS_FATAL_ASSERT(aws_http_headers_count(tester->current_info_headers) == 0); |
| 153 |
AWS_FATAL_ASSERT(tester->response_headers_done || aws_http_headers_count(tester->response_headers) == 0); |
| 154 |
AWS_FATAL_ASSERT(tester->response_trailer_done || aws_http_headers_count(tester->response_trailer) == 0); |
| 155 |
} |
| 156 |
|
| 157 |
tester->complete = true; |
| 158 |
tester->on_complete_error_code = error_code; |
| 159 |
tester->on_complete_connection_is_open = aws_http_connection_is_open(aws_http_stream_get_connection(stream)); |
| 160 |
aws_http_stream_get_incoming_response_status(stream, &tester->response_status); |
| 161 |
} |
| 162 |
|
| 163 |
static void s_on_h2_remote_end_stream(struct aws_http_stream *stream, void *user_data) { |
| 164 |
(void)stream; |
| 165 |
struct client_stream_tester *tester = user_data; |
| 166 |
tester->on_h2_remote_end_stream_invoked = true; |
| 167 |
} |
| 168 |
|
| 169 |
static void s_on_destroy(void *user_data) { |
| 170 |
struct client_stream_tester *tester = user_data; |
| 171 |
|
| 172 |
|
| 173 |
AWS_FATAL_ASSERT(!tester->destroyed); |
| 174 |
tester->destroyed = true; |
| 175 |
} |
| 176 |
|
| 177 |
int client_stream_tester_init( |
| 178 |
struct client_stream_tester *tester, |
| 179 |
struct aws_allocator *alloc, |
| 180 |
const struct client_stream_tester_options *options) { |
| 181 |
|
| 182 |
AWS_ZERO_STRUCT(*tester); |
| 183 |
tester->alloc = alloc; |
| 184 |
|
| 185 |
tester->response_status = AWS_HTTP_STATUS_CODE_UNKNOWN; |
| 186 |
|
| 187 |
tester->current_header_block = UNKNOWN_HEADER_BLOCK; |
| 188 |
|
| 189 |
tester->current_info_headers = aws_http_headers_new(alloc); |
| 190 |
ASSERT_NOT_NULL(tester->current_info_headers); |
| 191 |
|
| 192 |
tester->response_headers = aws_http_headers_new(alloc); |
| 193 |
ASSERT_NOT_NULL(tester->response_headers); |
| 194 |
|
| 195 |
tester->response_trailer = aws_http_headers_new(alloc); |
| 196 |
ASSERT_NOT_NULL(tester->response_trailer); |
| 197 |
|
| 198 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->response_body, alloc, 128)); |
| 199 |
|
| 200 |
struct aws_http_make_request_options request_options = { |
| 201 |
.self_size = sizeof(request_options), |
| 202 |
.request = options->request, |
| 203 |
.user_data = tester, |
| 204 |
.on_response_headers = s_on_headers, |
| 205 |
.on_response_header_block_done = s_on_header_block_done, |
| 206 |
.on_response_body = s_on_body, |
| 207 |
.on_metrics = s_on_metrics, |
| 208 |
.on_complete = s_on_complete, |
| 209 |
.on_destroy = s_on_destroy, |
| 210 |
.http2_use_manual_data_writes = options->http2_manual_write, |
| 211 |
.use_manual_data_writes = options->use_manual_data_writes, |
| 212 |
.on_h2_remote_end_stream = options->on_h2_remote_end_stream ? s_on_h2_remote_end_stream : NULL, |
| 213 |
}; |
| 214 |
tester->stream = aws_http_connection_make_request(options->connection, &request_options); |
| 215 |
ASSERT_NOT_NULL(tester->stream); |
| 216 |
|
| 217 |
ASSERT_SUCCESS(aws_http_stream_activate(tester->stream)); |
| 218 |
return AWS_OP_SUCCESS; |
| 219 |
} |
| 220 |
|
| 221 |
void client_stream_tester_clean_up(struct client_stream_tester *tester) { |
| 222 |
for (size_t i = 0; i < tester->num_info_responses; ++i) { |
| 223 |
aws_http_message_release(tester->info_responses[i]); |
| 224 |
} |
| 225 |
|
| 226 |
aws_http_headers_release(tester->current_info_headers); |
| 227 |
aws_http_headers_release(tester->response_headers); |
| 228 |
aws_http_headers_release(tester->response_trailer); |
| 229 |
aws_byte_buf_clean_up(&tester->response_body); |
| 230 |
aws_http_stream_release(tester->stream); |
| 231 |
AWS_ZERO_STRUCT(*tester); |
| 232 |
} |