Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
test_ari.c File Reference

Test ARI API.

Author
David M. Lee, II <[email protected]> 
.
More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/ari.h"
Include dependency graph for test_ari.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (get_docs)
 
 AST_TEST_DEFINE (get_docs_nohost)
 
 AST_TEST_DEFINE (get_docs_notfound)
 
 AST_TEST_DEFINE (get_docs_hackerz)
 
 AST_TEST_DEFINE (invoke_get)
 
 AST_TEST_DEFINE (invoke_wildcard)
 
 AST_TEST_DEFINE (invoke_delete)
 
 AST_TEST_DEFINE (invoke_post)
 
 AST_TEST_DEFINE (invoke_bad_post)
 
 AST_TEST_DEFINE (invoke_not_found)
 
static int load_module (void)
 
static struct ast_ari_responseresponse_alloc (void)
 
static void response_free (struct ast_ari_response *resp)
 
static void * setup_invocation_test (void)
 
static void tear_down_invocation_test (void *ignore)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "ARI testing" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
#define HANDLER(name, response_code)
 
static int invocation_count
 
static struct stasis_rest_handlers bang
 
static struct stasis_rest_handlers bar
 
static struct stasis_rest_handlers bam
 
static struct stasis_rest_handlers test_root
 
static void handler (const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
 

Detailed Description

Test ARI API.

Author
David M. Lee, II <[email protected]> 
.

Definition in file test_ari.c.

Macro Definition Documentation

◆ HANDLER

#define HANDLER (   name,
  response_code 
)

Definition at line 97 of file test_ari.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 578 of file test_ari.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 578 of file test_ari.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 578 of file test_ari.c.

◆ AST_TEST_DEFINE() [1/10]

AST_TEST_DEFINE ( get_docs  )

Definition at line 201 of file test_ari.c.

References ast_ari_get_docs(), ast_json_object_get(), ast_json_string_get(), AST_TEST_NOT_RUN, AST_TEST_PASS, ast_variable_new, ast_variables_destroy(), ast_ari_response::headers, sip_to_pjsip::info(), NULL, RAII_VAR, response_alloc(), response_free(), TEST_EXECUTE, and TEST_INIT.

202 {
203  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
204  RAII_VAR(struct ast_variable *, headers, NULL, ast_variables_destroy);
205  struct ast_json *basePathJson;
206  const char *basePath;
207 
208  switch (cmd) {
209  case TEST_INIT:
210  info->name = __func__;
211  info->category = "/res/ari/";
212  info->summary = "Test simple API get.";
213  info->description = "Test ARI binding logic.";
214  return AST_TEST_NOT_RUN;
215  case TEST_EXECUTE:
216  break;
217  }
218 
219  response = response_alloc();
220  headers = ast_variable_new("Host", "stasis.asterisk.org", __FILE__);
221  ast_ari_get_docs("resources.json", "", headers, response);
222  ast_test_validate(test, 200 == response->response_code);
223 
224  /* basePath should be relative to the Host header */
225  basePathJson = ast_json_object_get(response->message, "basePath");
226  ast_test_validate(test, NULL != basePathJson);
227  basePath = ast_json_string_get(basePathJson);
228  ast_test_validate(test, 0 == strcmp("http://stasis.asterisk.org/ari", basePath));
229 
230  return AST_TEST_PASS;
231 }
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, struct ast_ari_response *response)
Definition: res_ari.c:598
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
#define ast_variable_new(name, value, filename)
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [2/10]

AST_TEST_DEFINE ( get_docs_nohost  )

Definition at line 233 of file test_ari.c.

References ast_ari_get_docs(), ast_json_object_get(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), NULL, RAII_VAR, response_alloc(), response_free(), TEST_EXECUTE, and TEST_INIT.

