diff options
Diffstat (limited to 'test/core/httpcli')
-rw-r--r-- | test/core/httpcli/format_request_test.c | 166 | ||||
-rw-r--r-- | test/core/httpcli/httpcli_test.c | 101 | ||||
-rw-r--r-- | test/core/httpcli/parser_test.c | 155 |
3 files changed, 422 insertions, 0 deletions
diff --git a/test/core/httpcli/format_request_test.c b/test/core/httpcli/format_request_test.c new file mode 100644 index 0000000000..12ea7fda60 --- /dev/null +++ b/test/core/httpcli/format_request_test.c @@ -0,0 +1,166 @@ +/* + * + * Copyright 2014, 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/httpcli/format_request.h" + +#include <string.h> + +#include <grpc/support/log.h> +#include "test/core/util/test_config.h" + +static void test_format_get_request() { + grpc_httpcli_header hdr = {"x-yz", "abc"}; + grpc_httpcli_request req; + gpr_slice slice; + + + memset(&req, 0, sizeof(req)); + req.host = "example.com"; + req.path = "/index.html"; + req.hdr_count = 1; + req.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() { + grpc_httpcli_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.path = "/index.html"; + req.hdr_count = 1; + req.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() { + grpc_httpcli_header hdr = {"x-yz", "abc"}; + grpc_httpcli_request req; + gpr_slice slice; + + memset(&req, 0, sizeof(req)); + req.host = "example.com"; + req.path = "/index.html"; + req.hdr_count = 1; + req.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() { + grpc_httpcli_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.path = "/index.html"; + req.hdr_count = 2; + req.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/httpcli/httpcli_test.c b/test/core/httpcli/httpcli_test.c new file mode 100644 index 0000000000..5c0d87c427 --- /dev/null +++ b/test/core/httpcli/httpcli_test.c @@ -0,0 +1,101 @@ +/* + * + * Copyright 2014, 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/httpcli/httpcli.h" + +#include <string.h> + +#include <grpc/support/log.h> +#include "test/core/util/test_config.h" + +static gpr_event g_done; +static grpc_em g_em; + +static gpr_timespec n_seconds_time(int seconds) { + return gpr_time_add(gpr_now(), gpr_time_from_micros(seconds * 1000000)); +} + +static void on_finish(void *arg, const grpc_httpcli_response *response) { + GPR_ASSERT(arg == (void *)42); + GPR_ASSERT(response); + GPR_ASSERT(response->status == 200); + gpr_event_set(&g_done, (void *)1); +} + +static void test_get(int use_ssl) { + grpc_httpcli_request req; + + gpr_log(GPR_INFO, "running %s with use_ssl=%d.", __FUNCTION__, (int)use_ssl); + + gpr_event_init(&g_done); + memset(&req, 0, sizeof(req)); + req.host = "www.google.com"; + req.path = "/"; + req.use_ssl = use_ssl; + + grpc_httpcli_get(&req, n_seconds_time(15), &g_em, on_finish, (void *)42); + GPR_ASSERT(gpr_event_wait(&g_done, n_seconds_time(20))); +} + +/* +static void test_post(int use_ssl) { + grpc_httpcli_request req; + + gpr_log(GPR_INFO, "running %s with use_ssl=%d.", __FUNCTION__, (int)use_ssl); + + gpr_event_init(&g_done); + memset(&req, 0, sizeof(req)); + req.host = "requestb.in"; + req.path = "/1eamwr21"; + req.use_ssl = use_ssl; + + grpc_httpcli_post(&req, NULL, 0, n_seconds_time(15), &g_em, on_finish, + (void *)42); + GPR_ASSERT(gpr_event_wait(&g_done, n_seconds_time(20))); +} +*/ + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + grpc_em_init(&g_em); + + test_get(0); + test_get(1); + + /* test_post(0); */ + /* test_post(1); */ + + grpc_em_destroy(&g_em); + + return 0; +} diff --git a/test/core/httpcli/parser_test.c b/test/core/httpcli/parser_test.c new file mode 100644 index 0000000000..455f6a6393 --- /dev/null +++ b/test/core/httpcli/parser_test.c @@ -0,0 +1,155 @@ +/* + * + * Copyright 2014, 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/httpcli/parser.h" + +#include <stdarg.h> +#include <string.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> +#include <grpc/support/useful.h> +#include "test/core/util/slice_splitter.h" +#include "test/core/util/test_config.h" + +static void test_succeeds(grpc_slice_split_mode split_mode, char *response, + int expect_status, char *expect_body, ...) { + grpc_httpcli_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_httpcli_parser_init(&parser); + + for (i = 0; i < num_slices; i++) { + GPR_ASSERT(grpc_httpcli_parser_parse(&parser, slices[i])); + gpr_slice_unref(slices[i]); + } + GPR_ASSERT(grpc_httpcli_parser_eof(&parser)); + + GPR_ASSERT(expect_status == parser.r.status); + if (expect_body != NULL) { + GPR_ASSERT(strlen(expect_body) == parser.r.body_length); + GPR_ASSERT(0 == memcmp(expect_body, parser.r.body, parser.r.body_length)); + } else { + GPR_ASSERT(parser.r.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.r.hdr_count); + expect_value = va_arg(args, char *); + GPR_ASSERT(expect_value); + GPR_ASSERT(0 == strcmp(expect_key, parser.r.hdrs[i].key)); + GPR_ASSERT(0 == strcmp(expect_value, parser.r.hdrs[i].value)); + i++; + } + va_end(args); + GPR_ASSERT(i == parser.r.hdr_count); + + grpc_httpcli_parser_destroy(&parser); + gpr_free(slices); +} + +static void test_fails(grpc_slice_split_mode split_mode, char *response) { + grpc_httpcli_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_httpcli_parser_init(&parser); + + for (i = 0; i < num_slices; i++) { + if (!done && !grpc_httpcli_parser_parse(&parser, slices[i])) { + done = 1; + } + gpr_slice_unref(slices[i]); + } + if (!done && !grpc_httpcli_parser_eof(&parser)) { + done = 1; + } + GPR_ASSERT(done); + + grpc_httpcli_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}; + + 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_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"); + } + + return 0; +} |