logo

httpc

barebones HTTP client, intended for simple usage like downloading filesgit clone https://hacktivis.me/git/httpc.git

uri_test.c (2486B)


  1. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+httpc@hacktivis.me>
  2. // SPDX-License-Identifier: MPL-2.0
  3. #define _POSIX_C_SOURCE 200809L
  4. #include "httpc.h" // decode_uri
  5. #include <stdio.h> // printf
  6. #include <string.h> // strcmp
  7. #include <stdbool.h>
  8. int counter = 0;
  9. #define diag(x) (diag_f((int)(x), #x))
  10. static int
  11. diag_f(int status, const char *expression)
  12. {
  13. if(status) return status;
  14. printf("# Expression failed: %s\n", expression);
  15. return status;
  16. }
  17. #define str_match(got, expt) (str_match_f(got, #got, expt, #expt))
  18. static int
  19. str_match_f(const char *got, const char *got_name, const char *expt, const char *expt_name)
  20. {
  21. if(got == NULL && expt == NULL) return 0;
  22. if(got != NULL && expt != NULL)
  23. {
  24. if(strcmp(got, expt) == 0) return 0;
  25. printf("# %s (\"%s\") != %s (\"%s\")\n", got_name, got, expt_name, expt);
  26. return 1;
  27. }
  28. printf("# %s (\"%s\") != %s (\"%s\")\n", got_name, got, expt_name, expt);
  29. return 0;
  30. }
  31. static bool
  32. t(char *arg, struct URI *exp, const char *error)
  33. {
  34. int id = ++counter;
  35. char *res_error = NULL;
  36. struct URI *res = decode_uri(arg, &res_error);
  37. int err = 0;
  38. err += str_match(res_error, error);
  39. if(exp == NULL)
  40. {
  41. if(!diag(res == NULL)) err++;
  42. }
  43. else
  44. {
  45. if(diag(res != NULL))
  46. {
  47. err += str_match(exp->scheme, res->scheme);
  48. err += str_match(exp->host, res->host);
  49. err += str_match(exp->port, res->port);
  50. err += str_match(exp->path, res->path);
  51. err += str_match(exp->fragment, res->fragment);
  52. } else err++;
  53. }
  54. decode_uri_finish(res);
  55. if(err != 0)
  56. {
  57. printf("not ok %d - %s\n", id, arg);
  58. return 1;
  59. }
  60. printf("ok %d - %s\n", id, arg);
  61. return 0;
  62. }
  63. int
  64. main()
  65. {
  66. int plan = 4;
  67. printf("1..%d\n", plan);
  68. int err = 0;
  69. err += t(
  70. "https://example.org/",
  71. &(struct URI){
  72. .scheme = "https",
  73. .host = "example.org",
  74. .port = NULL,
  75. .path = "/",
  76. .fragment = NULL,
  77. },
  78. NULL
  79. );
  80. err += t(
  81. "https://example.org",
  82. &(struct URI){
  83. .scheme = "https",
  84. .host = "example.org",
  85. .port = NULL,
  86. .path = "",
  87. .fragment = NULL,
  88. },
  89. NULL
  90. );
  91. err += t(
  92. "https://example.org:1337/",
  93. &(struct URI){
  94. .scheme = "https",
  95. .host = "example.org",
  96. .port = "1337",
  97. .path = "/",
  98. .fragment = NULL,
  99. },
  100. NULL
  101. );
  102. err += t(
  103. "https://[fe80::feed:face]:1337/",
  104. &(struct URI){
  105. .scheme = "https",
  106. .host = "fe80::feed:face",
  107. .port = "1337",
  108. .path = "/",
  109. .fragment = NULL,
  110. },
  111. NULL
  112. );
  113. if(!diag(plan == counter)) err++;
  114. return err;
  115. }