234 {
235  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
236  struct ast_variable *headers = NULL;
237  struct ast_json *basePathJson;
238 
239  switch (cmd) {
240  case TEST_INIT:
241  info->name = __func__;
242  info->category = "/res/ari/";
243  info->summary = "Test API get without a Host header";
244  info->description = "Test ARI binding logic.";
245  return AST_TEST_NOT_RUN;
246  case TEST_EXECUTE:
247  break;
248  }
249 
250  response = response_alloc();
251  ast_ari_get_docs("resources.json", "", headers, response);
252  ast_test_validate(test, 200 == response->response_code);
253 
254  /* basePath should be relative to the Host header */
255  basePathJson = ast_json_object_get(response->message, "basePath");
256  ast_test_validate(test, NULL == basePathJson);
257 
258  return AST_TEST_PASS;
259 }
void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, struct ast_ari_response *response)
Definition: res_ari.c:598
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [3/10]

AST_TEST_DEFINE ( get_docs_notfound  )

Definition at line 261 of file test_ari.c.

References ast_ari_get_docs(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), NULL, RAII_VAR, response_alloc(), response_free(), TEST_EXECUTE, and TEST_INIT.

262 {
263  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
264  struct ast_variable *headers = NULL;
265 
266  switch (cmd) {
267  case TEST_INIT:
268  info->name = __func__;
269  info->category = "/res/ari/";
270  info->summary = "Test API get for invalid resource";
271  info->description = "Test ARI binding logic.";
272  return AST_TEST_NOT_RUN;
273  case TEST_EXECUTE:
274  break;
275  }
276 
277  response = response_alloc();
278  ast_ari_get_docs("i-am-not-a-resource.json", "", headers, response);
279  ast_test_validate(test, 404 == response->response_code);
280 
281  return AST_TEST_PASS;
282 }
void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, struct ast_ari_response *response)
Definition: res_ari.c:598
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)

◆ AST_TEST_DEFINE() [4/10]

AST_TEST_DEFINE ( get_docs_hackerz  )

Definition at line 284 of file test_ari.c.

References ast_ari_get_docs(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), NULL, RAII_VAR, response_alloc(), response_free(), TEST_EXECUTE, and TEST_INIT.

285 {
286  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
287  struct ast_variable *headers = NULL;
288 
289  switch (cmd) {
290  case TEST_INIT:
291  info->name = __func__;
292  info->category = "/res/ari/";
293  info->summary = "Test API get for a file outside the rest-api path";
294  info->description = "Test ARI binding logic.";
295  return AST_TEST_NOT_RUN;
296  case TEST_EXECUTE:
297  break;
298  }
299 
300  response = response_alloc();
301  ast_ari_get_docs("../../../../sbin/asterisk", "", headers, response);
302  ast_test_validate(test, 404 == response->response_code);
303 
304  return AST_TEST_PASS;
305 }
void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, struct ast_ari_response *response)
Definition: res_ari.c:598
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)

◆ AST_TEST_DEFINE() [5/10]

AST_TEST_DEFINE ( invoke_get  )

Definition at line 307 of file test_ari.c.

References ast_ari_invoke(), ast_assert, AST_HTTP_GET, ast_json_equal(), ast_json_null(), ast_json_pack(), ast_json_unref(), AST_TEST_NOT_RUN, AST_TEST_PASS, ast_variable_new, sip_to_pjsip::info(), invocation_count, ast_variable::next, NULL, RAII_VAR, response_alloc(), response_free(), setup_invocation_test(), tear_down_invocation_test(), TEST_EXECUTE, and TEST_INIT.

