uri_test.c (2486B)
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+httpc@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _POSIX_C_SOURCE 200809L
- #include "httpc.h" // decode_uri
- #include <stdio.h> // printf
- #include <string.h> // strcmp
- #include <stdbool.h>
- int counter = 0;
- #define diag(x) (diag_f((int)(x), #x))
- static int
- diag_f(int status, const char *expression)
- {
- if(status) return status;
- printf("# Expression failed: %s\n", expression);
- return status;
- }
- #define str_match(got, expt) (str_match_f(got, #got, expt, #expt))
- static int
- str_match_f(const char *got, const char *got_name, const char *expt, const char *expt_name)
- {
- if(got == NULL && expt == NULL) return 0;
- if(got != NULL && expt != NULL)
- {
- if(strcmp(got, expt) == 0) return 0;
- printf("# %s (\"%s\") != %s (\"%s\")\n", got_name, got, expt_name, expt);
- return 1;
- }
- printf("# %s (\"%s\") != %s (\"%s\")\n", got_name, got, expt_name, expt);
- return 0;
- }
- static bool
- t(char *arg, struct URI *exp, const char *error)
- {
- int id = ++counter;
- char *res_error = NULL;
- struct URI *res = decode_uri(arg, &res_error);
- int err = 0;
- err += str_match(res_error, error);
- if(exp == NULL)
- {
- if(!diag(res == NULL)) err++;
- }
- else
- {
- if(diag(res != NULL))
- {
- err += str_match(exp->scheme, res->scheme);
- err += str_match(exp->host, res->host);
- err += str_match(exp->port, res->port);
- err += str_match(exp->path, res->path);
- err += str_match(exp->fragment, res->fragment);
- } else err++;
- }
- decode_uri_finish(res);
- if(err != 0)
- {
- printf("not ok %d - %s\n", id, arg);
- return 1;
- }
- printf("ok %d - %s\n", id, arg);
- return 0;
- }
- int
- main()
- {
- int plan = 4;
- printf("1..%d\n", plan);
- int err = 0;
- err += t(
- "https://example.org/",
- &(struct URI){
- .scheme = "https",
- .host = "example.org",
- .port = NULL,
- .path = "/",
- .fragment = NULL,
- },
- NULL
- );
- err += t(
- "https://example.org",
- &(struct URI){
- .scheme = "https",
- .host = "example.org",
- .port = NULL,
- .path = "",
- .fragment = NULL,
- },
- NULL
- );
- err += t(
- "https://example.org:1337/",
- &(struct URI){
- .scheme = "https",
- .host = "example.org",
- .port = "1337",
- .path = "/",
- .fragment = NULL,
- },
- NULL
- );
- err += t(
- "https://[fe80::feed:face]:1337/",
- &(struct URI){
- .scheme = "https",
- .host = "fe80::feed:face",
- .port = "1337",
- .path = "/",
- .fragment = NULL,
- },
- NULL
- );
- if(!diag(plan == counter)) err++;
- return err;
- }