format_request_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/http/format_request.h"
  19. #include <string.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/log.h>
  22. #include "test/core/util/test_config.h"
  23. static void test_format_get_request(void) {
  24. grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
  25. grpc_http_request req;
  26. grpc_slice slice;
  27. const char* host = "example.com";
  28. memset(&req, 0, sizeof(req));
  29. req.hdr_count = 1;
  30. req.hdrs = &hdr;
  31. slice = grpc_httpcli_format_get_request(&req, host, "/index.html");
  32. GPR_ASSERT(0 == grpc_slice_str_cmp(slice,
  33. "GET /index.html HTTP/1.0\r\n"
  34. "Host: example.com\r\n"
  35. "Connection: close\r\n"
  36. "User-Agent: " GRPC_HTTPCLI_USER_AGENT
  37. "\r\n"
  38. "x-yz: abc\r\n"
  39. "\r\n"));
  40. grpc_slice_unref(slice);
  41. }
  42. static void test_format_post_request(void) {
  43. grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
  44. grpc_http_request req;
  45. grpc_slice slice;
  46. const char* host = "example.com";
  47. memset(&req, 0, sizeof(req));
  48. req.hdr_count = 1;
  49. req.hdrs = &hdr;
  50. req.body = const_cast<char*>("fake body");
  51. req.body_length = 9;
  52. slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
  53. GPR_ASSERT(0 == grpc_slice_str_cmp(slice,
  54. "POST /index.html HTTP/1.0\r\n"
  55. "Host: example.com\r\n"
  56. "Connection: close\r\n"
  57. "User-Agent: " GRPC_HTTPCLI_USER_AGENT
  58. "\r\n"
  59. "x-yz: abc\r\n"
  60. "Content-Type: text/plain\r\n"
  61. "Content-Length: 9\r\n"
  62. "\r\n"
  63. "fake body"));
  64. grpc_slice_unref(slice);
  65. }
  66. static void test_format_post_request_no_body(void) {
  67. grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
  68. grpc_http_request req;
  69. grpc_slice slice;
  70. const char* host = "example.com";
  71. memset(&req, 0, sizeof(req));
  72. req.hdr_count = 1;
  73. req.hdrs = &hdr;
  74. slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
  75. GPR_ASSERT(0 == grpc_slice_str_cmp(slice,
  76. "POST /index.html HTTP/1.0\r\n"
  77. "Host: example.com\r\n"
  78. "Connection: close\r\n"
  79. "User-Agent: " GRPC_HTTPCLI_USER_AGENT
  80. "\r\n"
  81. "x-yz: abc\r\n"
  82. "\r\n"));
  83. grpc_slice_unref(slice);
  84. }
  85. static void test_format_post_request_content_type_override(void) {
  86. grpc_http_header hdrs[2];
  87. grpc_http_request req;
  88. grpc_slice slice;
  89. const char* host = "example.com";
  90. hdrs[0].key = const_cast<char*>("x-yz");
  91. hdrs[0].value = const_cast<char*>("abc");
  92. hdrs[1].key = const_cast<char*>("Content-Type");
  93. hdrs[1].value = const_cast<char*>("application/x-www-form-urlencoded");
  94. memset(&req, 0, sizeof(req));
  95. req.hdr_count = 2;
  96. req.hdrs = hdrs;
  97. req.body = const_cast<char*>("fake%20body");
  98. req.body_length = 11;
  99. slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
  100. GPR_ASSERT(0 == grpc_slice_str_cmp(
  101. slice,
  102. "POST /index.html HTTP/1.0\r\n"
  103. "Host: example.com\r\n"
  104. "Connection: close\r\n"
  105. "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"
  106. "x-yz: abc\r\n"
  107. "Content-Type: application/x-www-form-urlencoded\r\n"
  108. "Content-Length: 11\r\n"
  109. "\r\n"
  110. "fake%20body"));
  111. grpc_slice_unref(slice);
  112. }
  113. int main(int argc, char** argv) {
  114. grpc::testing::TestEnvironment env(argc, argv);
  115. grpc_init();
  116. test_format_get_request();
  117. test_format_post_request();
  118. test_format_post_request_no_body();
  119. test_format_post_request_content_type_override();
  120. grpc_shutdown();
  121. return 0;
  122. }