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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <string.h>
#include <ctype.h>
#include <jansson.h>
#include <ulfius.h>
#include <orcania.h>
#include "unit-tests.h"
/**
* Developper-friendly response print
*/
void print_response(struct _u_response * response) {
char * dump_json = NULL;
json_t * json_body;
if (response != NULL) {
printf("Status: %ld\n\n", response->status);
json_body = ulfius_get_json_body_response(response, NULL);
if (json_body != NULL) {
dump_json = json_dumps(json_body, JSON_INDENT(2));
printf("Json body:\n%s\n\n", dump_json);
free(dump_json);
} else {
printf("String body: %.*s\n\n", (int)response->binary_body_length, (char *)response->binary_body);
}
json_decref(json_body);
}
}
int test_request(struct _u_request * req, long int expected_status, json_t * expected_json_body, const char * exptected_string_body, const char * expected_redirect_uri_contains) {
int res, to_return = 0;
struct _u_response response;
json_t * json_body;
ulfius_init_response(&response);
res = ulfius_send_http_request(req, &response);
if (res == U_OK) {
if (response.status != expected_status) {
printf("##########################\nError status (%s %s %ld)\n", req->http_verb, req->http_url, expected_status);
print_response(&response);
printf("##########################\n\n");
} else if (expected_json_body != NULL) {
json_body = ulfius_get_json_body_response(&response, NULL);
if (json_body == NULL || json_search(json_body, expected_json_body) == NULL) {
char * dump_expected = json_dumps(expected_json_body, JSON_ENCODE_ANY), * dump_response = json_dumps(json_body, JSON_ENCODE_ANY);
printf("##########################\nError json (%s %s)\n", req->http_verb, req->http_url);
printf("Expected result in response:\n%s\nWhile response is:\n%s\n", dump_expected, dump_response);
printf("##########################\n\n");
free(dump_expected);
free(dump_response);
} else {
to_return = 1;
}
json_decref(json_body);
} else if (exptected_string_body != NULL && o_strnstr((const char *)response.binary_body, exptected_string_body, response.binary_body_length) == NULL) {
printf("##########################\nError (%s %s)\n", req->http_verb, req->http_url);
printf("Expected result in response:\n%s\nWhile response is:\n%s\n", exptected_string_body, (const char *)response.binary_body);
printf("##########################\n\n");
} else if (expected_redirect_uri_contains != NULL && o_strstr(u_map_get(response.map_header, "Location"), expected_redirect_uri_contains) == NULL) {
printf("##########################\nError (%s %s)\n", req->http_verb, req->http_url);
printf("expected_redirect_uri_contains is %s\nwhile redirect_uri is %s\n", expected_redirect_uri_contains, u_map_get(response.map_header, "Location"));
printf("##########################\n\n");
} else {
to_return = 1;
}
} else {
printf("Error in http request: %d\n", res);
}
ulfius_clean_response(&response);
return to_return;
}
int run_simple_test(struct _u_request * req, const char * method, const char * url, const char * auth_basic_user, const char * auth_basic_password, json_t * json_body, const struct _u_map * body, int expected_status, json_t * expected_json_body, const char * exptected_string_body, const char * expected_redirect_uri_contains) {
struct _u_request * request;
int res;
if (req != NULL) {
request = ulfius_duplicate_request(req);
free(request->http_verb);
free(request->http_url);
} else {
request = malloc(sizeof (struct _u_request));
ulfius_init_request(request);
}
request->http_verb = o_strdup(method);
request->http_url = strdup(url);
if (body != NULL) {
u_map_copy_into(request->map_post_body, body);
} else if (json_body != NULL) {
ulfius_set_json_body_request(request, json_body);
}
free(request->auth_basic_user);
free(request->auth_basic_password);
request->auth_basic_user = o_strdup(auth_basic_user);
request->auth_basic_password = o_strdup(auth_basic_password);
res = test_request(request, expected_status, expected_json_body, exptected_string_body, expected_redirect_uri_contains);
ulfius_clean_request_full(request);
return res;
}
/**
* Converts a hex character to its integer value
*/
char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
/**
* Converts an integer value to its hex character
*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/**
* Returns a url-encoded version of str
* IMPORTANT: be sure to free() the returned string after use
* Thanks Geek Hideout!
* http://www.geekhideout.com/urlcode.shtml
*/
char * url_encode(const char * str) {
char * pstr = (char*)str, * buf = malloc(strlen(str) * 3 + 1), * pbuf = buf;
while (* pstr) {
if (isalnum(* pstr) || * pstr == '-' || * pstr == '_' || * pstr == '.' || * pstr == '~')
* pbuf++ = * pstr;
else if (* pstr == ' ')
* pbuf++ = '+';
else
* pbuf++ = '%', * pbuf++ = to_hex(* pstr >> 4), * pbuf++ = to_hex(* pstr & 15);
pstr++;
}
* pbuf = '\0';
return buf;
}
/**
* Returns a url-decoded version of str
* IMPORTANT: be sure to free() the returned string after use
* Thanks Geek Hideout!
* http://www.geekhideout.com/urlcode.shtml
*/
char * url_decode(const char * str) {
char * pstr = (char*)str, * buf = malloc(strlen(str) + 1), * pbuf = buf;
while (* pstr) {
if (* pstr == '%') {
if (pstr[1] && pstr[2]) {
* pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
pstr += 2;
}
} else if (* pstr == '+') {
* pbuf++ = ' ';
} else {
* pbuf++ = * pstr;
}
pstr++;
}
* pbuf = '\0';
return buf;
}
|