diff options
author | David M. Lee <dlee@digium.com> | 2013-04-22 14:58:53 +0000 |
---|---|---|
committer | David M. Lee <dlee@digium.com> | 2013-04-22 14:58:53 +0000 |
commit | 1c21b8575bfd70b98b1102fd3dd09fc0bc335e14 (patch) | |
tree | 9a6ef6074e545ad2768bc1994e1a233fc1443729 /tests | |
parent | 1871017cc6bd2e2ce7c638eeb6813e982377a521 (diff) |
This patch adds a RESTful HTTP interface to Asterisk.
The API itself is documented using Swagger, a lightweight mechanism for
documenting RESTful API's using JSON. This allows us to use swagger-ui
to provide executable documentation for the API, generate client
bindings in different languages, and generate a lot of the boilerplate
code for implementing the RESTful bindings. The API docs live in the
rest-api/ directory.
The RESTful bindings are generated from the Swagger API docs using a set
of Mustache templates. The code generator is written in Python, and
uses Pystache. Pystache has no dependencies, and be installed easily
using pip. Code generation code lives in rest-api-templates/.
The generated code reduces a lot of boilerplate when it comes to
handling HTTP requests. It also helps us have greater consistency in the
REST API.
(closes issue ASTERISK-20891)
Review: https://reviewboard.asterisk.org/r/2376/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@386232 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_stasis.c | 117 | ||||
-rw-r--r-- | tests/test_stasis_http.c | 547 | ||||
-rw-r--r-- | tests/test_strings.c | 63 |
3 files changed, 709 insertions, 18 deletions
diff --git a/tests/test_stasis.c b/tests/test_stasis.c index 3a7d52c07..8f81378ab 100644 --- a/tests/test_stasis.c +++ b/tests/test_stasis.c @@ -645,7 +645,6 @@ AST_TEST_DEFINE(cache) RAII_VAR(struct stasis_message *, test_message1_clear, NULL, ao2_cleanup); int actual_len; struct stasis_cache_update *actual_update; - struct ao2_container *cache_dump; switch (cmd) { case TEST_INIT: @@ -681,12 +680,6 @@ AST_TEST_DEFINE(cache) actual_len = consumer_wait_for(consumer, 2); ast_test_validate(test, 2 == actual_len); - /* Dump the cache to ensure that it has the correct number of items in it */ - cache_dump = stasis_cache_dump(caching_topic, NULL); - ast_test_validate(test, 2 == ao2_container_count(cache_dump)); - ao2_ref(cache_dump, -1); - cache_dump = NULL; - /* Check for new snapshot messages */ ast_test_validate(test, stasis_cache_update_type() == stasis_message_type(consumer->messages_rxed[0])); actual_update = stasis_message_data(consumer->messages_rxed[0]); @@ -722,12 +715,6 @@ AST_TEST_DEFINE(cache) /* stasis_cache_get returned a ref, so unref test_message2_2 */ ao2_ref(test_message2_2, -1); - /* Dump the cache to ensure that it has the correct number of items in it */ - cache_dump = stasis_cache_dump(caching_topic, NULL); - ast_test_validate(test, 2 == ao2_container_count(cache_dump)); - ao2_ref(cache_dump, -1); - cache_dump = NULL; - /* Clear snapshot 1 */ test_message1_clear = stasis_cache_clear_create(cache_type, "1"); ast_test_validate(test, NULL != test_message1_clear); @@ -742,17 +729,109 @@ AST_TEST_DEFINE(cache) ast_test_validate(test, NULL == actual_update->new_snapshot); ast_test_validate(test, NULL == stasis_cache_get(caching_topic, cache_type, "1")); - /* Dump the cache to ensure that it has the correct number of items in it */ + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(cache_dump) +{ + RAII_VAR(struct stasis_message_type *, cache_type, NULL, ao2_cleanup); + RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup); + RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, stasis_caching_unsubscribe); + RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup); + RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe); + RAII_VAR(struct stasis_message *, test_message1_1, NULL, ao2_cleanup); + RAII_VAR(struct stasis_message *, test_message2_1, NULL, ao2_cleanup); + RAII_VAR(struct stasis_message *, test_message2_2, NULL, ao2_cleanup); + RAII_VAR(struct stasis_message *, test_message1_clear, NULL, ao2_cleanup); + RAII_VAR(struct ao2_container *, cache_dump, NULL, ao2_cleanup); + int actual_len; + struct ao2_iterator i; + void *obj; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = test_category; + info->summary = "Test passing messages through cache topic unscathed."; + info->description = "Test passing messages through cache topic unscathed."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + cache_type = stasis_message_type_create("Cacheable"); + ast_test_validate(test, NULL != cache_type); + topic = stasis_topic_create("SomeTopic"); + ast_test_validate(test, NULL != topic); + caching_topic = stasis_caching_topic_create(topic, cache_test_data_id); + ast_test_validate(test, NULL != caching_topic); + consumer = consumer_create(1); + ast_test_validate(test, NULL != consumer); + sub = stasis_subscribe(stasis_caching_get_topic(caching_topic), consumer_exec, consumer); + ast_test_validate(test, NULL != sub); + ao2_ref(consumer, +1); + + test_message1_1 = cache_test_message_create(cache_type, "1", "1"); + ast_test_validate(test, NULL != test_message1_1); + test_message2_1 = cache_test_message_create(cache_type, "2", "1"); + ast_test_validate(test, NULL != test_message2_1); + + /* Post a couple of snapshots */ + stasis_publish(topic, test_message1_1); + stasis_publish(topic, test_message2_1); + actual_len = consumer_wait_for(consumer, 2); + ast_test_validate(test, 2 == actual_len); + + /* Check the cache */ + cache_dump = stasis_cache_dump(caching_topic, NULL); + ast_test_validate(test, NULL != cache_dump); + ast_test_validate(test, 2 == ao2_container_count(cache_dump)); + i = ao2_iterator_init(cache_dump, 0); + while ((obj = ao2_iterator_next(&i))) { + RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup); + ast_test_validate(test, actual_cache_entry == test_message1_1 || actual_cache_entry == test_message2_1); + } + + /* Update snapshot 2 */ + test_message2_2 = cache_test_message_create(cache_type, "2", "2"); + ast_test_validate(test, NULL != test_message2_2); + stasis_publish(topic, test_message2_2); + + actual_len = consumer_wait_for(consumer, 3); + ast_test_validate(test, 3 == actual_len); + + /* Check the cache */ + cache_dump = stasis_cache_dump(caching_topic, NULL); + ast_test_validate(test, NULL != cache_dump); + ast_test_validate(test, 2 == ao2_container_count(cache_dump)); + i = ao2_iterator_init(cache_dump, 0); + while ((obj = ao2_iterator_next(&i))) { + RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup); + ast_test_validate(test, actual_cache_entry == test_message1_1 || actual_cache_entry == test_message2_2); + } + + /* Clear snapshot 1 */ + test_message1_clear = stasis_cache_clear_create(cache_type, "1"); + ast_test_validate(test, NULL != test_message1_clear); + stasis_publish(topic, test_message1_clear); + + actual_len = consumer_wait_for(consumer, 4); + ast_test_validate(test, 4 == actual_len); + + /* Check the cache */ cache_dump = stasis_cache_dump(caching_topic, NULL); + ast_test_validate(test, NULL != cache_dump); ast_test_validate(test, 1 == ao2_container_count(cache_dump)); - ao2_ref(cache_dump, -1); - cache_dump = NULL; + i = ao2_iterator_init(cache_dump, 0); + while ((obj = ao2_iterator_next(&i))) { + RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup); + ast_test_validate(test, actual_cache_entry == test_message2_2); + } /* Dump the cache to ensure that it has no subscription change items in it since those aren't cached */ + ao2_cleanup(cache_dump); cache_dump = stasis_cache_dump(caching_topic, stasis_subscription_change_type()); ast_test_validate(test, 0 == ao2_container_count(cache_dump)); - ao2_ref(cache_dump, -1); - cache_dump = NULL; return AST_TEST_PASS; } @@ -909,6 +988,7 @@ static int unload_module(void) AST_TEST_UNREGISTER(forward); AST_TEST_UNREGISTER(cache_passthrough); AST_TEST_UNREGISTER(cache); + AST_TEST_UNREGISTER(cache_dump); AST_TEST_UNREGISTER(route_conflicts); AST_TEST_UNREGISTER(router); AST_TEST_UNREGISTER(interleaving); @@ -925,6 +1005,7 @@ static int load_module(void) AST_TEST_REGISTER(forward); AST_TEST_REGISTER(cache_passthrough); AST_TEST_REGISTER(cache); + AST_TEST_REGISTER(cache_dump); AST_TEST_REGISTER(route_conflicts); AST_TEST_REGISTER(router); AST_TEST_REGISTER(interleaving); diff --git a/tests/test_stasis_http.c b/tests/test_stasis_http.c new file mode 100644 index 000000000..953c1023f --- /dev/null +++ b/tests/test_stasis_http.c @@ -0,0 +1,547 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2013, Digium, Inc. + * + * David M. Lee, II <dlee@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! + * \file \brief Test Stasis HTTP API. + * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim + * + * \ingroup tests + */ + +/*** MODULEINFO + <depend>TEST_FRAMEWORK</depend> + <depend>res_stasis_http</depend> + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/module.h" +#include "asterisk/test.h" +#include "asterisk/stasis_http.h" + +/*!@{*/ + +/*! + * \internal + * The following code defines a simple RESTful API for unit testing. The + * response encodes the inputs of the invocation. The invocation_count + * counter is also incremented. + * + * - /foo (GET) + * - /foo/bar (GET, POST) + * - /foo/{bam} (GET) + * - /foo/{bam}/bang (GET, POST, DE1LETE) + */ + +static int invocation_count; + +/*! + * \internal + * Shared code for all handlers + */ +static void handler(const char *name, + int response_code, + struct ast_variable *get_params, + struct ast_variable *path_vars, + struct ast_variable *headers, + struct stasis_http_response *response) +{ + struct ast_json *message = ast_json_pack("{s: s, s: {}, s: {}, s: {}}", + "name", name, + "get_params", + "path_vars", + "headers"); + struct ast_json *get_params_obj = ast_json_object_get(message, "get_params"); + struct ast_json *path_vars_obj = ast_json_object_get(message, "path_vars"); + struct ast_json *headers_obj = ast_json_object_get(message, "headers"); + + for (; get_params != NULL; get_params = get_params->next) { + ast_json_object_set(get_params_obj, get_params->name, ast_json_string_create(get_params->value)); + } + + for (; path_vars != NULL; path_vars = path_vars->next) { + ast_json_object_set(path_vars_obj, path_vars->name, ast_json_string_create(path_vars->value)); + } + + for (; headers != NULL; headers = headers->next) { + ast_json_object_set(headers_obj, headers->name, ast_json_string_create(headers->value)); + } + + ++invocation_count; + response->response_code = response_code; + response->message = message; +} + +/*! + * \internal + * Macro to reduce the handler definition boiler-plate. + */ +#define HANDLER(name, response_code) \ + static void name(struct ast_variable *get_params, \ + struct ast_variable *path_vars, \ + struct ast_variable *headers, \ + struct stasis_http_response *response) \ + { \ + handler(#name, response_code, get_params, path_vars, headers, response); \ + } + +HANDLER(bang_get, 200) +HANDLER(bang_post, 200) +HANDLER(bang_delete, 204) +HANDLER(bar_get, 200) +HANDLER(bar_post, 200) +HANDLER(bam_get, 200) +HANDLER(foo_get, 200) + +static struct stasis_rest_handlers bang = { + .path_segment = "bang", + .callbacks = { + [AST_HTTP_GET] = bang_get, + [AST_HTTP_POST] = bang_post, + [AST_HTTP_DELETE] = bang_delete, + }, + .num_children = 0 +}; +static struct stasis_rest_handlers bar = { + .path_segment = "bar", + .callbacks = { + [AST_HTTP_GET] = bar_get, + [AST_HTTP_POST] = bar_post, + }, + .num_children = 0 +}; +static struct stasis_rest_handlers bam = { + .path_segment = "bam", + .is_wildcard = 1, + .callbacks = { + [AST_HTTP_GET] = bam_get, + }, + .num_children = 1, + .children = { &bang } +}; +static struct stasis_rest_handlers test_root = { + .path_segment = "foo", + .callbacks = { + [AST_HTTP_GET] = foo_get, + }, + .num_children = 3, + .children = { &bar, &bam, &bang } +}; +/*!@}*/ + +/*! + * \internal + * \c stasis_http_response constructor. + */ +static struct stasis_http_response *response_alloc(void) +{ + struct stasis_http_response *resp = ast_calloc(1, sizeof(struct stasis_http_response)); + resp->headers = ast_str_create(24); + return resp; +} + +/*! + * \internal + * \c stasis_http_response destructor. + */ +static void response_free(struct stasis_http_response *resp) +{ + ast_free(resp->headers); + ast_json_unref(resp->message); + ast_free(resp); +} + +/*! + * \ internal + * Setup test fixture for invocation tests. + */ +static void *setup_invocation_test(void) { + int r; + invocation_count = 0; + r = stasis_http_add_handler(&test_root); + ast_assert(r == 0); + return NULL; +} + +/*! + * \ internal + * Tear down test fixture for invocation tests. + */ +static void tear_down_invocation_test(void *ignore) { + stasis_http_remove_handler(&test_root); +} + + +AST_TEST_DEFINE(get_docs) +{ + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + RAII_VAR(struct ast_variable *, headers, NULL, ast_variables_destroy); + struct ast_json *basePathJson; + const char *basePath; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test simple API get."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + headers = ast_variable_new("Host", "stasis.asterisk.org", __FILE__); + stasis_http_get_docs("resources.json", headers, response); + ast_test_validate(test, 200 == response->response_code); + + /* basePath should be relative to the Host header */ + basePathJson = ast_json_object_get(response->message, "basePath"); + ast_test_validate(test, NULL != basePathJson); + basePath = ast_json_string_get(basePathJson); + ast_test_validate(test, 0 == strcmp("http://stasis.asterisk.org/stasis", basePath)); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(get_docs_nohost) +{ + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + struct ast_variable *headers = NULL; + struct ast_json *basePathJson; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test API get without a Host header"; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + stasis_http_get_docs("resources.json", headers, response); + ast_test_validate(test, 200 == response->response_code); + + /* basePath should be relative to the Host header */ + basePathJson = ast_json_object_get(response->message, "basePath"); + ast_test_validate(test, NULL == basePathJson); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(get_docs_notfound) +{ + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test API get for invalid resource"; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + stasis_http_get_docs("i-am-not-a-resource.json", headers, response); + ast_test_validate(test, 404 == response->response_code); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(get_docs_hackerz) +{ + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test API get for a file outside the rest-api path"; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + stasis_http_get_docs("../../../../sbin/asterisk", headers, response); + ast_test_validate(test, 404 == response->response_code); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(invoke_get) +{ + RAII_VAR(void *, fixture, setup_invocation_test(), tear_down_invocation_test); + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); + struct ast_variable *get_params = NULL; + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test simple GET of an HTTP resource."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + get_params = ast_variable_new("get1", "get-one", __FILE__); + ast_assert(get_params != NULL); + get_params->next = ast_variable_new("get2", "get-two", __FILE__); + ast_assert(get_params->next != NULL); + + headers = ast_variable_new("head1", "head-one", __FILE__); + ast_assert(headers != NULL); + headers->next = ast_variable_new("head2", "head-two", __FILE__); + ast_assert(headers->next != NULL); + + expected = ast_json_pack("{s: s, s: {s: s, s: s}, s: {s: s, s: s}, s: {}}", + "name", "foo_get", + "get_params", + "get1", "get-one", + "get2", "get-two", + "headers", + "head1", "head-one", + "head2", "head-two", + "path_vars"); + + stasis_http_invoke("foo", AST_HTTP_GET, get_params, headers, response); + + ast_test_validate(test, 1 == invocation_count); + ast_test_validate(test, 200 == response->response_code); + ast_test_validate(test, ast_json_equal(expected, response->message)); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(invoke_wildcard) +{ + RAII_VAR(void *, fixture, setup_invocation_test(), tear_down_invocation_test); + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); + struct ast_variable *get_params = NULL; + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test GET of a wildcard resource."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + expected = ast_json_pack("{s: s, s: {}, s: {}, s: {s: s}}", + "name", "bam_get", + "get_params", + "headers", + "path_vars", + "bam", "foshizzle"); + + stasis_http_invoke("foo/foshizzle", AST_HTTP_GET, get_params, headers, response); + + ast_test_validate(test, 1 == invocation_count); + ast_test_validate(test, 200 == response->response_code); + ast_test_validate(test, ast_json_equal(expected, response->message)); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(invoke_delete) +{ + RAII_VAR(void *, fixture, setup_invocation_test(), tear_down_invocation_test); + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); + struct ast_variable *get_params = NULL; + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test DELETE of an HTTP resource."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + expected = ast_json_pack("{s: s, s: {}, s: {}, s: {s: s}}", + "name", "bang_delete", + "get_params", + "headers", + "path_vars", + "bam", "foshizzle"); + + stasis_http_invoke("foo/foshizzle/bang", AST_HTTP_DELETE, get_params, headers, response); + + ast_test_validate(test, 1 == invocation_count); + ast_test_validate(test, 204 == response->response_code); + ast_test_validate(test, ast_json_equal(expected, response->message)); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(invoke_post) +{ + RAII_VAR(void *, fixture, setup_invocation_test(), tear_down_invocation_test); + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); + struct ast_variable *get_params = NULL; + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test POST of an HTTP resource."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + get_params = ast_variable_new("get1", "get-one", __FILE__); + ast_assert(get_params != NULL); + get_params->next = ast_variable_new("get2", "get-two", __FILE__); + ast_assert(get_params->next != NULL); + + headers = ast_variable_new("head1", "head-one", __FILE__); + ast_assert(headers != NULL); + headers->next = ast_variable_new("head2", "head-two", __FILE__); + ast_assert(headers->next != NULL); + + expected = ast_json_pack("{s: s, s: {s: s, s: s}, s: {s: s, s: s}, s: {}}", + "name", "bar_post", + "get_params", + "get1", "get-one", + "get2", "get-two", + "headers", + "head1", "head-one", + "head2", "head-two", + "path_vars"); + + stasis_http_invoke("foo/bar", AST_HTTP_POST, get_params, headers, response); + + ast_test_validate(test, 1 == invocation_count); + ast_test_validate(test, 200 == response->response_code); + ast_test_validate(test, ast_json_equal(expected, response->message)); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(invoke_bad_post) +{ + RAII_VAR(void *, fixture, setup_invocation_test(), tear_down_invocation_test); + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + struct ast_variable *get_params = NULL; + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test POST on a resource that doesn't support it."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + stasis_http_invoke("foo", AST_HTTP_POST, get_params, headers, response); + + ast_test_validate(test, 0 == invocation_count); + ast_test_validate(test, 405 == response->response_code); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(invoke_not_found) +{ + RAII_VAR(void *, fixture, setup_invocation_test(), tear_down_invocation_test); + RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free); + struct ast_variable *get_params = NULL; + struct ast_variable *headers = NULL; + + switch (cmd) { + case TEST_INIT: + info->name = __func__; + info->category = "/stasis/http/"; + info->summary = "Test GET on a resource that does not exist."; + info->description = "Test Stasis HTTP binding logic."; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + stasis_http_invoke("foo/fizzle/i-am-not-a-resource", AST_HTTP_GET, get_params, headers, response); + + ast_test_validate(test, 0 == invocation_count); + ast_test_validate(test, 404 == response->response_code); + + return AST_TEST_PASS; +} + +static int unload_module(void) +{ + AST_TEST_UNREGISTER(get_docs); + AST_TEST_UNREGISTER(get_docs_nohost); + AST_TEST_UNREGISTER(get_docs_notfound); + AST_TEST_UNREGISTER(get_docs_hackerz); + AST_TEST_UNREGISTER(invoke_get); + AST_TEST_UNREGISTER(invoke_wildcard); + AST_TEST_UNREGISTER(invoke_delete); + AST_TEST_UNREGISTER(invoke_post); + AST_TEST_UNREGISTER(invoke_bad_post); + AST_TEST_UNREGISTER(invoke_not_found); + return 0; +} + +static int load_module(void) +{ + AST_TEST_REGISTER(get_docs); + AST_TEST_REGISTER(get_docs_nohost); + AST_TEST_REGISTER(get_docs_notfound); + AST_TEST_REGISTER(get_docs_hackerz); + AST_TEST_REGISTER(invoke_get); + AST_TEST_REGISTER(invoke_wildcard); + AST_TEST_REGISTER(invoke_delete); + AST_TEST_REGISTER(invoke_post); + AST_TEST_REGISTER(invoke_bad_post); + AST_TEST_REGISTER(invoke_not_found); + return AST_MODULE_LOAD_SUCCESS; +} + +AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Stasis HTTP testing", + .load = load_module, + .unload = unload_module, + .nonoptreq = "res_stasis_http", + ); diff --git a/tests/test_strings.c b/tests/test_strings.c index 2a18c1f3b..5e5a17d27 100644 --- a/tests/test_strings.c +++ b/tests/test_strings.c @@ -251,15 +251,78 @@ cleanup: return res; } +AST_TEST_DEFINE(begins_with_test) +{ + switch (cmd) { + case TEST_INIT: + info->name = "begins_with"; + info->category = "/main/strings/"; + info->summary = "Test ast_begins_with"; + info->description = "Test ast_begins_with"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + // prefixes + ast_test_validate(test, 1 == ast_begins_with("foobar", "foobar")); + ast_test_validate(test, 1 == ast_begins_with("foobar", "foo")); + ast_test_validate(test, 1 == ast_begins_with("foobar", "")); + ast_test_validate(test, 1 == ast_begins_with("", "")); + + // not prefixes + ast_test_validate(test, 0 == ast_begins_with("foobar", "bang")); + ast_test_validate(test, 0 == ast_begins_with("foobar", "foobat")); + ast_test_validate(test, 0 == ast_begins_with("boo", "boom")); + ast_test_validate(test, 0 == ast_begins_with("", "blitz")); + + // nothing failed; we're all good! + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(ends_with_test) +{ + switch (cmd) { + case TEST_INIT: + info->name = "ends_with"; + info->category = "/main/strings/"; + info->summary = "Test ast_ends_with"; + info->description = "Test ast_ends_with"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + // prefixes + ast_test_validate(test, 1 == ast_ends_with("foobar", "foobar")); + ast_test_validate(test, 1 == ast_ends_with("foobar", "bar")); + ast_test_validate(test, 1 == ast_ends_with("foobar", "")); + ast_test_validate(test, 1 == ast_ends_with("", "")); + + // not suffixes + ast_test_validate(test, 0 == ast_ends_with("bar", "bbar")); + ast_test_validate(test, 0 == ast_ends_with("foobar", "bang")); + ast_test_validate(test, 0 == ast_ends_with("foobar", "foobat")); + ast_test_validate(test, 0 == ast_ends_with("boo", "boom")); + ast_test_validate(test, 0 == ast_ends_with("", "blitz")); + + // nothing failed; we're all good! + return AST_TEST_PASS; +} + static int unload_module(void) { AST_TEST_UNREGISTER(str_test); + AST_TEST_UNREGISTER(begins_with_test); + AST_TEST_UNREGISTER(ends_with_test); return 0; } static int load_module(void) { AST_TEST_REGISTER(str_test); + AST_TEST_REGISTER(begins_with_test); + AST_TEST_REGISTER(ends_with_test); return AST_MODULE_LOAD_SUCCESS; } |