blob: be8301c5e33940c2254e8620d4f63daf2269fc1d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/http/httpcli.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36#include <string.h>
37
Craig Tillerf39f2d62015-06-06 14:43:06 -070038#include <grpc/grpc.h>
Craig Tillerb5075e32015-06-05 13:00:46 -070039#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/log.h>
Craig Tillerb5075e32015-06-05 13:00:46 -070041#include <grpc/support/string_util.h>
42#include <grpc/support/subprocess.h>
ctiller18b49ab2014-12-09 14:39:16 -080043#include <grpc/support/sync.h>
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/iomgr/iomgr.h"
Craig Tillerb5075e32015-06-05 13:00:46 -070045#include "test/core/util/port.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046#include "test/core/util/test_config.h"
47
Craig Tillerb1fa1d52015-05-11 10:27:53 -070048static int g_done = 0;
Craig Tiller49ff23f2015-06-01 11:32:04 -070049static grpc_httpcli_context g_context;
Craig Tiller69b093b2016-02-25 19:04:07 -080050static gpr_mu *g_mu;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070051static grpc_polling_entity g_pops;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
Craig Tillera82950e2015-09-22 12:33:20 -070053static gpr_timespec n_seconds_time(int seconds) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050054 return grpc_timeout_seconds_to_deadline(seconds);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055}
56
Craig Tillerf707d622016-05-06 14:26:12 -070057static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -070058 const char *expect =
59 "<html><head><title>Hello world!</title></head>"
60 "<body><p>This is a test</p></body></html>";
Craig Tillerf707d622016-05-06 14:26:12 -070061 grpc_http_response *response = arg;
Craig Tillera82950e2015-09-22 12:33:20 -070062 GPR_ASSERT(response);
63 GPR_ASSERT(response->status == 200);
64 GPR_ASSERT(response->body_length == strlen(expect));
65 GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
Craig Tiller69b093b2016-02-25 19:04:07 -080066 gpr_mu_lock(g_mu);
Craig Tillerb1fa1d52015-05-11 10:27:53 -070067 g_done = 1;
Craig Tillerc97065d2016-06-07 13:00:14 -070068 GPR_ASSERT(GRPC_LOG_IF_ERROR(
69 "pollset_kick",
70 grpc_pollset_kick(grpc_polling_entity_pollset(&g_pops), NULL)));
Craig Tiller69b093b2016-02-25 19:04:07 -080071 gpr_mu_unlock(g_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072}
73
Craig Tillered2164d2015-12-08 22:03:36 -080074static void test_get(int port) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 grpc_httpcli_request req;
Craig Tiller9a576332015-06-17 10:21:49 -070076 char *host;
Craig Tillerf5768a62015-09-22 10:54:34 -070077 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078
Craig Tillerb1fa1d52015-05-11 10:27:53 -070079 g_done = 0;
Craig Tillered2164d2015-12-08 22:03:36 -080080 gpr_log(GPR_INFO, "test_get");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081
Craig Tillera82950e2015-09-22 12:33:20 -070082 gpr_asprintf(&host, "localhost:%d", port);
83 gpr_log(GPR_INFO, "requesting from %s", host);
Craig Tillerb5075e32015-06-05 13:00:46 -070084
Craig Tillera82950e2015-09-22 12:33:20 -070085 memset(&req, 0, sizeof(req));
Craig Tillerb5075e32015-06-05 13:00:46 -070086 req.host = host;
Matthew Iselin1824f052016-02-10 12:16:06 +110087 req.http.path = "/get";
Craig Tillered2164d2015-12-08 22:03:36 -080088 req.handshaker = &grpc_httpcli_plaintext;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089
Craig Tillerf707d622016-05-06 14:26:12 -070090 grpc_http_response response;
91 memset(&response, 0, sizeof(response));
Craig Tiller20afa3d2016-10-17 14:52:14 -070092 grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get");
Craig Tiller91031da2016-12-28 15:44:25 -080093 grpc_httpcli_get(
94 &exec_ctx, &g_context, &g_pops, resource_quota, &req, n_seconds_time(15),
95 grpc_closure_create(on_finish, &response, grpc_schedule_on_exec_ctx),
96 &response);
Craig Tillera59c16c2016-10-31 07:25:01 -070097 grpc_resource_quota_unref_internal(&exec_ctx, resource_quota);
Craig Tiller69b093b2016-02-25 19:04:07 -080098 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -070099 while (!g_done) {
Craig Tillerd0a8ae12016-02-18 08:01:19 -0800100 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700101 GPR_ASSERT(GRPC_LOG_IF_ERROR(
102 "pollset_work",
Craig Tillerc97065d2016-06-07 13:00:14 -0700103 grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops),
104 &worker, gpr_now(GPR_CLOCK_MONOTONIC),
105 n_seconds_time(20))));
Craig Tiller69b093b2016-02-25 19:04:07 -0800106 gpr_mu_unlock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700107 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller69b093b2016-02-25 19:04:07 -0800108 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700109 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800110 gpr_mu_unlock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700111 gpr_free(host);
Craig Tiller6a64bfd2016-05-09 13:05:03 -0700112 grpc_http_response_destroy(&response);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113}
114
Craig Tillered2164d2015-12-08 22:03:36 -0800115static void test_post(int port) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116 grpc_httpcli_request req;
Craig Tiller9a576332015-06-17 10:21:49 -0700117 char *host;
Craig Tillerf5768a62015-09-22 10:54:34 -0700118 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119
Craig Tillerf39f2d62015-06-06 14:43:06 -0700120 g_done = 0;
Craig Tillered2164d2015-12-08 22:03:36 -0800121 gpr_log(GPR_INFO, "test_post");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122
Craig Tillera82950e2015-09-22 12:33:20 -0700123 gpr_asprintf(&host, "localhost:%d", port);
124 gpr_log(GPR_INFO, "posting to %s", host);
Craig Tillerb5075e32015-06-05 13:00:46 -0700125
Craig Tillera82950e2015-09-22 12:33:20 -0700126 memset(&req, 0, sizeof(req));
Craig Tillerb5075e32015-06-05 13:00:46 -0700127 req.host = host;
Matthew Iselin1824f052016-02-10 12:16:06 +1100128 req.http.path = "/post";
Craig Tillered2164d2015-12-08 22:03:36 -0800129 req.handshaker = &grpc_httpcli_plaintext;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130
Craig Tillerf707d622016-05-06 14:26:12 -0700131 grpc_http_response response;
132 memset(&response, 0, sizeof(response));
Craig Tiller20afa3d2016-10-17 14:52:14 -0700133 grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post");
Craig Tiller91031da2016-12-28 15:44:25 -0800134 grpc_httpcli_post(
135 &exec_ctx, &g_context, &g_pops, resource_quota, &req, "hello", 5,
136 n_seconds_time(15),
137 grpc_closure_create(on_finish, &response, grpc_schedule_on_exec_ctx),
138 &response);
Craig Tillera59c16c2016-10-31 07:25:01 -0700139 grpc_resource_quota_unref_internal(&exec_ctx, resource_quota);
Craig Tiller69b093b2016-02-25 19:04:07 -0800140 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700141 while (!g_done) {
Craig Tillerd0a8ae12016-02-18 08:01:19 -0800142 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700143 GPR_ASSERT(GRPC_LOG_IF_ERROR(
144 "pollset_work",
Craig Tillerc97065d2016-06-07 13:00:14 -0700145 grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops),
146 &worker, gpr_now(GPR_CLOCK_MONOTONIC),
147 n_seconds_time(20))));
Craig Tiller69b093b2016-02-25 19:04:07 -0800148 gpr_mu_unlock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700149 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller69b093b2016-02-25 19:04:07 -0800150 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700151 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800152 gpr_mu_unlock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700153 gpr_free(host);
Craig Tiller6a64bfd2016-05-09 13:05:03 -0700154 grpc_http_response_destroy(&response);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155}
Craig Tillerf39f2d62015-06-06 14:43:06 -0700156
Craig Tillerc97065d2016-06-07 13:00:14 -0700157static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) {
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700158 grpc_pollset_destroy(grpc_polling_entity_pollset(p));
Craig Tillerdfff1b82015-09-21 14:39:57 -0700159}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160
Craig Tillera82950e2015-09-22 12:33:20 -0700161int main(int argc, char **argv) {
Craig Tillerdfff1b82015-09-21 14:39:57 -0700162 grpc_closure destroyed;
Craig Tillerf5768a62015-09-22 10:54:34 -0700163 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller9a576332015-06-17 10:21:49 -0700164 gpr_subprocess *server;
Craig Tillerb5075e32015-06-05 13:00:46 -0700165 char *me = argv[0];
Craig Tillera82950e2015-09-22 12:33:20 -0700166 char *lslash = strrchr(me, '/');
Craig Tiller9a576332015-06-17 10:21:49 -0700167 char *args[4];
Craig Tillera82950e2015-09-22 12:33:20 -0700168 int port = grpc_pick_unused_port_or_die();
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100169 int arg_shift = 0;
170 /* figure out where we are */
171 char *root;
172 if (lslash) {
173 root = gpr_malloc((size_t)(lslash - me + 1));
174 memcpy(root, me, (size_t)(lslash - me));
175 root[lslash - me] = 0;
176 } else {
177 root = gpr_strdup(".");
178 }
Craig Tillerb5075e32015-06-05 13:00:46 -0700179
Nicolas "Pixel" Noblec27a99b2015-12-15 02:46:16 +0100180 GPR_ASSERT(argc <= 2);
181 if (argc == 2) {
182 args[0] = gpr_strdup(argv[1]);
Craig Tillera82950e2015-09-22 12:33:20 -0700183 } else {
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100184 arg_shift = 1;
185 gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root);
Matthew Iselin1824f052016-02-10 12:16:06 +1100186 gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root);
Craig Tillera82950e2015-09-22 12:33:20 -0700187 }
Craig Tillerb5075e32015-06-05 13:00:46 -0700188
189 /* start the server */
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100190 args[1 + arg_shift] = "--port";
191 gpr_asprintf(&args[2 + arg_shift], "%d", port);
192 server = gpr_subprocess_create(3 + arg_shift, (const char **)args);
Craig Tillera82950e2015-09-22 12:33:20 -0700193 GPR_ASSERT(server);
194 gpr_free(args[0]);
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100195 if (arg_shift) gpr_free(args[1]);
196 gpr_free(args[2 + arg_shift]);
197 gpr_free(root);
Craig Tillerb5075e32015-06-05 13:00:46 -0700198
Craig Tillera82950e2015-09-22 12:33:20 -0700199 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
200 gpr_time_from_seconds(5, GPR_TIMESPAN)));
Craig Tillerb5075e32015-06-05 13:00:46 -0700201
Craig Tillera82950e2015-09-22 12:33:20 -0700202 grpc_test_init(argc, argv);
203 grpc_init();
204 grpc_httpcli_context_init(&g_context);
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700205 grpc_pollset *pollset = gpr_malloc(grpc_pollset_size());
206 grpc_pollset_init(pollset, &g_mu);
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700207 g_pops = grpc_polling_entity_create_from_pollset(pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208
Craig Tillered2164d2015-12-08 22:03:36 -0800209 test_get(port);
210 test_post(port);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800212 grpc_httpcli_context_destroy(&exec_ctx, &g_context);
Craig Tiller91031da2016-12-28 15:44:25 -0800213 grpc_closure_init(&destroyed, destroy_pops, &g_pops,
214 grpc_schedule_on_exec_ctx);
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700215 grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops),
216 &destroyed);
Craig Tillera82950e2015-09-22 12:33:20 -0700217 grpc_exec_ctx_finish(&exec_ctx);
218 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700220 gpr_free(grpc_polling_entity_pollset(&g_pops));
Craig Tiller69b093b2016-02-25 19:04:07 -0800221
Craig Tillera82950e2015-09-22 12:33:20 -0700222 gpr_subprocess_destroy(server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223
224 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800225}