url_test.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/string.h>
  7. #include <bx/url.h>
  8. struct UrlTest
  9. {
  10. bool result;
  11. const char* url;
  12. const char* tokens[bx::UrlView::Count];
  13. };
  14. static const UrlTest s_urlTest[] =
  15. {
  16. { true
  17. , "scheme://username:[email protected]:80/this/is/path/index.php?query=\"value\"#fragment",
  18. { "scheme", "username", "password", "host.rs", "80", "/this/is/path/index.php", "query=\"value\"", "fragment" }
  19. },
  20. { true
  21. , "scheme://host.rs/",
  22. { "scheme", "", "", "host.rs", "", "/", "", "" },
  23. },
  24. { true
  25. , "scheme://host.rs:1389/",
  26. { "scheme", "", "", "host.rs", "1389", "/", "", "" },
  27. },
  28. { true
  29. , "host.rs/abvgd.html",
  30. { "", "", "", "host.rs", "", "/abvgd.html", "", "" },
  31. },
  32. { true
  33. , "https://192.168.0.1:8080/",
  34. { "https", "", "", "192.168.0.1", "8080", "/", "", "" },
  35. },
  36. { true
  37. , "file:///d:/tmp/archive.tar.gz",
  38. { "file", "", "", "", "", "/d:/tmp/archive.tar.gz", "", "" },
  39. },
  40. };
  41. TEST_CASE("tokenizeUrl", "[url][string]")
  42. {
  43. bx::UrlView url;
  44. for (uint32_t ii = 0; ii < BX_COUNTOF(s_urlTest); ++ii)
  45. {
  46. const UrlTest& urlTest = s_urlTest[ii];
  47. bool result = url.parse(urlTest.url);
  48. REQUIRE(urlTest.result == result);
  49. if (result)
  50. {
  51. for (uint32_t token = 0; token < bx::UrlView::Count; ++token)
  52. {
  53. // char tmp[1024];
  54. // strCopy(tmp, BX_COUNTOF(tmp), url.get(bx::UrlView::Enum(token)) );
  55. // printf("`%s`, expected: `%s`\n", tmp, urlTest.tokens[token]);
  56. REQUIRE(0 == bx::strCmp(urlTest.tokens[token], url.get(bx::UrlView::Enum(token)) ) );
  57. }
  58. }
  59. }
  60. }