network/gru_uri_format_test.c

URI formatting example

#include <stdbool.h>
#include <stdlib.h>

#include <common/gru_status.h>
#include <network/gru_uri.h>

bool test1() {
    gru_uri_t uri = {0};
    gru_status_t status = gru_status_new();

    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, "Test 1 failed due to invalid URI: %s", uri_str);
        free(uri_str);
        return false;
    }

    free(uri_str);
    return true;
}

bool test2() {
    gru_uri_t uri = {0};
    gru_status_t status = gru_status_new();

    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, "Test 2 failed due to invalid URI: %s", uri_str);
        free(uri_str);
        return false;
    }

    free(uri_str);
    return true;
}

bool test3() {
    gru_uri_t uri = {0};
    gru_status_t status = gru_status_new();

    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, "Test 3 failed due to invalid URI: %s", uri_str);
        free(uri_str);
        return false;
    }

    free(uri_str);
    return true;
}

bool test4() {
    gru_uri_t uri = {0};
    gru_status_t status = gru_status_new();

    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, "Test 4 failed due to invalid URI: %s", uri_str);
        free(uri_str);
        return false;
    }

    free(uri_str);
    return true;
}

bool test5() {
    gru_uri_t uri = {0};
    gru_status_t status = gru_status_new();

    uri.scheme = "test";
    uri.host = "2620:52:0:2880:223:7dff:fe4a:743d";
    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://[2620:52:0:2880:223:7dff:fe4a:743d]:99") != 0) {
        fprintf(stderr, "Test 5 failed due to invalid URI: %s", uri_str);
        free(uri_str);
        return false;
    }

    free(uri_str);
    return true;
}

int main(int argc, char **argv) {

    // TODO: Not my best work. To be refactored ...
    if (test1()) {
        if (test2()) {
            if (test3()) {
                if (test4()) {
                    if (test5()) {
                        return EXIT_SUCCESS;
                    }
                }
            }
        }
    }

    return EXIT_FAILURE;
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 7 Jan 2019 for GRU - Generic Reusable Utilities by  doxygen 1.6.1