Line data Source code
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 :
34 : #include "src/core/httpcli/httpcli.h"
35 :
36 : #include <string.h>
37 :
38 : #include <grpc/grpc.h>
39 : #include "src/core/iomgr/iomgr.h"
40 : #include <grpc/support/alloc.h>
41 : #include <grpc/support/log.h>
42 : #include <grpc/support/string_util.h>
43 : #include <grpc/support/subprocess.h>
44 : #include <grpc/support/sync.h>
45 : #include "test/core/util/port.h"
46 : #include "test/core/util/test_config.h"
47 :
48 : static int g_done = 0;
49 : static grpc_httpcli_context g_context;
50 : static grpc_pollset g_pollset;
51 :
52 25 : static gpr_timespec n_seconds_time(int seconds) {
53 25 : return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(seconds);
54 : }
55 :
56 2 : static void on_finish(grpc_exec_ctx *exec_ctx, void *arg,
57 : const grpc_httpcli_response *response) {
58 2 : const char *expect =
59 : "<html><head><title>Hello world!</title></head>"
60 : "<body><p>This is a test</p></body></html>";
61 2 : GPR_ASSERT(arg == (void *)42);
62 2 : GPR_ASSERT(response);
63 2 : GPR_ASSERT(response->status == 200);
64 2 : GPR_ASSERT(response->body_length == strlen(expect));
65 2 : GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
66 2 : gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset));
67 2 : g_done = 1;
68 2 : grpc_pollset_kick(&g_pollset, NULL);
69 2 : gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset));
70 2 : }
71 :
72 1 : static void test_get(int use_ssl, int port) {
73 : grpc_httpcli_request req;
74 : char *host;
75 1 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
76 :
77 1 : g_done = 0;
78 1 : gpr_log(GPR_INFO, "running %s with use_ssl=%d.", "test_get", use_ssl);
79 :
80 1 : gpr_asprintf(&host, "localhost:%d", port);
81 1 : gpr_log(GPR_INFO, "requesting from %s", host);
82 :
83 1 : memset(&req, 0, sizeof(req));
84 1 : req.host = host;
85 1 : req.path = "/get";
86 1 : req.handshaker = use_ssl ? &grpc_httpcli_ssl : &grpc_httpcli_plaintext;
87 :
88 1 : grpc_httpcli_get(&exec_ctx, &g_context, &g_pollset, &req, n_seconds_time(15),
89 : on_finish, (void *)42);
90 1 : gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset));
91 14 : while (!g_done) {
92 : grpc_pollset_worker worker;
93 12 : grpc_pollset_work(&exec_ctx, &g_pollset, &worker,
94 : gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
95 12 : gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset));
96 12 : grpc_exec_ctx_finish(&exec_ctx);
97 12 : gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset));
98 : }
99 1 : gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset));
100 1 : gpr_free(host);
101 1 : }
102 :
103 1 : static void test_post(int use_ssl, int port) {
104 : grpc_httpcli_request req;
105 : char *host;
106 1 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
107 :
108 1 : g_done = 0;
109 1 : gpr_log(GPR_INFO, "running %s with use_ssl=%d.", "test_post", (int)use_ssl);
110 :
111 1 : gpr_asprintf(&host, "localhost:%d", port);
112 1 : gpr_log(GPR_INFO, "posting to %s", host);
113 :
114 1 : memset(&req, 0, sizeof(req));
115 1 : req.host = host;
116 1 : req.path = "/post";
117 1 : req.handshaker = use_ssl ? &grpc_httpcli_ssl : &grpc_httpcli_plaintext;
118 :
119 1 : grpc_httpcli_post(&exec_ctx, &g_context, &g_pollset, &req, "hello", 5,
120 : n_seconds_time(15), on_finish, (void *)42);
121 1 : gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset));
122 13 : while (!g_done) {
123 : grpc_pollset_worker worker;
124 11 : grpc_pollset_work(&exec_ctx, &g_pollset, &worker,
125 : gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
126 11 : gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset));
127 11 : grpc_exec_ctx_finish(&exec_ctx);
128 11 : gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset));
129 : }
130 1 : gpr_mu_unlock(GRPC_POLLSET_MU(&g_pollset));
131 1 : gpr_free(host);
132 1 : }
133 :
134 1 : static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, int success) {
135 1 : grpc_pollset_destroy(p);
136 1 : }
137 :
138 1 : int main(int argc, char **argv) {
139 : grpc_closure destroyed;
140 1 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
141 : gpr_subprocess *server;
142 1 : char *me = argv[0];
143 1 : char *lslash = strrchr(me, '/');
144 : char *args[4];
145 : char root[1024];
146 1 : int port = grpc_pick_unused_port_or_die();
147 :
148 : /* figure out where we are */
149 1 : if (lslash) {
150 1 : memcpy(root, me, (size_t)(lslash - me));
151 1 : root[lslash - me] = 0;
152 : } else {
153 0 : strcpy(root, ".");
154 : }
155 :
156 : /* start the server */
157 1 : gpr_asprintf(&args[0], "%s/../../test/core/httpcli/test_server.py", root);
158 1 : args[1] = "--port";
159 1 : gpr_asprintf(&args[2], "%d", port);
160 1 : server = gpr_subprocess_create(3, (const char **)args);
161 1 : GPR_ASSERT(server);
162 1 : gpr_free(args[0]);
163 1 : gpr_free(args[2]);
164 :
165 1 : gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
166 : gpr_time_from_seconds(5, GPR_TIMESPAN)));
167 :
168 1 : grpc_test_init(argc, argv);
169 1 : grpc_init();
170 1 : grpc_httpcli_context_init(&g_context);
171 1 : grpc_pollset_init(&g_pollset);
172 :
173 1 : test_get(0, port);
174 1 : test_post(0, port);
175 :
176 1 : grpc_httpcli_context_destroy(&g_context);
177 1 : grpc_closure_init(&destroyed, destroy_pollset, &g_pollset);
178 1 : grpc_pollset_shutdown(&exec_ctx, &g_pollset, &destroyed);
179 1 : grpc_exec_ctx_finish(&exec_ctx);
180 1 : grpc_shutdown();
181 :
182 1 : gpr_subprocess_destroy(server);
183 :
184 1 : return 0;
185 : }
|