[asterisk-commits] dlee: branch dlee/stasis-http r379234 - in /team/dlee/stasis-http: cog/ inclu...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 16 12:17:17 CST 2013


Author: dlee
Date: Wed Jan 16 12:17:13 2013
New Revision: 379234

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=379234
Log:
Basic invocation unit tests

Modified:
    team/dlee/stasis-http/cog/stasis_cog.py
    team/dlee/stasis-http/cog/stasis_http_resources.c.cog
    team/dlee/stasis-http/include/asterisk/stasis_http.h
    team/dlee/stasis-http/include/asterisk/stasis_http_bridges.h
    team/dlee/stasis-http/include/asterisk/stasis_http_channels.h
    team/dlee/stasis-http/include/asterisk/stasis_http_recordings.h
    team/dlee/stasis-http/res/res_stasis_http.c
    team/dlee/stasis-http/res/stasis_http_resources.c
    team/dlee/stasis-http/tests/test_stasis_http.c

Modified: team/dlee/stasis-http/cog/stasis_cog.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/cog/stasis_cog.py?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/cog/stasis_cog.py (original)
+++ team/dlee/stasis-http/cog/stasis_cog.py Wed Jan 16 12:17:13 2013
@@ -147,7 +147,7 @@
 	    cog.outl('%s {' % operation.get_args_name())
 	    for param in operation.get_all_parameters().itervalues():
 		if param.description:
-		    cog.outl('\t/*! %s%s */' % (param.description, required))
+		    cog.outl('\t/*! %s */' % (param.description))
 		cog.out('\t%s' % param.c_type)
 		if not param.c_type.endswith('*'):
 		    cog.out(' ')

Modified: team/dlee/stasis-http/cog/stasis_http_resources.c.cog
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/cog/stasis_http_resources.c.cog?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/cog/stasis_http_resources.c.cog (original)
+++ team/dlee/stasis-http/cog/stasis_http_resources.c.cog Wed Jan 16 12:17:13 2013
@@ -50,6 +50,6 @@
 ]]] */
 /*[[[end]]]*/
 
-struct stasis_rest_handlers *stasis_root_handler(void) {
+struct stasis_rest_handlers *stasis_default_root_handler(void) {
 	return &stasis;
 }

Modified: team/dlee/stasis-http/include/asterisk/stasis_http.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_http.h?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_http.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_http.h Wed Jan 16 12:17:13 2013
@@ -78,13 +78,41 @@
 };
 
 /*!
- * \brief Handler for the root RESTful resource.
+ * \brief Handler for the default root RESTful resource.
  */
