| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/test.h" |
| 37 |
#include "asterisk/ari.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static int invocation_count; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static void handler(const char *name, |
| 60 |
int response_code, |
| 61 |
struct ast_variable *get_params, |
| 62 |
struct ast_variable *path_vars, |
| 63 |
struct ast_variable *headers, |
| 64 |
struct ast_json *body, |
| 65 |
struct ast_ari_response *response) |
| 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 |
|
| 88 |
++invocation_count; |
| 89 |
response->response_code = response_code; |
| 90 |
response->message = message; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#define HANDLER(name, response_code) \ |
| 98 |
static void name(struct ast_tcptls_session_instance *ser, \ |
| 99 |
struct ast_variable *get_params, \ |
| 100 |
struct ast_variable *path_vars, \ |
| 101 |
struct ast_variable *headers, \ |
| 102 |
struct ast_json *body, \ |
| 103 |
struct ast_ari_response *response) \ |
| 104 |
{ \ |
| 105 |
handler(#name, response_code, get_params, path_vars, headers, body, response); \ |
| 106 |
} |
| 107 |
|
| 108 |
HANDLER(bang_get, 200) |
| 109 |
HANDLER(bang_post, 200) |
| 110 |
HANDLER(bang_delete, 204) |
| 111 |
HANDLER(bar_get, 200) |
| 112 |
HANDLER(bar_post, 200) |
| 113 |
HANDLER(bam_get, 200) |
| 114 |
HANDLER(foo_get, 200) |
| 115 |
|
| 116 |
static struct stasis_rest_handlers bang = { |
| 117 |
.path_segment = "bang", |
| 118 |
.callbacks = { |
| 119 |
[AST_HTTP_GET] = bang_get, |
| 120 |
[AST_HTTP_POST] = bang_post, |
| 121 |
[AST_HTTP_DELETE] = bang_delete, |
| 122 |
}, |
| 123 |
.num_children = 0 |
| 124 |
}; |
| 125 |
static struct stasis_rest_handlers bar = { |
| 126 |
.path_segment = "bar", |
| 127 |
.callbacks = { |
| 128 |
[AST_HTTP_GET] = bar_get, |
| 129 |
[AST_HTTP_POST] = bar_post, |
| 130 |
}, |
| 131 |
.num_children = 0 |
| 132 |
}; |
| 133 |
static struct stasis_rest_handlers bam = { |
| 134 |
.path_segment = "bam", |
| 135 |
.is_wildcard = 1, |
| 136 |
.callbacks = { |
| 137 |
[AST_HTTP_GET] = bam_get, |
| 138 |
}, |
| 139 |
.num_children = 1, |
| 140 |
.children = { &bang } |
| 141 |
}; |
| 142 |
static struct stasis_rest_handlers test_root = { |
| 143 |
.path_segment = "foo", |
| 144 |
.callbacks = { |
| 145 |
[AST_HTTP_GET] = foo_get, |
| 146 |
}, |
| 147 |
.num_children = 3, |
| 148 |
.children = { &bar, &bam, &bang } |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static struct ast_ari_response *response_alloc(void) |
| 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 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
static void response_free(struct ast_ari_response *resp) |
| 168 |
{ |
| 169 |
if (!resp) { |
| 170 |
return; |
| 171 |
} |
| 172 |
ast_free(resp->headers); |
| 173 |
ast_json_unref(resp->message); |
| 174 |
ast_free(resp); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static void *setup_invocation_test(void) { |
| 182 |
int r; |
| 183 |
invocation_count = 0; |
| 184 |
r = ast_ari_add_handler(&test_root); |
| 185 |
ast_assert(r == 0); |
| 186 |
return &invocation_count; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static void tear_down_invocation_test(void *ignore) { |
| 194 |
if (!ignore) { |
| 195 |
return; |
| 196 |
} |
| 197 |
ast_ari_remove_handler(&test_root); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
AST_TEST_DEFINE(get_docs) |
| 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 |
|
| 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 |
} |
| 232 |
|
| 233 |
AST_TEST_DEFINE(get_docs_nohost) |
| 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 |
|
| 255 |
basePathJson = ast_json_object_get(response->message, "basePath"); |
| 256 |
ast_test_validate(test, NULL == basePathJson); |
| 257 |
|
| 258 |
return AST_TEST_PASS; |
| 259 |
} |
| 260 |
|
| 261 |
AST_TEST_DEFINE(get_docs_notfound) |
| 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 |
} |
| 283 |
|
| 284 |
AST_TEST_DEFINE(get_docs_hackerz) |
| 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 |
} |
| 306 |
|
| 307 |
AST_TEST_DEFINE(invoke_get) |
| 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 *tail = NULL; |
| 313 |
RAII_VAR(struct ast_variable *, get_params, NULL, ast_variables_destroy); |
| 314 |
RAII_VAR(struct ast_variable *, headers, NULL, ast_variables_destroy); |
| 315 |
|
| 316 |
switch (cmd) { |
| 317 |
case TEST_INIT: |
| 318 |
info->name = __func__; |
| 319 |
info->category = "/res/ari/"; |
| 320 |
info->summary = "Test simple GET of an HTTP resource."; |
| 321 |
info->description = "Test ARI binding logic."; |
| 322 |
return AST_TEST_NOT_RUN; |
| 323 |
case TEST_EXECUTE: |
| 324 |
break; |
| 325 |
} |
| 326 |
|
| 327 |
fixture = setup_invocation_test(); |
| 328 |
response = response_alloc(); |
| 329 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("get1", "get-one", __FILE__)); |
| 330 |
ast_assert(tail != NULL); |
| 331 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("get2", "get-two", __FILE__)); |
| 332 |
ast_assert(tail != NULL); |
| 333 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("api_key", "aritestro:aritestropw", __FILE__)); |
| 334 |
ast_assert(tail != NULL); |
| 335 |
|
| 336 |
tail = ast_variable_list_append_hint(&headers, NULL, ast_variable_new("head1", "head-one", __FILE__)); |
| 337 |
ast_assert(tail != NULL); |
| 338 |
tail = ast_variable_list_append_hint(&headers, NULL, ast_variable_new("head2", "head-two", __FILE__)); |
| 339 |
ast_assert(tail != NULL); |
| 340 |
|
| 341 |
expected = ast_json_pack("{s: s, s: {s: s, s: s, s: s}, s: {s: s, s: s}, s: {}}", |
| 342 |
"name", "foo_get", |
| 343 |
"get_params", |
| 344 |
"get1", "get-one", |
| 345 |
"get2", "get-two", |
| 346 |
"api_key", "aritestro:aritestropw", |
| 347 |
"headers", |
| 348 |
"head1", "head-one", |
| 349 |
"head2", "head-two", |
| 350 |
"path_vars"); |
| 351 |
|
| 352 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo", AST_HTTP_GET, get_params, headers, |
| 353 |
ast_json_null(), response); |
| 354 |
|
| 355 |
ast_test_validate(test, 1 == invocation_count); |
| 356 |
ast_test_validate(test, 200 == response->response_code); |
| 357 |
ast_test_validate(test, ast_json_equal(expected, response->message)); |
| 358 |
|
| 359 |
return AST_TEST_PASS; |
| 360 |
} |
| 361 |
|
| 362 |
AST_TEST_DEFINE(invoke_wildcard) |
| 363 |
{ |
| 364 |
RAII_VAR(void *, fixture, NULL, tear_down_invocation_test); |
| 365 |
RAII_VAR(struct ast_ari_response *, response, NULL, response_free); |
| 366 |
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); |
| 367 |
struct ast_variable *tail = NULL; |
| 368 |
RAII_VAR(struct ast_variable *, get_params, NULL, ast_variables_destroy); |
| 369 |
struct ast_variable *headers = NULL; |
| 370 |
|
| 371 |
switch (cmd) { |
| 372 |
case TEST_INIT: |
| 373 |
info->name = __func__; |
| 374 |
info->category = "/res/ari/"; |
| 375 |
info->summary = "Test GET of a wildcard resource."; |
| 376 |
info->description = "Test ARI binding logic."; |
| 377 |
return AST_TEST_NOT_RUN; |
| 378 |
case TEST_EXECUTE: |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
fixture = setup_invocation_test(); |
| 383 |
response = response_alloc(); |
| 384 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("api_key", "aritestro:aritestropw", __FILE__)); |
| 385 |
ast_assert(tail != NULL); |
| 386 |
|
| 387 |
expected = ast_json_pack("{s: s, s: {s: s}, s: {}, s: {s: s}}", |
| 388 |
"name", "bam_get", |
| 389 |
"get_params", |
| 390 |
"api_key", "aritestro:aritestropw", |
| 391 |
"headers", |
| 392 |
"path_vars", |
| 393 |
"bam", "foshizzle"); |
| 394 |
|
| 395 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo/foshizzle", AST_HTTP_GET, get_params, headers, |
| 396 |
ast_json_null(), response); |
| 397 |
|
| 398 |
ast_test_validate(test, 1 == invocation_count); |
| 399 |
ast_test_validate(test, 200 == response->response_code); |
| 400 |
ast_test_validate(test, ast_json_equal(expected, response->message)); |
| 401 |
|
| 402 |
return AST_TEST_PASS; |
| 403 |
} |
| 404 |
|
| 405 |
static enum ast_test_result_state invoke_delete_common(struct ast_test_info *info, enum ast_test_command cmd, |
| 406 |
struct ast_test *test, const char *creds, int expect_pass, int expect_rc) |
| 407 |
{ |
| 408 |
RAII_VAR(void *, fixture, NULL, tear_down_invocation_test); |
| 409 |
RAII_VAR(struct ast_ari_response *, response, NULL, response_free); |
| 410 |
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); |
| 411 |
struct ast_variable *tail = NULL; |
| 412 |
RAII_VAR(struct ast_variable *, get_params, NULL, ast_variables_destroy); |
| 413 |
struct ast_variable *headers = NULL; |
| 414 |
|
| 415 |
fixture = setup_invocation_test(); |
| 416 |
response = response_alloc(); |
| 417 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("api_key", creds, __FILE__)); |
| 418 |
ast_assert(tail != NULL); |
| 419 |
|
| 420 |
expected = ast_json_pack("{s: s, s: {s: s}, s: {}, s: {s: s}}", |
| 421 |
"name", "bang_delete", |
| 422 |
"get_params", |
| 423 |
"api_key", creds, |
| 424 |
"headers", |
| 425 |
"path_vars", |
| 426 |
"bam", "foshizzle"); |
| 427 |
|
| 428 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo/foshizzle/bang", AST_HTTP_DELETE, get_params, headers, |
| 429 |
ast_json_null(), response); |
| 430 |
|
| 431 |
ast_test_validate(test, expect_pass == invocation_count); |
| 432 |
ast_test_validate(test, expect_rc == response->response_code); |
| 433 |
ast_test_validate(test, ast_json_equal(expected, response->message) == expect_pass); |
| 434 |
|
| 435 |
return AST_TEST_PASS; |
| 436 |
} |
| 437 |
|
| 438 |
AST_TEST_DEFINE(invoke_delete) |
| 439 |
{ |
| 440 |
switch (cmd) { |
| 441 |
case TEST_INIT: |
| 442 |
info->name = __func__; |
| 443 |
info->category = "/res/ari/"; |
| 444 |
info->summary = "Test DELETE of an HTTP resource."; |
| 445 |
info->description = "Test ARI binding logic."; |
| 446 |
return AST_TEST_NOT_RUN; |
| 447 |
case TEST_EXECUTE: |
| 448 |
break; |
| 449 |
} |
| 450 |
|
| 451 |
return invoke_delete_common(info, cmd, test, "aritest:aritestpw", 1, 204); |
| 452 |
} |
| 453 |
|
| 454 |
AST_TEST_DEFINE(invoke_delete_forbidden) |
| 455 |
{ |
| 456 |
switch (cmd) { |
| 457 |
case TEST_INIT: |
| 458 |
info->name = __func__; |
| 459 |
info->category = "/res/ari/"; |
| 460 |
info->summary = "Test forbidden DELETE of an HTTP resource."; |
| 461 |
info->description = "Test ARI binding logic."; |
| 462 |
return AST_TEST_NOT_RUN; |
| 463 |
case TEST_EXECUTE: |
| 464 |
break; |
| 465 |
} |
| 466 |
|
| 467 |
return invoke_delete_common(info, cmd, test, "aritestro:aritestropw", 0, 403); |
| 468 |
} |
| 469 |
|
| 470 |
AST_TEST_DEFINE(invoke_post) |
| 471 |
{ |
| 472 |
RAII_VAR(void *, fixture, NULL, tear_down_invocation_test); |
| 473 |
RAII_VAR(struct ast_ari_response *, response, NULL, response_free); |
| 474 |
RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref); |
| 475 |
struct ast_variable *tail = NULL; |
| 476 |
RAII_VAR(struct ast_variable *, get_params, NULL, ast_variables_destroy); |
| 477 |
RAII_VAR(struct ast_variable *, headers, NULL, ast_variables_destroy); |
| 478 |
|
| 479 |
switch (cmd) { |
| 480 |
case TEST_INIT: |
| 481 |
info->name = __func__; |
| 482 |
info->category = "/res/ari/"; |
| 483 |
info->summary = "Test POST of an HTTP resource."; |
| 484 |
info->description = "Test ARI binding logic."; |
| 485 |
return AST_TEST_NOT_RUN; |
| 486 |
case TEST_EXECUTE: |
| 487 |
break; |
| 488 |
} |
| 489 |
|
| 490 |
fixture = setup_invocation_test(); |
| 491 |
response = response_alloc(); |
| 492 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("get1", "get-one", __FILE__)); |
| 493 |
ast_assert(tail != NULL); |
| 494 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("get2", "get-two", __FILE__)); |
| 495 |
ast_assert(tail != NULL); |
| 496 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("api_key", "aritest:aritestpw", __FILE__)); |
| 497 |
ast_assert(tail != NULL); |
| 498 |
|
| 499 |
tail = ast_variable_list_append_hint(&headers, NULL, ast_variable_new("head1", "head-one", __FILE__)); |
| 500 |
ast_assert(tail != NULL); |
| 501 |
tail = ast_variable_list_append_hint(&headers, NULL, ast_variable_new("head2", "head-two", __FILE__)); |
| 502 |
ast_assert(tail != NULL); |
| 503 |
|
| 504 |
expected = ast_json_pack("{s: s, s: {s: s, s: s, s: s}, s: {s: s, s: s}, s: {}}", |
| 505 |
"name", "bar_post", |
| 506 |
"get_params", |
| 507 |
"get1", "get-one", |
| 508 |
"get2", "get-two", |
| 509 |
"api_key", "aritest:aritestpw", |
| 510 |
"headers", |
| 511 |
"head1", "head-one", |
| 512 |
"head2", "head-two", |
| 513 |
"path_vars"); |
| 514 |
|
| 515 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo/bar", AST_HTTP_POST, get_params, headers, |
| 516 |
ast_json_null(), response); |
| 517 |
|
| 518 |
ast_test_validate(test, 1 == invocation_count); |
| 519 |
ast_test_validate(test, 200 == response->response_code); |
| 520 |
ast_test_validate(test, ast_json_equal(expected, response->message)); |
| 521 |
|
| 522 |
return AST_TEST_PASS; |
| 523 |
} |
| 524 |
|
| 525 |
AST_TEST_DEFINE(invoke_bad_post) |
| 526 |
{ |
| 527 |
RAII_VAR(void *, fixture, NULL, tear_down_invocation_test); |
| 528 |
RAII_VAR(struct ast_ari_response *, response, NULL, response_free); |
| 529 |
struct ast_variable *tail = NULL; |
| 530 |
RAII_VAR(struct ast_variable *, get_params, NULL, ast_variables_destroy); |
| 531 |
struct ast_variable *headers = NULL; |
| 532 |
|
| 533 |
switch (cmd) { |
| 534 |
case TEST_INIT: |
| 535 |
info->name = __func__; |
| 536 |
info->category = "/res/ari/"; |
| 537 |
info->summary = "Test POST on a resource that doesn't support it."; |
| 538 |
info->description = "Test ARI binding logic."; |
| 539 |
return AST_TEST_NOT_RUN; |
| 540 |
case TEST_EXECUTE: |
| 541 |
break; |
| 542 |
} |
| 543 |
|
| 544 |
fixture = setup_invocation_test(); |
| 545 |
response = response_alloc(); |
| 546 |
|
| 547 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("api_key", "aritest:aritestpw", __FILE__)); |
| 548 |
ast_assert(tail != NULL); |
| 549 |
|
| 550 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo", AST_HTTP_POST, get_params, headers, |
| 551 |
ast_json_null(), response); |
| 552 |
|
| 553 |
ast_test_validate(test, 0 == invocation_count); |
| 554 |
ast_test_validate(test, 405 == response->response_code); |
| 555 |
|
| 556 |
return AST_TEST_PASS; |
| 557 |
} |
| 558 |
|
| 559 |
AST_TEST_DEFINE(invoke_no_user) |
| 560 |
{ |
| 561 |
RAII_VAR(void *, fixture, NULL, tear_down_invocation_test); |
| 562 |
RAII_VAR(struct ast_ari_response *, response, NULL, response_free); |
| 563 |
struct ast_variable *get_params = NULL; |
| 564 |
struct ast_variable *headers = NULL; |
| 565 |
|
| 566 |
switch (cmd) { |
| 567 |
case TEST_INIT: |
| 568 |
info->name = __func__; |
| 569 |
info->category = "/res/ari/"; |
| 570 |
info->summary = "Test POST on a resource that doesn't support it."; |
| 571 |
info->description = "Test ARI binding logic."; |
| 572 |
return AST_TEST_NOT_RUN; |
| 573 |
case TEST_EXECUTE: |
| 574 |
break; |
| 575 |
} |
| 576 |
|
| 577 |
fixture = setup_invocation_test(); |
| 578 |
response = response_alloc(); |
| 579 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo", AST_HTTP_POST, get_params, headers, |
| 580 |
ast_json_null(), response); |
| 581 |
|
| 582 |
ast_test_validate(test, 0 == invocation_count); |
| 583 |
ast_test_validate(test, 401 == response->response_code); |
| 584 |
|
| 585 |
return AST_TEST_PASS; |
| 586 |
} |
| 587 |
|
| 588 |
AST_TEST_DEFINE(invoke_not_found) |
| 589 |
{ |
| 590 |
RAII_VAR(void *, fixture, NULL, tear_down_invocation_test); |
| 591 |
RAII_VAR(struct ast_ari_response *, response, NULL, response_free); |
| 592 |
struct ast_variable *tail = NULL; |
| 593 |
RAII_VAR(struct ast_variable *, get_params, NULL, ast_variables_destroy); |
| 594 |
struct ast_variable *headers = NULL; |
| 595 |
|
| 596 |
switch (cmd) { |
| 597 |
case TEST_INIT: |
| 598 |
info->name = __func__; |
| 599 |
info->category = "/res/ari/"; |
| 600 |
info->summary = "Test GET on a resource that does not exist."; |
| 601 |
info->description = "Test ARI binding logic."; |
| 602 |
return AST_TEST_NOT_RUN; |
| 603 |
case TEST_EXECUTE: |
| 604 |
break; |
| 605 |
} |
| 606 |
|
| 607 |
fixture = setup_invocation_test(); |
| 608 |
response = response_alloc(); |
| 609 |
|
| 610 |
tail = ast_variable_list_append_hint(&get_params, NULL, ast_variable_new("api_key", "aritest:aritestpw", __FILE__)); |
| 611 |
ast_assert(tail != NULL); |
| 612 |
|
| 613 |
ast_ari_invoke(NULL, ARI_INVOKE_SOURCE_TEST, NULL, "foo/fizzle/i-am-not-a-resource", AST_HTTP_GET, get_params, headers, |
| 614 |
ast_json_null(), response); |
| 615 |
|
| 616 |
ast_test_validate(test, 0 == invocation_count); |
| 617 |
ast_test_validate(test, 404 == response->response_code); |
| 618 |
|
| 619 |
return AST_TEST_PASS; |
| 620 |
} |
| 621 |
|
| 622 |
static int unload_module(void) |
| 623 |
{ |
| 624 |
AST_TEST_UNREGISTER(get_docs); |
| 625 |
AST_TEST_UNREGISTER(get_docs_nohost); |
| 626 |
AST_TEST_UNREGISTER(get_docs_notfound); |
| 627 |
AST_TEST_UNREGISTER(get_docs_hackerz); |
| 628 |
AST_TEST_UNREGISTER(invoke_get); |
| 629 |
AST_TEST_UNREGISTER(invoke_wildcard); |
| 630 |
AST_TEST_UNREGISTER(invoke_delete); |
| 631 |
AST_TEST_UNREGISTER(invoke_delete_forbidden); |
| 632 |
AST_TEST_UNREGISTER(invoke_post); |
| 633 |
AST_TEST_UNREGISTER(invoke_bad_post); |
| 634 |
AST_TEST_UNREGISTER(invoke_no_user); |
| 635 |
AST_TEST_UNREGISTER(invoke_not_found); |
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static int load_module(void) |
| 640 |
{ |
| 641 |
AST_TEST_REGISTER(get_docs); |
| 642 |
AST_TEST_REGISTER(get_docs_nohost); |
| 643 |
AST_TEST_REGISTER(get_docs_notfound); |
| 644 |
AST_TEST_REGISTER(get_docs_hackerz); |
| 645 |
AST_TEST_REGISTER(invoke_get); |
| 646 |
AST_TEST_REGISTER(invoke_wildcard); |
| 647 |
AST_TEST_REGISTER(invoke_delete); |
| 648 |
AST_TEST_REGISTER(invoke_delete_forbidden); |
| 649 |
AST_TEST_REGISTER(invoke_post); |
| 650 |
AST_TEST_REGISTER(invoke_bad_post); |
| 651 |
AST_TEST_REGISTER(invoke_no_user); |
| 652 |
AST_TEST_REGISTER(invoke_not_found); |
| 653 |
return AST_MODULE_LOAD_SUCCESS; |
| 654 |
} |
| 655 |
|
| 656 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ARI testing", |
| 657 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 658 |
.load = load_module, |
| 659 |
.unload = unload_module, |
| 660 |
.requires = "res_ari", |
| 661 |
); |