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