-struct stasis_rest_handlers *stasis_root_handler(void);
+struct stasis_rest_handlers *stasis_default_root_handler(void);
 
 /*!
  * \internal
- * \brief Service function for API declarations. =
+ * \brief Set a new handler for the root RESTful resource.
+ *
+ * Only call from res_stasis_http and test_stasis_http. Only public to allow
+ * for unit testing.
+ *
+ * \param new_handler New root RESTful handler.
+ * \param return The old handler.
+ */
+struct stasis_rest_handlers *stasis_set_root_handler(struct stasis_rest_handlers *new_handler);
+
+/*!
+ * \internal
+ * \brief Stasis RESTful invocation handler.
+ *
+ * Only call from res_stasis_http and test_stasis_http. Only public to allow
+ * for unit testing.
+ *
+ * \param uri HTTP URI.
+ * \param method HTTP method.
+ * \param get_params HTTP \c GET parameters.
+ * \param headers HTTP headers.
+ * \param[out] response RESTful HTTP response.
+ */
+void stasis_http_invoke(const char *uri, enum ast_http_method method, struct ast_variable *get_params,
+			struct ast_variable *headers, struct stasis_http_response *response);
+
+/*!
+ * \internal
+ * \brief Service function for API declarations.
  *
  * Only call from res_stasis_http and test_stasis_http. Only public to allow
  * for unit testing.

Modified: team/dlee/stasis-http/include/asterisk/stasis_http_bridges.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_http_bridges.h?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_http_bridges.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_http_bridges.h Wed Jan 16 12:17:13 2013
@@ -35,21 +35,21 @@
 #ifndef _ASTERISK_STASIS_HTTP_BRIDGES_H
 #define _ASTERISK_STASIS_HTTP_BRIDGES_H
 struct ast_add_channel_to_bridge_args {
-	/*! Bridge's id (required) */
+	/*! Bridge's id */
 	const char *bridge_id;
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel;
 };
 struct ast_remove_channel_from_bridge_args {
-	/*! Bridge's id (required) */
+	/*! Bridge's id */
 	const char *bridge_id;
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel;
 };
 struct ast_record_bridge_args {
-	/*! Bridge's id (required) */
+	/*! Bridge's id */
 	const char *bridge_id;
-	/*! Recording's filename (required) */
+	/*! Recording's filename */
 	const char *name;
 	/*! Maximum duration of the recording, in seconds. 0 for no limit. */
 	int max_duration_seconds;
@@ -63,11 +63,11 @@
 	const char *terminate_on;
 };
 struct ast_get_bridge_args {
-	/*! Bridge's id (required) */
+	/*! Bridge's id */
 	const char *bridge_id;
 };
 struct ast_delete_bridge_args {
-	/*! Bridge's id (required) */
+	/*! Bridge's id */
 	const char *bridge_id;
 };
 struct ast_get_bridges_args {

Modified: team/dlee/stasis-http/include/asterisk/stasis_http_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_http_channels.h?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_http_channels.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_http_channels.h Wed Jan 16 12:17:13 2013
@@ -43,37 +43,37 @@
 	const char *context;
 };
 struct ast_continue_in_dialplan_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 };
 struct ast_reject_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 };
 struct ast_answer_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 };
 struct ast_hangup_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 };
 struct ast_mute_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 	/*! Direction in which to unmute audio */
 	const char *direction;
 };
 struct ast_unmute_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 	/*! Direction in which to unmute audio */
 	const char *direction;
 };
 struct ast_record_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
-	/*! Recording's filename (required) */
+	/*! Recording's filename */
 	const char *name;
 	/*! Maximum duration of the recording, in seconds. 0 for no limit */
 	int max_duration_seconds;
