GRU - Generic Reusable Utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
network/gru_uri_format_test.c

URI formatting example

#include <stdbool.h>
#include <stdlib.h>
bool test1() {
gru_uri_t uri = {0};
uri.scheme = "test";
uri.host = "localhost";
uri.port = 90;
uri.path = "/root/path";
char *uri_str = gru_uri_simple_format(&uri, &status);
if (!uri_str) {
fprintf(stderr, "%s", status.message);
return false;
}
if (strcmp(uri_str, "test://localhost:90/root/path") != 0) {
fprintf(stderr, "Invalid URI: %s", uri_str);
free(uri_str);
return false;
}
free(uri_str);
return true;
}
bool test2() {
gru_uri_t uri = {0};
uri.scheme = "test";
uri.host = "localhost";
uri.port = 0;
uri.path = "/root/path";
char *uri_str = gru_uri_simple_format(&uri, &status);
if (!uri_str) {
fprintf(stderr, "%s", status.message);
return false;
}
if (strcmp(uri_str, "test://localhost/root/path") != 0) {
fprintf(stderr, "Invalid URI: %s", uri_str);
free(uri_str);
return false;
}
free(uri_str);
return true;
}
bool test3() {
gru_uri_t uri = {0};
uri.scheme = "test";
uri.host = "localhost";
uri.port = 0;
uri.path = NULL;
char *uri_str = gru_uri_simple_format(&uri, &status);
if (!uri_str) {
fprintf(stderr, "%s", status.message);
return false;
}
if (strcmp(uri_str, "test://localhost") != 0) {
fprintf(stderr, "Invalid URI: %s", uri_str);
free(uri_str);
return false;
}
free(uri_str);
return true;
}
bool test4() {
gru_uri_t uri = {0};
uri.scheme = "test";
uri.host = "localhost";
uri.port = 99;
uri.path = NULL;
char *uri_str = gru_uri_simple_format(&uri, &status);
if (!uri_str) {
fprintf(stderr, "%s", status.message);
return false;
}
if (strcmp(uri_str, "test://localhost:99") != 0) {
fprintf(stderr, "Invalid URI: %s", uri_str);
free(uri_str);
return false;
}
free(uri_str);
return true;
}
int main(int argc, char **argv) {
if (test1()) {
if (test2()) {
if (test3()) {
if (test4()) {
return EXIT_SUCCESS;
}
}
}
}
return EXIT_FAILURE;
}