|
|
@ -23,6 +23,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include <string> |
|
|
|
#include <string> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <gtest/gtest.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "absl/strings/str_format.h" |
|
|
|
#include "absl/strings/str_format.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <grpc/grpc.h> |
|
|
|
#include <grpc/grpc.h> |
|
|
@ -54,22 +56,22 @@ static void test_request_succeeds(grpc_slice_split_mode split_mode, |
|
|
|
grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request); |
|
|
|
grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request); |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_slices; i++) { |
|
|
|
for (i = 0; i < num_slices; i++) { |
|
|
|
GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i], nullptr) == |
|
|
|
ASSERT_EQ(grpc_http_parser_parse(&parser, slices[i], nullptr), |
|
|
|
GRPC_ERROR_NONE); |
|
|
|
GRPC_ERROR_NONE); |
|
|
|
grpc_slice_unref(slices[i]); |
|
|
|
grpc_slice_unref(slices[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); |
|
|
|
ASSERT_EQ(grpc_http_parser_eof(&parser), GRPC_ERROR_NONE); |
|
|
|
|
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type); |
|
|
|
ASSERT_EQ(GRPC_HTTP_REQUEST, parser.type); |
|
|
|
GPR_ASSERT(0 == strcmp(expect_method, request.method)); |
|
|
|
ASSERT_STREQ(expect_method, request.method); |
|
|
|
GPR_ASSERT(0 == strcmp(expect_path, request.path)); |
|
|
|
ASSERT_STREQ(expect_path, request.path); |
|
|
|
GPR_ASSERT(expect_version == request.version); |
|
|
|
ASSERT_EQ(expect_version, request.version); |
|
|
|
|
|
|
|
|
|
|
|
if (expect_body != nullptr) { |
|
|
|
if (expect_body != nullptr) { |
|
|
|
GPR_ASSERT(strlen(expect_body) == request.body_length); |
|
|
|
ASSERT_EQ(strlen(expect_body), request.body_length); |
|
|
|
GPR_ASSERT(0 == memcmp(expect_body, request.body, request.body_length)); |
|
|
|
ASSERT_EQ(0, memcmp(expect_body, request.body, request.body_length)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
GPR_ASSERT(request.body_length == 0); |
|
|
|
ASSERT_EQ(request.body_length, 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
va_start(args, expect_body); |
|
|
|
va_start(args, expect_body); |
|
|
@ -79,15 +81,15 @@ static void test_request_succeeds(grpc_slice_split_mode split_mode, |
|
|
|
char* expect_value; |
|
|
|
char* expect_value; |
|
|
|
expect_key = va_arg(args, char*); |
|
|
|
expect_key = va_arg(args, char*); |
|
|
|
if (!expect_key) break; |
|
|
|
if (!expect_key) break; |
|
|
|
GPR_ASSERT(i < request.hdr_count); |
|
|
|
ASSERT_LT(i, request.hdr_count); |
|
|
|
expect_value = va_arg(args, char*); |
|
|
|
expect_value = va_arg(args, char*); |
|
|
|
GPR_ASSERT(expect_value); |
|
|
|
ASSERT_TRUE(expect_value); |
|
|
|
GPR_ASSERT(0 == strcmp(expect_key, request.hdrs[i].key)); |
|
|
|
ASSERT_STREQ(expect_key, request.hdrs[i].key); |
|
|
|
GPR_ASSERT(0 == strcmp(expect_value, request.hdrs[i].value)); |
|
|
|
ASSERT_STREQ(expect_value, request.hdrs[i].value); |
|
|
|
i++; |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
va_end(args); |
|
|
|
va_end(args); |
|
|
|
GPR_ASSERT(i == request.hdr_count); |
|
|
|
ASSERT_EQ(i, request.hdr_count); |
|
|
|
|
|
|
|
|
|
|
|
grpc_http_request_destroy(&request); |
|
|
|
grpc_http_request_destroy(&request); |
|
|
|
grpc_http_parser_destroy(&parser); |
|
|
|
grpc_http_parser_destroy(&parser); |
|
|
@ -112,19 +114,19 @@ static void test_succeeds(grpc_slice_split_mode split_mode, |
|
|
|
grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); |
|
|
|
grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_slices; i++) { |
|
|
|
for (i = 0; i < num_slices; i++) { |
|
|
|
GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i], nullptr) == |
|
|
|
ASSERT_EQ(grpc_http_parser_parse(&parser, slices[i], nullptr), |
|
|
|
GRPC_ERROR_NONE); |
|
|
|
GRPC_ERROR_NONE); |
|
|
|
grpc_slice_unref(slices[i]); |
|
|
|
grpc_slice_unref(slices[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); |
|
|
|
ASSERT_EQ(grpc_http_parser_eof(&parser), GRPC_ERROR_NONE); |
|
|
|
|
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type); |
|
|
|
ASSERT_EQ(GRPC_HTTP_RESPONSE, parser.type); |
|
|
|
GPR_ASSERT(expect_status == response.status); |
|
|
|
ASSERT_EQ(expect_status, response.status); |
|
|
|
if (expect_body != nullptr) { |
|
|
|
if (expect_body != nullptr) { |
|
|
|
GPR_ASSERT(strlen(expect_body) == response.body_length); |
|
|
|
ASSERT_EQ(strlen(expect_body), response.body_length); |
|
|
|
GPR_ASSERT(0 == memcmp(expect_body, response.body, response.body_length)); |
|
|
|
ASSERT_EQ(0, memcmp(expect_body, response.body, response.body_length)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
GPR_ASSERT(response.body_length == 0); |
|
|
|
ASSERT_EQ(response.body_length, 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
va_start(args, expect_body); |
|
|
|
va_start(args, expect_body); |
|
|
@ -134,15 +136,15 @@ static void test_succeeds(grpc_slice_split_mode split_mode, |
|
|
|
char* expect_value; |
|
|
|
char* expect_value; |
|
|
|
expect_key = va_arg(args, char*); |
|
|
|
expect_key = va_arg(args, char*); |
|
|
|
if (!expect_key) break; |
|
|
|
if (!expect_key) break; |
|
|
|
GPR_ASSERT(i < response.hdr_count); |
|
|
|
ASSERT_LT(i, response.hdr_count); |
|
|
|
expect_value = va_arg(args, char*); |
|
|
|
expect_value = va_arg(args, char*); |
|
|
|
GPR_ASSERT(expect_value); |
|
|
|
ASSERT_TRUE(expect_value); |
|
|
|
GPR_ASSERT(0 == strcmp(expect_key, response.hdrs[i].key)); |
|
|
|
ASSERT_STREQ(expect_key, response.hdrs[i].key); |
|
|
|
GPR_ASSERT(0 == strcmp(expect_value, response.hdrs[i].value)); |
|
|
|
ASSERT_STREQ(expect_value, response.hdrs[i].value); |
|
|
|
i++; |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
va_end(args); |
|
|
|
va_end(args); |
|
|
|
GPR_ASSERT(i == response.hdr_count); |
|
|
|
ASSERT_EQ(i, response.hdr_count); |
|
|
|
|
|
|
|
|
|
|
|
grpc_http_response_destroy(&response); |
|
|
|
grpc_http_response_destroy(&response); |
|
|
|
grpc_http_parser_destroy(&parser); |
|
|
|
grpc_http_parser_destroy(&parser); |
|
|
@ -174,7 +176,7 @@ static void test_fails(grpc_slice_split_mode split_mode, |
|
|
|
if (GRPC_ERROR_NONE == error) { |
|
|
|
if (GRPC_ERROR_NONE == error) { |
|
|
|
error = grpc_http_parser_eof(&parser); |
|
|
|
error = grpc_http_parser_eof(&parser); |
|
|
|
} |
|
|
|
} |
|
|
|
GPR_ASSERT(!GRPC_ERROR_IS_NONE(error)); |
|
|
|
ASSERT_FALSE(GRPC_ERROR_IS_NONE(error)); |
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
|
|
|
|
|
|
|
grpc_http_response_destroy(&response); |
|
|
|
grpc_http_response_destroy(&response); |
|
|
@ -207,7 +209,7 @@ static void test_request_fails(grpc_slice_split_mode split_mode, |
|
|
|
if (GRPC_ERROR_IS_NONE(error)) { |
|
|
|
if (GRPC_ERROR_IS_NONE(error)) { |
|
|
|
error = grpc_http_parser_eof(&parser); |
|
|
|
error = grpc_http_parser_eof(&parser); |
|
|
|
} |
|
|
|
} |
|
|
|
GPR_ASSERT(!GRPC_ERROR_IS_NONE(error)); |
|
|
|
ASSERT_FALSE(GRPC_ERROR_IS_NONE(error)); |
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
|
|
|
|
|
|
|
grpc_http_request_destroy(&request); |
|
|
|
grpc_http_request_destroy(&request); |
|
|
@ -215,14 +217,10 @@ static void test_request_fails(grpc_slice_split_mode split_mode, |
|
|
|
gpr_free(slices); |
|
|
|
gpr_free(slices); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
TEST(ParserTest, MainTest) { |
|
|
|
size_t i; |
|
|
|
size_t i; |
|
|
|
const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY, |
|
|
|
const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY, |
|
|
|
GRPC_SLICE_SPLIT_ONE_BYTE}; |
|
|
|
GRPC_SLICE_SPLIT_ONE_BYTE}; |
|
|
|
|
|
|
|
|
|
|
|
grpc::testing::TestEnvironment env(&argc, argv); |
|
|
|
|
|
|
|
grpc_init(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) { |
|
|
|
for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) { |
|
|
|
test_succeeds(split_modes[i], |
|
|
|
test_succeeds(split_modes[i], |
|
|
|
"HTTP/1.0 200 OK\r\n" |
|
|
|
"HTTP/1.0 200 OK\r\n" |
|
|
@ -332,7 +330,11 @@ int main(int argc, char** argv) { |
|
|
|
gpr_free(tmp1); |
|
|
|
gpr_free(tmp1); |
|
|
|
test_fails(split_modes[i], tmp2.c_str()); |
|
|
|
test_fails(split_modes[i], tmp2.c_str()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
grpc_shutdown(); |
|
|
|
int main(int argc, char** argv) { |
|
|
|
return 0; |
|
|
|
grpc::testing::TestEnvironment env(&argc, argv); |
|
|
|
|
|
|
|
::testing::InitGoogleTest(&argc, argv); |
|
|
|
|
|
|
|
grpc::testing::TestGrpcScope grpc_scope; |
|
|
|
|
|
|
|
return RUN_ALL_TESTS(); |
|
|
|
} |
|
|
|
} |
|
|
|