httpd_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <libtest/test.hpp>
  39. using namespace libtest;
  40. #include <libgearman/gearman.h>
  41. #include "tests/start_worker.h"
  42. #include <cassert>
  43. #include <cstdio>
  44. #include <cstdlib>
  45. #include <cstring>
  46. #include <sys/stat.h>
  47. #include <sys/types.h>
  48. #include <unistd.h>
  49. #include <tests/basic.h>
  50. #include <tests/context.h>
  51. #include "tests/workers/v2/echo_or_react.h"
  52. // Prototypes
  53. #ifndef __INTEL_COMPILER
  54. #pragma GCC diagnostic ignored "-Wold-style-cast"
  55. #endif
  56. static char host_url[1024]= { 0 };
  57. #define WORKER_FUNCTION_NAME "httpd_worker"
  58. static test_return_t curl_no_function_TEST(void *)
  59. {
  60. Application curl("/usr/bin/curl");
  61. curl.add_option(host_url);
  62. ASSERT_EQ(Application::SUCCESS, curl.run());
  63. ASSERT_EQ(Application::SUCCESS, curl.join());
  64. return TEST_SUCCESS;
  65. }
  66. static test_return_t curl_function_no_body_TEST(void *)
  67. {
  68. Application curl("/usr/bin/curl");
  69. char worker_url[1024 + sizeof(WORKER_FUNCTION_NAME) - 1];
  70. snprintf(worker_url, sizeof(worker_url), "%s%s", host_url, WORKER_FUNCTION_NAME);
  71. curl.add_option(worker_url);
  72. curl.add_option("--header", "\"X-Gearman-Unique: curl_function_no_body_TEST\"");
  73. ASSERT_EQ(Application::SUCCESS, curl.run());
  74. ASSERT_EQ(Application::SUCCESS, curl.join());
  75. test_zero(curl.stdout_result_length());
  76. return TEST_SUCCESS;
  77. }
  78. static test_return_t curl_function_TEST(void *)
  79. {
  80. // Cleanup previous run
  81. unlink("var/tmp/curl_function_TEST.out");
  82. Application curl("/usr/bin/curl");
  83. char worker_url[1024 + sizeof(WORKER_FUNCTION_NAME) - 1];
  84. snprintf(worker_url, sizeof(worker_url), "%s%s", host_url, WORKER_FUNCTION_NAME);
  85. curl.add_option("--header", "\"X-Gearman-Unique: curl_function_TEST\"");
  86. curl.add_option("--data", "fubar");
  87. curl.add_option("--silent");
  88. curl.add_option("--show-error");
  89. curl.add_option("--output", "var/tmp/curl_function_TEST.out");
  90. curl.add_option("--connect-timeout", "1");
  91. curl.add_option(worker_url);
  92. ASSERT_EQ(Application::SUCCESS, curl.run());
  93. ASSERT_EQ(Application::SUCCESS, curl.join());
  94. test_zero(curl.stdout_result_length());
  95. struct stat stat_buffer;
  96. test_zero(stat("var/tmp/curl_function_TEST.out", &stat_buffer));
  97. ASSERT_TRUE(stat_buffer.st_size >= off_t(146));
  98. test_zero(unlink("var/tmp/curl_function_TEST.out"));
  99. return TEST_SUCCESS;
  100. }
  101. static test_return_t GET_TEST(void *)
  102. {
  103. libtest::http::GET get(host_url);
  104. ASSERT_EQ(true, get.execute());
  105. return TEST_SUCCESS;
  106. }
  107. static test_return_t HEAD_TEST(void *)
  108. {
  109. libtest::http::HEAD head(host_url);
  110. ASSERT_EQ(true, head.execute());
  111. return TEST_SUCCESS;
  112. }
  113. static void *world_create(server_startup_st& servers, test_return_t& error)
  114. {
  115. if (valgrind_is_caller())
  116. {
  117. error= TEST_SKIPPED;
  118. return NULL;
  119. }
  120. in_port_t http_port= libtest::get_free_port();
  121. int length= snprintf(host_url, sizeof(host_url), "http://localhost:%d/", int(http_port));
  122. fatal_assert(length > 0 and sizeof(length) < sizeof(host_url));
  123. char buffer[1024];
  124. length= snprintf(buffer, sizeof(buffer), "--http-port=%d", int(http_port));
  125. fatal_assert(length > 0 and sizeof(length) < sizeof(buffer));
  126. const char *argv[]= { "--protocol=http", buffer, 0 };
  127. if (server_startup(servers, "gearmand", libtest::default_port(), argv) == false)
  128. {
  129. error= TEST_SKIPPED;
  130. return NULL;
  131. }
  132. worker_handles_st *workers= new worker_handles_st();
  133. gearman_function_t echo_react_fn= gearman_function_create(echo_or_react_worker_v2);
  134. workers->push(test_worker_start(libtest::default_port(), NULL, WORKER_FUNCTION_NAME, echo_react_fn, NULL, gearman_worker_options_t()));
  135. return workers;
  136. }
  137. static bool world_destroy(void *object)
  138. {
  139. worker_handles_st *workers= (worker_handles_st *)object;
  140. delete workers;
  141. return false;
  142. }
  143. static test_return_t check_for_libcurl(void *)
  144. {
  145. test_skip(true, HAVE_LIBCURL);
  146. return TEST_SUCCESS;
  147. }
  148. static test_return_t check_for_curl(void *)
  149. {
  150. test_skip(0, access("/usr/bin/curl", X_OK));
  151. return TEST_SUCCESS;
  152. }
  153. test_st curl_TESTS[] ={
  154. { "curl /", 0, curl_no_function_TEST },
  155. { "curl /" WORKER_FUNCTION_NAME, 0, curl_function_no_body_TEST },
  156. { "curl /" WORKER_FUNCTION_NAME " --data=fubar", 0, curl_function_TEST },
  157. { 0, 0, 0 }
  158. };
  159. test_st GET_TESTS[] ={
  160. { "GET /", 0, GET_TEST },
  161. { 0, 0, 0 }
  162. };
  163. test_st HEAD_TESTS[] ={
  164. { "HEAD /", 0, HEAD_TEST },
  165. { 0, 0, 0 }
  166. };
  167. test_st regression_TESTS[] ={
  168. { 0, 0, 0 }
  169. };
  170. collection_st collection[] ={
  171. { "curl", check_for_curl, 0, curl_TESTS },
  172. { "GET", check_for_libcurl, 0, GET_TESTS },
  173. { "regression", 0, 0, regression_TESTS },
  174. { 0, 0, 0, 0 }
  175. };
  176. void get_world(libtest::Framework *world)
  177. {
  178. world->collections(collection);
  179. world->create(world_create);
  180. world->destroy(world_destroy);
  181. }