uri_ut.h 5.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "uri.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. namespace NUri {
  5. struct TTest {
  6. TStringBuf Val;
  7. TParseFlags Flags;
  8. TState::EParsed State;
  9. TStringBuf Scheme;
  10. TStringBuf User;
  11. TStringBuf Pass;
  12. TStringBuf Host;
  13. ui16 Port;
  14. TStringBuf Path;
  15. TStringBuf Query;
  16. TStringBuf Frag;
  17. TStringBuf HashBang;
  18. };
  19. }
  20. #define URL_MSG(url1, url2, cmp) \
  21. (TString("[") + url1.PrintS() + ("] " cmp " [") + url2.PrintS() + "]")
  22. #define URL_EQ(url1, url2) \
  23. UNIT_ASSERT_EQUAL_C(url, url2, URL_MSG(url1, url2, "!="))
  24. #define URL_NEQ(url1, url2) \
  25. UNIT_ASSERT_UNEQUAL_C(url, url2, URL_MSG(url1, url2, "=="))
  26. #define CMP_FLD(url, test, fld) \
  27. UNIT_ASSERT_VALUES_EQUAL(url.GetField(TField::Field##fld), test.fld)
  28. #define CMP_URL(url, test) \
  29. do { \
  30. CMP_FLD(url, test, Scheme); \
  31. CMP_FLD(url, test, User); \
  32. CMP_FLD(url, test, Pass); \
  33. CMP_FLD(url, test, Host); \
  34. UNIT_ASSERT_VALUES_EQUAL(url.GetPort(), test.Port); \
  35. CMP_FLD(url, test, Path); \
  36. CMP_FLD(url, test, Query); \
  37. CMP_FLD(url, test, Frag); \
  38. CMP_FLD(url, test, HashBang); \
  39. } while (false)
  40. #define URL_TEST_ENC(url, test, enc) \
  41. do { \
  42. TState::EParsed st = url.ParseUri(test.Val, test.Flags, 0, enc); \
  43. UNIT_ASSERT_VALUES_EQUAL(st, test.State); \
  44. CMP_URL(url, test); \
  45. if (TState::ParsedOK != st) \
  46. break; \
  47. TUri _url; \
  48. TString urlstr, urlstr2; \
  49. urlstr = url.PrintS(); \
  50. TState::EParsed st2 = _url.ParseUri(urlstr, \
  51. (test.Flags & ~TFeature::FeatureNoRelPath) | TFeature::FeatureAllowRootless, 0, enc); \
  52. if (TState::ParsedEmpty != st2) \
  53. UNIT_ASSERT_VALUES_EQUAL(st2, test.State); \
  54. urlstr2 = _url.PrintS(); \
  55. UNIT_ASSERT_VALUES_EQUAL(urlstr, urlstr2); \
  56. CMP_URL(_url, test); \
  57. UNIT_ASSERT_VALUES_EQUAL(url.GetUrlFieldMask(), _url.GetUrlFieldMask()); \
  58. URL_EQ(url, _url); \
  59. const TStringBuf hostascii = url.GetField(TField::FieldHostAscii); \
  60. if (hostascii.Empty()) \
  61. break; \
  62. urlstr = url.PrintS(TField::FlagHostAscii); \
  63. st2 = _url.ParseUri(urlstr, \
  64. (test.Flags & ~TFeature::FeatureNoRelPath) | TFeature::FeatureAllowRootless, 0, enc); \
  65. UNIT_ASSERT_VALUES_EQUAL(st2, test.State); \
  66. urlstr2 = _url.PrintS(); \
  67. UNIT_ASSERT_VALUES_EQUAL(urlstr, urlstr2); \
  68. TTest test2 = test; \
  69. test2.Host = hostascii; \
  70. CMP_URL(_url, test2); \
  71. UNIT_ASSERT_VALUES_EQUAL(url.GetUrlFieldMask(), _url.GetUrlFieldMask()); \
  72. } while (false)
  73. #define URL_TEST(url, test) \
  74. URL_TEST_ENC(url, test, CODES_UTF8)