url_builder_ut.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "url_builder.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NYql;
  4. Y_UNIT_TEST_SUITE(TUrlBuilder) {
  5. Y_UNIT_TEST(UriOnly) {
  6. TUrlBuilder builder("https://localhost/abc");
  7. UNIT_ASSERT_VALUES_EQUAL(builder.Build(), "https://localhost/abc");
  8. }
  9. Y_UNIT_TEST(Basic) {
  10. TUrlBuilder builder("https://localhost/abc");
  11. builder.AddUrlParam("param1", "val1");
  12. builder.AddUrlParam("param2", "val2");
  13. UNIT_ASSERT_VALUES_EQUAL(builder.Build(), "https://localhost/abc?param1=val1&param2=val2");
  14. }
  15. Y_UNIT_TEST(BasicWithEncoding) {
  16. auto url = TUrlBuilder("https://localhost/abc")
  17. .AddUrlParam("param1", "=!@#$%^&*(){}[]\" ")
  18. .AddUrlParam("param2", "val2")
  19. .Build();
  20. UNIT_ASSERT_VALUES_EQUAL(url, "https://localhost/abc?param1=%3D!@%23$%25%5E%26*%28%29%7B%7D%5B%5D%22+&param2=val2");
  21. }
  22. Y_UNIT_TEST(EmptyPathComponent) {
  23. TUrlBuilder builder("https://localhost/abc");
  24. UNIT_ASSERT_EXCEPTION_CONTAINS(builder.AddPathComponent(""), std::exception, "Empty path component is not allowed");
  25. auto url = builder.Build();
  26. // not changed
  27. UNIT_ASSERT_VALUES_EQUAL(url, "https://localhost/abc");
  28. }
  29. Y_UNIT_TEST(SeveralPathComponents) {
  30. auto url = TUrlBuilder("https://localhost/abc")
  31. .AddPathComponent("oops")
  32. .AddPathComponent("long oops")
  33. .AddUrlParam("param1", "val1")
  34. .AddUrlParam("param1", "long param")
  35. .Build();
  36. UNIT_ASSERT_VALUES_EQUAL(url, "https://localhost/abc/oops/long%20oops?param1=val1&param1=long+param");
  37. }
  38. Y_UNIT_TEST(SeveralPathComponentsWithSlashInBaseUri) {
  39. // base uri ends with '/'
  40. auto url = TUrlBuilder("https://localhost/abc/")
  41. .AddPathComponent("oops%1234")
  42. .AddPathComponent("long&oops=xxx")
  43. .AddUrlParam("param1", "a&b=cdef")
  44. .Build();
  45. UNIT_ASSERT_VALUES_EQUAL(url, "https://localhost/abc/oops%251234/long&oops=xxx?param1=a%26b%3Dcdef");
  46. }
  47. }