format_request_test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/httpcli/format_request.h"
  34. #include <string.h>
  35. #include <grpc/support/log.h>
  36. #include "test/core/util/test_config.h"
  37. static void
  38. test_format_get_request (void)
  39. {
  40. grpc_httpcli_header hdr = { "x-yz", "abc" };
  41. grpc_httpcli_request req;
  42. gpr_slice slice;
  43. memset (&req, 0, sizeof (req));
  44. req.host = "example.com";
  45. req.path = "/index.html";
  46. req.hdr_count = 1;
  47. req.hdrs = &hdr;
  48. slice = grpc_httpcli_format_get_request (&req);
  49. GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "GET /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "\r\n"));
  50. gpr_slice_unref (slice);
  51. }
  52. static void
  53. test_format_post_request (void)
  54. {
  55. grpc_httpcli_header hdr = { "x-yz", "abc" };
  56. grpc_httpcli_request req;
  57. gpr_slice slice;
  58. char body_bytes[] = "fake body";
  59. size_t body_len = 9;
  60. memset (&req, 0, sizeof (req));
  61. req.host = "example.com";
  62. req.path = "/index.html";
  63. req.hdr_count = 1;
  64. req.hdrs = &hdr;
  65. slice = grpc_httpcli_format_post_request (&req, body_bytes, body_len);
  66. GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "POST /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "Content-Type: text/plain\r\n" "Content-Length: 9\r\n" "\r\n" "fake body"));
  67. gpr_slice_unref (slice);
  68. }
  69. static void
  70. test_format_post_request_no_body (void)
  71. {
  72. grpc_httpcli_header hdr = { "x-yz", "abc" };
  73. grpc_httpcli_request req;
  74. gpr_slice slice;
  75. memset (&req, 0, sizeof (req));
  76. req.host = "example.com";
  77. req.path = "/index.html";
  78. req.hdr_count = 1;
  79. req.hdrs = &hdr;
  80. slice = grpc_httpcli_format_post_request (&req, NULL, 0);
  81. GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "POST /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "\r\n"));
  82. gpr_slice_unref (slice);
  83. }
  84. static void
  85. test_format_post_request_content_type_override (void)
  86. {
  87. grpc_httpcli_header hdrs[2];
  88. grpc_httpcli_request req;
  89. gpr_slice slice;
  90. char body_bytes[] = "fake%20body";
  91. size_t body_len = 11;
  92. hdrs[0].key = "x-yz";
  93. hdrs[0].value = "abc";
  94. hdrs[1].key = "Content-Type";
  95. hdrs[1].value = "application/x-www-form-urlencoded";
  96. memset (&req, 0, sizeof (req));
  97. req.host = "example.com";
  98. req.path = "/index.html";
  99. req.hdr_count = 2;
  100. req.hdrs = hdrs;
  101. slice = grpc_httpcli_format_post_request (&req, body_bytes, body_len);
  102. GPR_ASSERT (0 == gpr_slice_str_cmp (slice, "POST /index.html HTTP/1.0\r\n" "Host: example.com\r\n" "Connection: close\r\n" "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" "x-yz: abc\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: 11\r\n" "\r\n" "fake%20body"));
  103. gpr_slice_unref (slice);
  104. }
  105. int
  106. main (int argc, char **argv)
  107. {
  108. grpc_test_init (argc, argv);
  109. test_format_get_request ();
  110. test_format_post_request ();
  111. test_format_post_request_no_body ();
  112. test_format_post_request_content_type_override ();
  113. return 0;
  114. }