308 {
309  RAII_VAR(void *, fixture, NULL, tear_down_invocation_test);
310  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
311  RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
312  struct ast_variable *get_params = NULL;
313  struct ast_variable *headers = NULL;
314 
315  switch (cmd) {
316  case TEST_INIT:
317  info->name = __func__;
318  info->category = "/res/ari/";
319  info->summary = "Test simple GET of an HTTP resource.";
320  info->description = "Test ARI binding logic.";
321  return AST_TEST_NOT_RUN;
322  case TEST_EXECUTE:
323  break;
324  }
325 
326  fixture = setup_invocation_test();
327  response = response_alloc();
328  get_params = ast_variable_new("get1", "get-one", __FILE__);
329  ast_assert(get_params != NULL);
330  get_params->next = ast_variable_new("get2", "get-two", __FILE__);
331  ast_assert(get_params->next != NULL);
332 
333  headers = ast_variable_new("head1", "head-one", __FILE__);
334  ast_assert(headers != NULL);
335  headers->next = ast_variable_new("head2", "head-two", __FILE__);
336  ast_assert(headers->next != NULL);
337 
338  expected = ast_json_pack("{s: s, s: {s: s, s: s}, s: {s: s, s: s}, s: {}}",
339  "name", "foo_get",
340  "get_params",
341  "get1", "get-one",
342  "get2", "get-two",
343  "headers",
344  "head1", "head-one",
345  "head2", "head-two",
346  "path_vars");
347 
348  ast_ari_invoke(NULL, "foo", AST_HTTP_GET, get_params, headers,
349  ast_json_null(), response);
350 
351  ast_test_validate(test, 1 == invocation_count);
352  ast_test_validate(test, 200 == response->response_code);
353  ast_test_validate(test, ast_json_equal(expected, response->message));
354 
355  return AST_TEST_PASS;
356 }
struct ast_variable * next
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
static void tear_down_invocation_test(void *ignore)
Definition: test_ari.c:193
void ast_ari_invoke(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: res_ari.c:491
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static void * setup_invocation_test(void)
Definition: test_ari.c:181
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_variable_new(name, value, filename)
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
static int invocation_count
Definition: test_ari.c:53
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [6/10]

AST_TEST_DEFINE ( invoke_wildcard  )

Definition at line 358 of file test_ari.c.

References ast_ari_invoke(), AST_HTTP_GET, ast_json_equal(), ast_json_null(), ast_json_pack(), ast_json_unref(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), invocation_count, NULL, RAII_VAR, response_alloc(), response_free(), setup_invocation_test(), tear_down_invocation_test(), TEST_EXECUTE, and TEST_INIT.

359 {
360  RAII_VAR(void *, fixture, NULL, tear_down_invocation_test);
361  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
362  RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
363  struct ast_variable *get_params = NULL;
364  struct ast_variable *headers = NULL;
365 
366  switch (cmd) {
367  case TEST_INIT:
368  info->name = __func__;
369  info->category = "/res/ari/";
370  info->summary = "Test GET of a wildcard resource.";
371  info->description = "Test ARI binding logic.";
372  return AST_TEST_NOT_RUN;
373  case TEST_EXECUTE:
374  break;
375  }
376 
377  fixture = setup_invocation_test();
378  response = response_alloc();
379  expected = ast_json_pack("{s: s, s: {}, s: {}, s: {s: s}}",
380  "name", "bam_get",
381  "get_params",
382  "headers",
383  "path_vars",
384  "bam", "foshizzle");
385 
386  ast_ari_invoke(NULL, "foo/foshizzle", AST_HTTP_GET, get_params, headers,
387  ast_json_null(), response);
388 
389  ast_test_validate(test, 1 == invocation_count);
390  ast_test_validate(test, 200 == response->response_code);
391  ast_test_validate(test, ast_json_equal(expected, response->message));
392 
393  return AST_TEST_PASS;
394 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
static void tear_down_invocation_test(void *ignore)
Definition: test_ari.c:193
void ast_ari_invoke(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: res_ari.c:491
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static void * setup_invocation_test(void)
Definition: test_ari.c:181
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
static int invocation_count
Definition: test_ari.c:53
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [7/10]

AST_TEST_DEFINE ( invoke_delete  )

Definition at line 396 of file test_ari.c.

References ast_ari_invoke(), AST_HTTP_DELETE, ast_json_equal(), ast_json_null(), ast_json_pack(), ast_json_unref(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), invocation_count, NULL, RAII_VAR, response_alloc(), response_free(), setup_invocation_test(), tear_down_invocation_test(), TEST_EXECUTE, and TEST_INIT.

397 {
398  RAII_VAR(void *, fixture, NULL, tear_down_invocation_test);
399  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
400  RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
401  struct ast_variable *get_params = NULL;
402  struct ast_variable *headers = NULL;
403 
404  switch (cmd) {
405  case TEST_INIT:
406  info->name = __func__;
407  info->category = "/res/ari/";
408  info->summary = "Test DELETE of an HTTP resource.";
409  info->description = "Test ARI binding logic.";
410  return AST_TEST_NOT_RUN;
411  case TEST_EXECUTE:
412  break;
413  }
414 
415  fixture = setup_invocation_test();
416  response = response_alloc();
417  expected = ast_json_pack("{s: s, s: {}, s: {}, s: {s: s}}",
418  "name", "bang_delete",
419  "get_params",
420  "headers",
421  "path_vars",
422  "bam", "foshizzle");
423 
424  ast_ari_invoke(NULL, "foo/foshizzle/bang", AST_HTTP_DELETE, get_params, headers,
425  ast_json_null(), response);
426 
427  ast_test_validate(test, 1 == invocation_count);
428  ast_test_validate(test, 204 == response->response_code);
429  ast_test_validate(test, ast_json_equal(expected, response->message));
430 
431  return AST_TEST_PASS;
432 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
static void tear_down_invocation_test(void *ignore)
Definition: test_ari.c:193
void ast_ari_invoke(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: res_ari.c:491
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static void * setup_invocation_test(void)
Definition: test_ari.c:181
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
static int invocation_count
Definition: test_ari.c:53
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [8/10]

AST_TEST_DEFINE ( invoke_post  )

Definition at line 434 of file test_ari.c.

References ast_ari_invoke(), ast_assert, AST_HTTP_POST, ast_json_equal(), ast_json_null(), ast_json_pack(), ast_json_unref(), AST_TEST_NOT_RUN, AST_TEST_PASS, ast_variable_new, sip_to_pjsip::info(), invocation_count, ast_variable::next, NULL, RAII_VAR, response_alloc(), response_free(), setup_invocation_test(), tear_down_invocation_test(), TEST_EXECUTE, and TEST_INIT.

435 {
436  RAII_VAR(void *, fixture, NULL, tear_down_invocation_test);
437  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
438  RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
439  struct ast_variable *get_params = NULL;
440  struct ast_variable *headers = NULL;
441 
442  switch (cmd) {
443  case TEST_INIT:
444  info->name = __func__;
445  info->category = "/res/ari/";
446  info->summary = "Test POST of an HTTP resource.";
447  info->description = "Test ARI binding logic.";
448  return AST_TEST_NOT_RUN;
449  case TEST_EXECUTE:
450  break;
451  }
452 
453  fixture = setup_invocation_test();
454  response = response_alloc();
455  get_params = ast_variable_new("get1", "get-one", __FILE__);
456  ast_assert(get_params != NULL);
457  get_params->next = ast_variable_new("get2", "get-two", __FILE__);
458  ast_assert(get_params->next != NULL);
459 
460  headers = ast_variable_new("head1", "head-one", __FILE__);
461  ast_assert(headers != NULL);
462  headers->next = ast_variable_new("head2", "head-two", __FILE__);
463  ast_assert(headers->next != NULL);
464 
465  expected = ast_json_pack("{s: s, s: {s: s, s: s}, s: {s: s, s: s}, s: {}}",
466  "name", "bar_post",
467  "get_params",
468  "get1", "get-one",
469  "get2", "get-two",
470  "headers",
471  "head1", "head-one",
472  "head2", "head-two",
473  "path_vars");
474 
475  ast_ari_invoke(NULL, "foo/bar", AST_HTTP_POST, get_params, headers,
476  ast_json_null(), response);
477 
478  ast_test_validate(test, 1 == invocation_count);
479  ast_test_validate(test, 200 == response->response_code);
480  ast_test_validate(test, ast_json_equal(expected, response->message));
481 
482  return AST_TEST_PASS;
483 }
struct ast_variable * next
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
static void tear_down_invocation_test(void *ignore)
Definition: test_ari.c:193
void ast_ari_invoke(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: res_ari.c:491
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static void * setup_invocation_test(void)
Definition: test_ari.c:181
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_variable_new(name, value, filename)
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
static int invocation_count
Definition: test_ari.c:53
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [9/10]

AST_TEST_DEFINE ( invoke_bad_post  )

Definition at line 485 of file test_ari.c.

References ast_ari_invoke(), AST_HTTP_POST, ast_json_null(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), invocation_count, NULL, RAII_VAR, response_alloc(), response_free(), setup_invocation_test(), tear_down_invocation_test(), TEST_EXECUTE, and TEST_INIT.

486 {
487  RAII_VAR(void *, fixture, NULL, tear_down_invocation_test);
488  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
489  struct ast_variable *get_params = NULL;
490  struct ast_variable *headers = NULL;
491 
492  switch (cmd) {
493  case TEST_INIT:
494  info->name = __func__;
495  info->category = "/res/ari/";
496  info->summary = "Test POST on a resource that doesn't support it.";
497  info->description = "Test ARI binding logic.";
498  return AST_TEST_NOT_RUN;
499  case TEST_EXECUTE:
500  break;
501  }
502 
503  fixture = setup_invocation_test();
504  response = response_alloc();
505  ast_ari_invoke(NULL, "foo", AST_HTTP_POST, get_params, headers,
506  ast_json_null(), response);
507 
508  ast_test_validate(test, 0 == invocation_count);
509  ast_test_validate(test, 405 == response->response_code);
510 
511  return AST_TEST_PASS;
512 }
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
static void tear_down_invocation_test(void *ignore)
Definition: test_ari.c:193
void ast_ari_invoke(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: res_ari.c:491
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static void * setup_invocation_test(void)
Definition: test_ari.c:181
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
static int invocation_count
Definition: test_ari.c:53

◆ AST_TEST_DEFINE() [10/10]

AST_TEST_DEFINE ( invoke_not_found  )

Definition at line 514 of file test_ari.c.

References ast_ari_invoke(), AST_HTTP_GET, ast_json_null(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), invocation_count, NULL, RAII_VAR, response_alloc(), response_free(), setup_invocation_test(), tear_down_invocation_test(), TEST_EXECUTE, and TEST_INIT.

515 {
516  RAII_VAR(void *, fixture, NULL, tear_down_invocation_test);
517  RAII_VAR(struct ast_ari_response *, response, NULL, response_free);
518  struct ast_variable *get_params = NULL;
519  struct ast_variable *headers = NULL;
520 
521  switch (cmd) {
522  case TEST_INIT:
523  info->name = __func__;
524  info->category = "/res/ari/";
525  info->summary = "Test GET on a resource that does not exist.";
526  info->description = "Test ARI binding logic.";
527  return AST_TEST_NOT_RUN;
528  case TEST_EXECUTE:
529  break;
530  }
531 
532  fixture = setup_invocation_test();
533  response = response_alloc();
534  ast_ari_invoke(NULL, "foo/fizzle/i-am-not-a-resource", AST_HTTP_GET, get_params, headers,
535  ast_json_null(), response);
536 
537  ast_test_validate(test, 0 == invocation_count);
538  ast_test_validate(test, 404 == response->response_code);
539 
540  return AST_TEST_PASS;
541 }
static struct ast_ari_response * response_alloc(void)
Definition: test_ari.c:156
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
static void tear_down_invocation_test(void *ignore)
Definition: test_ari.c:193
void ast_ari_invoke(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: res_ari.c:491
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static void * setup_invocation_test(void)
Definition: test_ari.c:181
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static void response_free(struct ast_ari_response *resp)
Definition: test_ari.c:167
def info(msg)
static int invocation_count
Definition: test_ari.c:53

◆ handler()

static void handler ( const char *  name,
int  response_code,
struct ast_variable get_params,
struct ast_variable path_vars,
struct ast_variable headers,
struct ast_json body,
struct ast_ari_response response 
)
static

Definition at line 59 of file test_ari.c.

References ast_json_object_get(), ast_json_object_set(), ast_json_pack(), ast_json_string_create(), invocation_count, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

Referenced by __aco_option_register(), add_sdp_streams(), allocate_subscription(), app_handle_subscriptions(), ast_ari_add_handler(), ast_ari_invoke(), ast_ari_remove_handler(), ast_msg_has_destination(), ast_mwi_state_callback_all(), ast_mwi_state_callback_subscribed(), channel_hangup_handler_cb(), event_session_alloc(), handle_conf_user_join(), handle_conf_user_leave(), handle_incoming_sdp(), handle_negotiated_sdp_session_media(), handle_stasis_state(), msg_q_cb(), publish_request_initial(), pubsub_on_rx_publish_request(), pubsub_on_rx_subscribe_request(), remove_handler(), root_handler_create(), sdp_requires_deferral(), session_media_set_handler(), session_outgoing_nat_hook(), sip_outbound_publish_synchronize(), stream_destroy(), sub_persistence_recreate(), subscription_get_handler_from_rdata(), and xmpp_action_hook().

66 {
67  struct ast_json *message = ast_json_pack("{s: s, s: {}, s: {}, s: {}}",
68  "name", name,
69  "get_params",
70  "path_vars",
71  "headers");
72  struct ast_json *get_params_obj = ast_json_object_get(message, "get_params");
73  struct ast_json *path_vars_obj = ast_json_object_get(message, "path_vars");
74  struct ast_json *headers_obj = ast_json_object_get(message, "headers");
75 
76  for (; get_params != NULL; get_params = get_params->next) {
77  ast_json_object_set(get_params_obj, get_params->name, ast_json_string_create(get_params->value));
78  }
79 
80  for (; path_vars != NULL; path_vars = path_vars->next) {
81  ast_json_object_set(path_vars_obj, path_vars->name, ast_json_string_create(path_vars->value));
82  }
83 
84  for (; headers != NULL; headers = headers->next) {
85  ast_json_object_set(headers_obj, headers->name, ast_json_string_create(headers->value));
86  }
87 
89  response->response_code = response_code;
90  response->message = message;
91 }
struct ast_variable * next
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
#define NULL
Definition: resample.c:96
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:404
int response_code
Definition: ari.h:98
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
static const char name[]
Definition: cdr_mysql.c:74
static int invocation_count
Definition: test_ari.c:53
struct ast_json * message
Definition: ari.h:93
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
Abstract JSON element (object, array, string, int, ...).

◆ load_module()

static int load_module ( void  )
static

Definition at line 558 of file test_ari.c.

References AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, AST_TEST_REGISTER, ASTERISK_GPL_KEY, and unload_module().

559 {
560  AST_TEST_REGISTER(get_docs);
561  AST_TEST_REGISTER(get_docs_nohost);
562  AST_TEST_REGISTER(get_docs_notfound);
563  AST_TEST_REGISTER(get_docs_hackerz);
564  AST_TEST_REGISTER(invoke_get);
565  AST_TEST_REGISTER(invoke_wildcard);
566  AST_TEST_REGISTER(invoke_delete);
567  AST_TEST_REGISTER(invoke_post);
568  AST_TEST_REGISTER(invoke_bad_post);
569  AST_TEST_REGISTER(invoke_not_found);
571 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ response_alloc()

static struct ast_ari_response* response_alloc ( void  )
static

Definition at line 156 of file test_ari.c.

References ast_calloc, ast_str_create, and ast_ari_response::headers.

Referenced by AST_TEST_DEFINE().

157 {
158  struct ast_ari_response *resp = ast_calloc(1, sizeof(struct ast_ari_response));
159  resp->headers = ast_str_create(24);
160  return resp;
161 }
struct ast_str * headers
Definition: ari.h:95
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ response_free()

static void response_free ( struct ast_ari_response resp)
static

Definition at line 167 of file test_ari.c.

References ast_free, ast_json_unref(), ast_ari_response::headers, and ast_ari_response::message.

Referenced by AST_TEST_DEFINE().

168 {
169  if (!resp) {
170  return;
171  }
172  ast_free(resp->headers);
173  ast_json_unref(resp->message);
174  ast_free(resp);
175 }
struct ast_str * headers
Definition: ari.h:95
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define ast_free(a)
Definition: astmm.h:182
struct ast_json * message
Definition: ari.h:93

◆ setup_invocation_test()

static void* setup_invocation_test ( void  )
static

\ internal Setup test fixture for invocation tests.

Definition at line 181 of file test_ari.c.

References ast_ari_add_handler(), ast_assert, and invocation_count.

Referenced by AST_TEST_DEFINE().

181  {
182  int r;
183  invocation_count = 0;
185  ast_assert(r == 0);
186  return &invocation_count;
187 }
#define ast_assert(a)
Definition: utils.h:695
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
static int invocation_count
Definition: test_ari.c:53
static struct stasis_rest_handlers test_root
Definition: test_ari.c:142

◆ tear_down_invocation_test()

static void tear_down_invocation_test ( void *  ignore)
static

\ internal Tear down test fixture for invocation tests.

Definition at line 193 of file test_ari.c.

References ast_ari_remove_handler().

Referenced by AST_TEST_DEFINE().

193  {
194  if (!ignore) {
195  return;
196  }
198 }
def ignore(key=None, val=None, section=None, pjsip=None, nmapped=None, type='endpoint')
Definition: sip_to_pjsip.py:48
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202
static struct stasis_rest_handlers test_root
Definition: test_ari.c:142

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 543 of file test_ari.c.

References AST_TEST_UNREGISTER.

Referenced by load_module().

544 {
545  AST_TEST_UNREGISTER(get_docs);
546  AST_TEST_UNREGISTER(get_docs_nohost);
547  AST_TEST_UNREGISTER(get_docs_notfound);
548  AST_TEST_UNREGISTER(get_docs_hackerz);
549  AST_TEST_UNREGISTER(invoke_get);
550  AST_TEST_UNREGISTER(invoke_wildcard);
551  AST_TEST_UNREGISTER(invoke_delete);
552  AST_TEST_UNREGISTER(invoke_post);
553  AST_TEST_UNREGISTER(invoke_bad_post);
554  AST_TEST_UNREGISTER(invoke_not_found);
555  return 0;
556 }
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "ARI testing" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari", }
static

Definition at line 578 of file test_ari.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 578 of file test_ari.c.

◆ bam

struct stasis_rest_handlers bam
static

Definition at line 133 of file test_ari.c.

◆ bang

struct stasis_rest_handlers bang
static

Definition at line 116 of file test_ari.c.

◆ bar

struct stasis_rest_handlers bar
static

Definition at line 125 of file test_ari.c.

◆ invocation_count

int invocation_count
static

Definition at line 53 of file test_ari.c.

Referenced by AST_TEST_DEFINE(), handler(), and setup_invocation_test().

◆ test_root

struct stasis_rest_handlers test_root
static

Definition at line 142 of file test_ari.c.