@@ -87,11 +87,11 @@
 	const char *terminate_on;
 };
 struct ast_get_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 };
 struct ast_delete_channel_args {
-	/*! Channel's id (required) */
+	/*! Channel's id */
 	const char *channel_id;
 };
 struct ast_get_channels_args {

Modified: team/dlee/stasis-http/include/asterisk/stasis_http_recordings.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_http_recordings.h?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_http_recordings.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_http_recordings.h Wed Jan 16 12:17:13 2013
@@ -35,31 +35,31 @@
 #ifndef _ASTERISK_STASIS_HTTP_RECORDINGS_H
 #define _ASTERISK_STASIS_HTTP_RECORDINGS_H
 struct ast_stop_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_pause_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_unpause_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_mute_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_unmute_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_get_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_delete_recording_args {
-	/*! Recording's id (required) */
+	/*! Recording's id */
 	const char *recording_id;
 };
 struct ast_get_recordings_args {

Modified: team/dlee/stasis-http/res/res_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/res_stasis_http.c?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/res/res_stasis_http.c (original)
+++ team/dlee/stasis-http/res/res_stasis_http.c Wed Jan 16 12:17:13 2013
@@ -46,6 +46,9 @@
 
 /*! WebSocket protocol for Stasis */
 static const char * const ws_protocol = "stasis";
+
+/*! Handler for root RESTful resource */
+static struct stasis_rest_handlers *root_handler;
 
 /*!
  * \internal
@@ -79,27 +82,20 @@
 	response->response_text = "OK";
 }
 
-/*!
- * \internal
- * \brief Stasis RESTful invocation handler.
- * \param uri HTTP URI.
- * \param method HTTP method.
- * \param get_params HTTP \c GET parameters.
- * \param headers HTTP headers.
- * \param[out] response RESTful HTTP response.
- */
-static void stasis_http_invoke(const char *uri,
-			       enum ast_http_method method,
-			       struct ast_variable *get_params,
-			       struct ast_variable *headers,
-			       struct stasis_http_response *response)
+void stasis_http_invoke(const char *uri,
+			enum ast_http_method method,
+			struct ast_variable *get_params,
+			struct ast_variable *headers,
+			struct stasis_http_response *response)
 {
 	RAII_VAR(char *, response_text, NULL, ast_free);
-	struct stasis_rest_handlers *handler = stasis_root_handler();
+	struct stasis_rest_handlers *handler = root_handler;
 	struct ast_variable *path_vars = NULL;
 	char *path = ast_strdupa(uri);
 	const char *path_segment;
 	stasis_rest_callback callback;
+
+	ast_assert(handler != NULL);
 
 	if (method < 0 || method >= AST_HTTP_MAX_METHOD) {
 		response_error(response, "Invalid method", 405, "Method Not Allowed");
@@ -151,13 +147,6 @@
 	}
 }
 
-/*!
- * \internal
- * \brief Service function for API declarations.
- * \param uri Requested URI.
- * \param headers HTTP headers.
- * \param[out] response RESTful HTTP response.
- */
 void stasis_http_get_api(const char *uri, struct ast_variable *headers, struct stasis_http_response *response) {
 	RAII_VAR(struct ast_str *, absolute_path_builder, ast_str_create(80), ast_free);
 	RAII_VAR(char *, absolute_api_dirname, NULL, free);
@@ -397,9 +386,18 @@
 	ast_websocket_unref(session);
 }
 
+struct stasis_rest_handlers *stasis_set_root_handler(struct stasis_rest_handlers *new_handler)
+{
+	struct stasis_rest_handlers *old_handler = root_handler;
+	root_handler = new_handler;
+	return old_handler;
+}
+
 static int load_module(void)
 {
 	int r = 0;
+
+	stasis_set_root_handler(stasis_default_root_handler());
 	r |= ast_http_uri_link(&http_uri);
 	r |= ast_websocket_add_protocol(ws_protocol, stasis_websocket_callback);
 	return r;

Modified: team/dlee/stasis-http/res/stasis_http_resources.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/stasis_http_resources.c?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/res/stasis_http_resources.c (original)
+++ team/dlee/stasis-http/res/stasis_http_resources.c Wed Jan 16 12:17:13 2013
@@ -708,6 +708,6 @@
 };
 
 
-struct stasis_rest_handlers *stasis_root_handler(void) {
+struct stasis_rest_handlers *stasis_default_root_handler(void) {
 	return &stasis;
 }

Modified: team/dlee/stasis-http/tests/test_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/tests/test_stasis_http.c?view=diff&rev=379234&r1=379233&r2=379234
==============================================================================
--- team/dlee/stasis-http/tests/test_stasis_http.c (original)
+++ team/dlee/stasis-http/tests/test_stasis_http.c Wed Jan 16 12:17:13 2013
@@ -38,44 +38,78 @@
 #include "asterisk/stasis_http.h"
 
 /*!@{*/
-#if 0
-/*!
- * These structs and functions define the restful API:
+
+/*!
+ * \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, DELETE)
- */
-
-struct test_received {
-	char *function;
-	struct ast_variable *get_params;
-	struct ast_variable *path_vars;
-	struct ast_variable *headers;
-};
-
-struct test_received test_received = {};
-
-#define HANDLER(name)						\
-	static void name(struct ast_variable *get_params,	\
-			 struct ast_variable *path_vars,	\
-			 struct ast_variable *headers,		\
-			 struct stasis_http_response *response)	\
-	{							\
-		test_received.function = #name;			\
-		test_received.get_params = get_params;		\
-		test_received.path_vars = path_vars;		\
-		test_received.headers = headers;		\
-	}
-
-
-HANDLER(bang_get)
-HANDLER(bang_post)
-HANDLER(bang_delete)
-HANDLER(bar_get)
-HANDLER(bar_post)
-HANDLER(bam_get)
-HANDLER(foo_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",
@@ -111,26 +145,54 @@
 	.num_children = 3,
 	.children = { &bar, &bam, &bang }
 };
-
-#endif // 0
-
+static struct stasis_rest_handlers test_root = {
+	.path_segment = "test-root",
+	.num_children = 1,
+	.children = { &foo }
+};
+/*!@}*/
+
+/*!
+ * \internal
+ * \c stasis_http_response constructor.
+ */
 static struct stasis_http_response *response_alloc(void)
 {
 	struct stasis_http_response *ret = ast_calloc(1, sizeof(struct stasis_http_response));
 	return ret;
 }
 
+/*!
+ * \internal
+ * \c stasis_http_response destructor.
+ */
 static void response_free(struct stasis_http_response *resp)
 {
 	ast_json_unref(resp->message);
 	ast_free(resp);
 }
 
-/*!@}*/
+/*!
+ * \ internal
+ * Setup test fixture for invocation tests.
+ */
+static void *setup_invocation_test(void) {
+	invocation_count = 0;
+	return stasis_set_root_handler(&test_root);
+}
+
+/*!
+ * \ internal
+ * Tear down test fixture for invocation tests.
+ */
+static void tear_down_invocation_test(void *old_handler) {
+	stasis_set_root_handler(old_handler);
+}
+
 
 AST_TEST_DEFINE(get_api)
 {
-	RAII_VAR(struct stasis_http_response *, response, response_alloc, response_free);
+	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;
@@ -148,10 +210,10 @@
 
 	headers = ast_variable_new("Host", "stasis.asterisk.org", __FILE__);
 	stasis_http_get_api("api/resources.json", headers, response);
-	ast_test_validate(test, 200 == response.response_code);
+	ast_test_validate(test, 200 == response->response_code);
 
 	/* basePath should be relative to the Host header */
-	basePathJson = ast_json_object_get(response.message, "basePath");
+	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));
@@ -161,7 +223,7 @@
 
 AST_TEST_DEFINE(get_api_nohost)
 {
-	RAII_VAR(struct stasis_http_response *, response, response_alloc, response_free);
+	RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free);
 	struct ast_variable *headers = NULL;
 	struct ast_json *basePathJson;
 
@@ -177,10 +239,10 @@
 	}
 
 	stasis_http_get_api("api/resources.json", headers, response);
-	ast_test_validate(test, 200 == response.response_code);
+	ast_test_validate(test, 200 == response->response_code);
 
 	/* basePath should be relative to the Host header */
-	basePathJson = ast_json_object_get(response.message, "basePath");
+	basePathJson = ast_json_object_get(response->message, "basePath");
 	ast_test_validate(test, NULL == basePathJson);
 
 	return AST_TEST_PASS;
@@ -188,7 +250,7 @@
 
 AST_TEST_DEFINE(get_api_notfound)
 {
-	RAII_VAR(struct stasis_http_response *, response, response_alloc, response_free);
+	RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free);
 	struct ast_variable *headers = NULL;
 
 	switch (cmd) {
@@ -203,14 +265,14 @@
 	}
 
 	stasis_http_get_api("api/i-am-not-a-resource.json", headers, response);
-	ast_test_validate(test, 404 == response.response_code);
+	ast_test_validate(test, 404 == response->response_code);
 
 	return AST_TEST_PASS;
 }
 
 AST_TEST_DEFINE(get_api_hackerz)
 {
-	RAII_VAR(struct stasis_http_response *, response, response_alloc, response_free);
+	RAII_VAR(struct stasis_http_response *, response, response_alloc(), response_free);
 	struct ast_variable *headers = NULL;
 
 	switch (cmd) {
@@ -225,7 +287,129 @@
 	}
 
 	stasis_http_get_api("api/../../../../sbin/asterisk", headers, response);
-	ast_test_validate(test, 404 == response.response_code);
+	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_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 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", "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 simple GET of an HTTP resource.";
+		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;
 }
@@ -236,6 +420,9 @@
 	AST_TEST_UNREGISTER(get_api_nohost);
 	AST_TEST_UNREGISTER(get_api_notfound);
 	AST_TEST_UNREGISTER(get_api_hackerz);
+	AST_TEST_UNREGISTER(invoke_get);
+	AST_TEST_UNREGISTER(invoke_post);
+	AST_TEST_UNREGISTER(invoke_bad_post);
 	return 0;
 }
 
@@ -245,6 +432,9 @@
 	AST_TEST_REGISTER(get_api_nohost);
 	AST_TEST_REGISTER(get_api_notfound);
 	AST_TEST_REGISTER(get_api_hackerz);
+	AST_TEST_REGISTER(invoke_get);
+	AST_TEST_REGISTER(invoke_post);
+	AST_TEST_REGISTER(invoke_bad_post);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 




More information about the asterisk-commits mailing list