aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/http
diff options
context:
space:
mode:
Diffstat (limited to 'test/core/http')
-rw-r--r--test/core/http/format_request_test.c165
-rw-r--r--test/core/http/httpcli_test.c200
-rw-r--r--test/core/http/httpscli_test.c203
-rw-r--r--test/core/http/parser_test.c260
-rwxr-xr-xtest/core/http/test_server.py71
5 files changed, 899 insertions, 0 deletions
diff --git a/test/core/http/format_request_test.c b/test/core/http/format_request_test.c
new file mode 100644
index 0000000000..67dfd24803
--- /dev/null
+++ b/test/core/http/format_request_test.c
@@ -0,0 +1,165 @@
+/*
+ *
+ * Copyright 2015-2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "src/core/http/format_request.h"
+
+#include <string.h>
+
+#include <grpc/support/log.h>
+#include "test/core/util/test_config.h"
+
+static void test_format_get_request(void) {
+ grpc_http_header hdr = {"x-yz", "abc"};
+ grpc_httpcli_request req;
+ gpr_slice slice;
+
+ memset(&req, 0, sizeof(req));
+ req.host = "example.com";
+ req.http.path = "/index.html";
+ req.http.hdr_count = 1;
+ req.http.hdrs = &hdr;
+
+ slice = grpc_httpcli_format_get_request(&req);
+
+ 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"));
+
+ gpr_slice_unref(slice);
+}
+
+static void test_format_post_request(void) {
+ grpc_http_header hdr = {"x-yz", "abc"};
+ grpc_httpcli_request req;
+ gpr_slice slice;
+ char body_bytes[] = "fake body";
+ size_t body_len = 9;
+
+ memset(&req, 0, sizeof(req));
+ req.host = "example.com";
+ req.http.path = "/index.html";
+ req.http.hdr_count = 1;
+ req.http.hdrs = &hdr;
+
+ slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
+
+ 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"));
+
+ gpr_slice_unref(slice);
+}
+
+static void test_format_post_request_no_body(void) {
+ grpc_http_header hdr = {"x-yz", "abc"};
+ grpc_httpcli_request req;
+ gpr_slice slice;
+
+ memset(&req, 0, sizeof(req));
+ req.host = "example.com";
+ req.http.path = "/index.html";
+ req.http.hdr_count = 1;
+ req.http.hdrs = &hdr;
+
+ slice = grpc_httpcli_format_post_request(&req, NULL, 0);
+
+ 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"));
+
+ gpr_slice_unref(slice);
+}
+
+static void test_format_post_request_content_type_override(void) {
+ grpc_http_header hdrs[2];
+ grpc_httpcli_request req;
+ gpr_slice slice;
+ char body_bytes[] = "fake%20body";
+ size_t body_len = 11;
+
+ hdrs[0].key = "x-yz";
+ hdrs[0].value = "abc";
+ hdrs[1].key = "Content-Type";
+ hdrs[1].value = "application/x-www-form-urlencoded";
+ memset(&req, 0, sizeof(req));
+ req.host = "example.com";
+ req.http.path = "/index.html";
+ req.http.hdr_count = 2;
+ req.http.hdrs = hdrs;
+
+ slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
+
+ 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"));
+
+ gpr_slice_unref(slice);
+}
+
+int main(int argc, char **argv) {
+ grpc_test_init(argc, argv);
+
+ test_format_get_request();
+ test_format_post_request();
+ test_format_post_request_no_body();
+ test_format_post_request_content_type_override();
+
+ return 0;
+}
diff --git a/test/core/http/httpcli_test.c b/test/core/http/httpcli_test.c
new file mode 100644
index 0000000000..bdb7a02e9b
--- /dev/null
+++ b/test/core/http/httpcli_test.c
@@ -0,0 +1,200 @@
+/*
+ *
+ * Copyright 2015-2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "src/core/http/httpcli.h"
+
+#include <string.h>
+
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/subprocess.h>
+#include <grpc/support/sync.h>
+#include "src/core/iomgr/iomgr.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+static int g_done = 0;
+static grpc_httpcli_context g_context;
+static gpr_mu *g_mu;
+static grpc_pollset *g_pollset;
+
+static gpr_timespec n_seconds_time(int seconds) {
+ return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(seconds);
+}
+
+static void on_finish(grpc_exec_ctx *exec_ctx, void *arg,
+ const grpc_httpcli_response *response) {
+ const char *expect =
+ "<html><head><title>Hello world!</title></head>"
+ "<body><p>This is a test</p></body></html>";
+ GPR_ASSERT(arg == (void *)42);
+ GPR_ASSERT(response);
+ GPR_ASSERT(response->status == 200);
+ GPR_ASSERT(response->body_length == strlen(expect));
+ GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
+ gpr_mu_lock(g_mu);
+ g_done = 1;
+ grpc_pollset_kick(g_pollset, NULL);
+ gpr_mu_unlock(g_mu);
+}
+
+static void test_get(int port) {
+ grpc_httpcli_request req;
+ char *host;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+
+ g_done = 0;
+ gpr_log(GPR_INFO, "test_get");
+
+ gpr_asprintf(&host, "localhost:%d", port);
+ gpr_log(GPR_INFO, "requesting from %s", host);
+
+ memset(&req, 0, sizeof(req));
+ req.host = host;
+ req.http.path = "/get";
+ req.handshaker = &grpc_httpcli_plaintext;
+
+ grpc_httpcli_get(&exec_ctx, &g_context, g_pollset, &req, n_seconds_time(15),
+ on_finish, (void *)42);
+ gpr_mu_lock(g_mu);
+ while (!g_done) {
+ grpc_pollset_worker *worker = NULL;
+ grpc_pollset_work(&exec_ctx, g_pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
+ gpr_mu_unlock(g_mu);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_mu_lock(g_mu);
+ }
+ gpr_mu_unlock(g_mu);
+ gpr_free(host);
+}
+
+static void test_post(int port) {
+ grpc_httpcli_request req;
+ char *host;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+
+ g_done = 0;
+ gpr_log(GPR_INFO, "test_post");
+
+ gpr_asprintf(&host, "localhost:%d", port);
+ gpr_log(GPR_INFO, "posting to %s", host);
+
+ memset(&req, 0, sizeof(req));
+ req.host = host;
+ req.http.path = "/post";
+ req.handshaker = &grpc_httpcli_plaintext;
+
+ grpc_httpcli_post(&exec_ctx, &g_context, g_pollset, &req, "hello", 5,
+ n_seconds_time(15), on_finish, (void *)42);
+ gpr_mu_lock(g_mu);
+ while (!g_done) {
+ grpc_pollset_worker *worker = NULL;
+ grpc_pollset_work(&exec_ctx, g_pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
+ gpr_mu_unlock(g_mu);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_mu_lock(g_mu);
+ }
+ gpr_mu_unlock(g_mu);
+ gpr_free(host);
+}
+
+static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, bool success) {
+ grpc_pollset_destroy(p);
+}
+
+int main(int argc, char **argv) {
+ grpc_closure destroyed;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ gpr_subprocess *server;
+ char *me = argv[0];
+ char *lslash = strrchr(me, '/');
+ char *args[4];
+ int port = grpc_pick_unused_port_or_die();
+ int arg_shift = 0;
+ /* figure out where we are */
+ char *root;
+ if (lslash) {
+ root = gpr_malloc((size_t)(lslash - me + 1));
+ memcpy(root, me, (size_t)(lslash - me));
+ root[lslash - me] = 0;
+ } else {
+ root = gpr_strdup(".");
+ }
+
+ GPR_ASSERT(argc <= 2);
+ if (argc == 2) {
+ args[0] = gpr_strdup(argv[1]);
+ } else {
+ arg_shift = 1;
+ gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root);
+ gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root);
+ }
+
+ /* start the server */
+ args[1 + arg_shift] = "--port";
+ gpr_asprintf(&args[2 + arg_shift], "%d", port);
+ server = gpr_subprocess_create(3 + arg_shift, (const char **)args);
+ GPR_ASSERT(server);
+ gpr_free(args[0]);
+ if (arg_shift) gpr_free(args[1]);
+ gpr_free(args[2 + arg_shift]);
+ gpr_free(root);
+
+ gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
+ gpr_time_from_seconds(5, GPR_TIMESPAN)));
+
+ grpc_test_init(argc, argv);
+ grpc_init();
+ grpc_httpcli_context_init(&g_context);
+ g_pollset = gpr_malloc(grpc_pollset_size());
+ grpc_pollset_init(g_pollset, &g_mu);
+
+ test_get(port);
+ test_post(port);
+
+ grpc_httpcli_context_destroy(&g_context);
+ grpc_closure_init(&destroyed, destroy_pollset, g_pollset);
+ grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed);
+ grpc_exec_ctx_finish(&exec_ctx);
+ grpc_shutdown();
+
+ gpr_free(g_pollset);
+
+ gpr_subprocess_destroy(server);
+
+ return 0;
+}
diff --git a/test/core/http/httpscli_test.c b/test/core/http/httpscli_test.c
new file mode 100644
index 0000000000..21845b6a8e
--- /dev/null
+++ b/test/core/http/httpscli_test.c
@@ -0,0 +1,203 @@
+/*
+ *
+ * Copyright 2015-2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "src/core/http/httpcli.h"
+
+#include <string.h>
+
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/subprocess.h>
+#include <grpc/support/sync.h>
+#include "src/core/iomgr/iomgr.h"
+#include "test/core/util/port.h"
+#include "test/core/util/test_config.h"
+
+static int g_done = 0;
+static grpc_httpcli_context g_context;
+static gpr_mu *g_mu;
+static grpc_pollset *g_pollset;
+
+static gpr_timespec n_seconds_time(int seconds) {
+ return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(seconds);
+}
+
+static void on_finish(grpc_exec_ctx *exec_ctx, void *arg,
+ const grpc_httpcli_response *response) {
+ const char *expect =
+ "<html><head><title>Hello world!</title></head>"
+ "<body><p>This is a test</p></body></html>";
+ GPR_ASSERT(arg == (void *)42);
+ GPR_ASSERT(response);
+ GPR_ASSERT(response->status == 200);
+ GPR_ASSERT(response->body_length == strlen(expect));
+ GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
+ gpr_mu_lock(g_mu);
+ g_done = 1;
+ grpc_pollset_kick(g_pollset, NULL);
+ gpr_mu_unlock(g_mu);
+}
+
+static void test_get(int port) {
+ grpc_httpcli_request req;
+ char *host;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+
+ g_done = 0;
+ gpr_log(GPR_INFO, "test_get");
+
+ gpr_asprintf(&host, "localhost:%d", port);
+ gpr_log(GPR_INFO, "requesting from %s", host);
+
+ memset(&req, 0, sizeof(req));
+ req.host = host;
+ req.ssl_host_override = "foo.test.google.fr";
+ req.http.path = "/get";
+ req.handshaker = &grpc_httpcli_ssl;
+
+ grpc_httpcli_get(&exec_ctx, &g_context, g_pollset, &req, n_seconds_time(15),
+ on_finish, (void *)42);
+ gpr_mu_lock(g_mu);
+ while (!g_done) {
+ grpc_pollset_worker *worker = NULL;
+ grpc_pollset_work(&exec_ctx, g_pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
+ gpr_mu_unlock(g_mu);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_mu_lock(g_mu);
+ }
+ gpr_mu_unlock(g_mu);
+ gpr_free(host);
+}
+
+static void test_post(int port) {
+ grpc_httpcli_request req;
+ char *host;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+
+ g_done = 0;
+ gpr_log(GPR_INFO, "test_post");
+
+ gpr_asprintf(&host, "localhost:%d", port);
+ gpr_log(GPR_INFO, "posting to %s", host);
+
+ memset(&req, 0, sizeof(req));
+ req.host = host;
+ req.ssl_host_override = "foo.test.google.fr";
+ req.http.path = "/post";
+ req.handshaker = &grpc_httpcli_ssl;
+
+ grpc_httpcli_post(&exec_ctx, &g_context, g_pollset, &req, "hello", 5,
+ n_seconds_time(15), on_finish, (void *)42);
+ gpr_mu_lock(g_mu);
+ while (!g_done) {
+ grpc_pollset_worker *worker = NULL;
+ grpc_pollset_work(&exec_ctx, g_pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
+ gpr_mu_unlock(g_mu);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_mu_lock(g_mu);
+ }
+ gpr_mu_unlock(g_mu);
+ gpr_free(host);
+}
+
+static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, bool success) {
+ grpc_pollset_destroy(p);
+}
+
+int main(int argc, char **argv) {
+ grpc_closure destroyed;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ gpr_subprocess *server;
+ char *me = argv[0];
+ char *lslash = strrchr(me, '/');
+ char *args[5];
+ int port = grpc_pick_unused_port_or_die();
+ int arg_shift = 0;
+ /* figure out where we are */
+ char *root;
+ if (lslash) {
+ root = gpr_malloc((size_t)(lslash - me + 1));
+ memcpy(root, me, (size_t)(lslash - me));
+ root[lslash - me] = 0;
+ } else {
+ root = gpr_strdup(".");
+ }
+
+ GPR_ASSERT(argc <= 2);
+ if (argc == 2) {
+ args[0] = gpr_strdup(argv[1]);
+ } else {
+ arg_shift = 1;
+ gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root);
+ gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root);
+ }
+
+ /* start the server */
+ args[1 + arg_shift] = "--port";
+ gpr_asprintf(&args[2 + arg_shift], "%d", port);
+ args[3 + arg_shift] = "--ssl";
+ server = gpr_subprocess_create(4 + arg_shift, (const char **)args);
+ GPR_ASSERT(server);
+ gpr_free(args[0]);
+ if (arg_shift) gpr_free(args[1]);
+ gpr_free(args[2 + arg_shift]);
+ gpr_free(root);
+
+ gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
+ gpr_time_from_seconds(5, GPR_TIMESPAN)));
+
+ grpc_test_init(argc, argv);
+ grpc_init();
+ grpc_httpcli_context_init(&g_context);
+ g_pollset = gpr_malloc(grpc_pollset_size());
+ grpc_pollset_init(g_pollset, &g_mu);
+
+ test_get(port);
+ test_post(port);
+
+ grpc_httpcli_context_destroy(&g_context);
+ grpc_closure_init(&destroyed, destroy_pollset, g_pollset);
+ grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed);
+ grpc_exec_ctx_finish(&exec_ctx);
+ grpc_shutdown();
+
+ gpr_free(g_pollset);
+
+ gpr_subprocess_destroy(server);
+
+ return 0;
+}
diff --git a/test/core/http/parser_test.c b/test/core/http/parser_test.c
new file mode 100644
index 0000000000..338a301534
--- /dev/null
+++ b/test/core/http/parser_test.c
@@ -0,0 +1,260 @@
+/*
+ *
+ * Copyright 2015-2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "src/core/http/parser.h"
+
+#include <stdarg.h>
+#include <string.h>
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/useful.h>
+#include "test/core/util/slice_splitter.h"
+#include "test/core/util/test_config.h"
+
+static void test_request_succeeds(grpc_slice_split_mode split_mode,
+ char *request, char *expect_method,
+ grpc_http_version expect_version,
+ char *expect_path, char *expect_body, ...) {
+ grpc_http_parser parser;
+ gpr_slice input_slice = gpr_slice_from_copied_string(request);
+ size_t num_slices;
+ size_t i;
+ gpr_slice *slices;
+ va_list args;
+
+ grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
+ gpr_slice_unref(input_slice);
+
+ grpc_http_parser_init(&parser);
+
+ for (i = 0; i < num_slices; i++) {
+ GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i]));
+ gpr_slice_unref(slices[i]);
+ }
+ GPR_ASSERT(grpc_http_parser_eof(&parser));
+
+ GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type);
+ GPR_ASSERT(0 == strcmp(expect_method, parser.http.request.method));
+ GPR_ASSERT(0 == strcmp(expect_path, parser.http.request.path));
+ GPR_ASSERT(expect_version == parser.http.request.version);
+
+ if (expect_body != NULL) {
+ GPR_ASSERT(strlen(expect_body) == parser.http.request.body_length);
+ GPR_ASSERT(0 == memcmp(expect_body, parser.http.request.body,
+ parser.http.request.body_length));
+ } else {
+ GPR_ASSERT(parser.http.request.body_length == 0);
+ }
+
+ va_start(args, expect_body);
+ i = 0;
+ for (;;) {
+ char *expect_key;
+ char *expect_value;
+ expect_key = va_arg(args, char *);
+ if (!expect_key) break;
+ GPR_ASSERT(i < parser.http.request.hdr_count);
+ expect_value = va_arg(args, char *);
+ GPR_ASSERT(expect_value);
+ GPR_ASSERT(0 == strcmp(expect_key, parser.http.request.hdrs[i].key));
+ GPR_ASSERT(0 == strcmp(expect_value, parser.http.request.hdrs[i].value));
+ i++;
+ }
+ va_end(args);
+ GPR_ASSERT(i == parser.http.request.hdr_count);
+
+ grpc_http_parser_destroy(&parser);
+ gpr_free(slices);
+}
+
+static void test_succeeds(grpc_slice_split_mode split_mode, char *response,
+ int expect_status, char *expect_body, ...) {
+ grpc_http_parser parser;
+ gpr_slice input_slice = gpr_slice_from_copied_string(response);
+ size_t num_slices;
+ size_t i;
+ gpr_slice *slices;
+ va_list args;
+
+ grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
+ gpr_slice_unref(input_slice);
+
+ grpc_http_parser_init(&parser);
+
+ for (i = 0; i < num_slices; i++) {
+ GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i]));
+ gpr_slice_unref(slices[i]);
+ }
+ GPR_ASSERT(grpc_http_parser_eof(&parser));
+
+ GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type);
+ GPR_ASSERT(expect_status == parser.http.response.status);
+ if (expect_body != NULL) {
+ GPR_ASSERT(strlen(expect_body) == parser.http.response.body_length);
+ GPR_ASSERT(0 == memcmp(expect_body, parser.http.response.body,
+ parser.http.response.body_length));
+ } else {
+ GPR_ASSERT(parser.http.response.body_length == 0);
+ }
+
+ va_start(args, expect_body);
+ i = 0;
+ for (;;) {
+ char *expect_key;
+ char *expect_value;
+ expect_key = va_arg(args, char *);
+ if (!expect_key) break;
+ GPR_ASSERT(i < parser.http.response.hdr_count);
+ expect_value = va_arg(args, char *);
+ GPR_ASSERT(expect_value);
+ GPR_ASSERT(0 == strcmp(expect_key, parser.http.response.hdrs[i].key));
+ GPR_ASSERT(0 == strcmp(expect_value, parser.http.response.hdrs[i].value));
+ i++;
+ }
+ va_end(args);
+ GPR_ASSERT(i == parser.http.response.hdr_count);
+
+ grpc_http_parser_destroy(&parser);
+ gpr_free(slices);
+}
+
+static void test_fails(grpc_slice_split_mode split_mode, char *response) {
+ grpc_http_parser parser;
+ gpr_slice input_slice = gpr_slice_from_copied_string(response);
+ size_t num_slices;
+ size_t i;
+ gpr_slice *slices;
+ int done = 0;
+
+ grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
+ gpr_slice_unref(input_slice);
+
+ grpc_http_parser_init(&parser);
+
+ for (i = 0; i < num_slices; i++) {
+ if (!done && !grpc_http_parser_parse(&parser, slices[i])) {
+ done = 1;
+ }
+ gpr_slice_unref(slices[i]);
+ }
+ if (!done && !grpc_http_parser_eof(&parser)) {
+ done = 1;
+ }
+ GPR_ASSERT(done);
+
+ grpc_http_parser_destroy(&parser);
+ gpr_free(slices);
+}
+
+int main(int argc, char **argv) {
+ size_t i;
+ const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY,
+ GRPC_SLICE_SPLIT_ONE_BYTE};
+ char *tmp1, *tmp2;
+
+ grpc_test_init(argc, argv);
+
+ for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) {
+ test_succeeds(split_modes[i],
+ "HTTP/1.0 200 OK\r\n"
+ "xyz: abc\r\n"
+ "\r\n"
+ "hello world!",
+ 200, "hello world!", "xyz", "abc", NULL);
+ test_succeeds(split_modes[i],
+ "HTTP/1.0 404 Not Found\r\n"
+ "\r\n",
+ 404, NULL, NULL);
+ test_succeeds(split_modes[i],
+ "HTTP/1.1 200 OK\r\n"
+ "xyz: abc\r\n"
+ "\r\n"
+ "hello world!",
+ 200, "hello world!", "xyz", "abc", NULL);
+ test_request_succeeds(split_modes[i],
+ "GET / HTTP/1.0\r\n"
+ "\r\n",
+ "GET", GRPC_HTTP_HTTP10, "/", NULL, NULL);
+ test_request_succeeds(split_modes[i],
+ "GET / HTTP/1.0\r\n"
+ "\r\n"
+ "xyz",
+ "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
+ test_request_succeeds(split_modes[i],
+ "GET / HTTP/1.1\r\n"
+ "\r\n"
+ "xyz",
+ "GET", GRPC_HTTP_HTTP11, "/", "xyz", NULL);
+ test_request_succeeds(split_modes[i],
+ "GET / HTTP/2.0\r\n"
+ "\r\n"
+ "xyz",
+ "GET", GRPC_HTTP_HTTP20, "/", "xyz", NULL);
+ test_request_succeeds(split_modes[i],
+ "GET / HTTP/1.0\r\n"
+ "xyz: abc\r\n"
+ "\r\n"
+ "xyz",
+ "GET", GRPC_HTTP_HTTP10, "/", "xyz", "xyz", "abc",
+ NULL);
+ test_fails(split_modes[i], "HTTP/1.0\r\n");
+ test_fails(split_modes[i], "HTTP/1.2\r\n");
+ test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n");
+ test_fails(split_modes[i], "HTTP/1.0 200 OK\n");
+ test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n");
+ test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n");
+ test_fails(split_modes[i],
+ "HTTP/1.0 200 OK\r\n"
+ "xyz: abc\r\n"
+ " def\r\n"
+ "\r\n"
+ "hello world!");
+ test_fails(split_modes[i], "GET\r\n");
+ test_fails(split_modes[i], "GET /\r\n");
+ test_fails(split_modes[i], "GET / HTTP/0.0\r\n");
+ test_fails(split_modes[i], "GET / ____/1.0\r\n");
+ test_fails(split_modes[i], "GET / HTTP/1.2\r\n");
+
+ tmp1 = gpr_malloc(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH);
+ memset(tmp1, 'a', 2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1);
+ tmp1[2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1] = 0;
+ gpr_asprintf(&tmp2, "HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1);
+ test_fails(split_modes[i], tmp2);
+ gpr_free(tmp1);
+ gpr_free(tmp2);
+ }
+
+ return 0;
+}
diff --git a/test/core/http/test_server.py b/test/core/http/test_server.py
new file mode 100755
index 0000000000..ecde494cc0
--- /dev/null
+++ b/test/core/http/test_server.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python2.7
+# Copyright 2015-2016, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""Server for httpcli_test"""
+
+import argparse
+import BaseHTTPServer
+import os
+import ssl
+import sys
+
+_PEM = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../../..', 'src/core/tsi/test_creds/server1.pem'))
+_KEY = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../../..', 'src/core/tsi/test_creds/server1.key'))
+print _PEM
+open(_PEM).close()
+
+argp = argparse.ArgumentParser(description='Server for httpcli_test')
+argp.add_argument('-p', '--port', default=10080, type=int)
+argp.add_argument('-s', '--ssl', default=False, action='store_true')
+args = argp.parse_args()
+
+print 'server running on port %d' % args.port
+
+class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
+ def good(self):
+ self.send_response(200)
+ self.send_header('Content-Type', 'text/html')
+ self.end_headers()
+ self.wfile.write('<html><head><title>Hello world!</title></head>')
+ self.wfile.write('<body><p>This is a test</p></body></html>')
+
+ def do_GET(self):
+ if self.path == '/get':
+ self.good()
+
+ def do_POST(self):
+ content = self.rfile.read(int(self.headers.getheader('content-length')))
+ if self.path == '/post' and content == 'hello':
+ self.good()
+
+httpd = BaseHTTPServer.HTTPServer(('localhost', args.port), Handler)
+if args.ssl:
+ httpd.socket = ssl.wrap_socket(httpd.socket, certfile=_PEM, keyfile=_KEY, server_side=True)
+httpd.serve_forever()