url.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <catch.hpp>
  17. #include <alibabacloud/core/Url.h>
  18. using namespace AlibabaCloud;
  19. TEST_CASE("testUrl")
  20. {
  21. const std::string src = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  22. Url url;
  23. url.setScheme("abc");
  24. url.setUserName("username");
  25. url.setPassword("password");
  26. url.setHost("example.com");
  27. url.setPath("/path/data");
  28. url.setPort(123);
  29. url.setQuery("key=value&key2=value2");
  30. url.setFragment("fragid1");
  31. REQUIRE(url.toString() == src);
  32. url.clear();
  33. url.fromString(src);
  34. REQUIRE(url.scheme() == "abc");
  35. REQUIRE(url.userName() == "username");
  36. REQUIRE(url.password() == "password");
  37. REQUIRE(url.host() == "example.com");
  38. REQUIRE(url.path() == "/path/data");
  39. REQUIRE(url.port() == 123);
  40. REQUIRE(url.query() == "key=value&key2=value2");
  41. REQUIRE(url.fragment() == "fragid1");
  42. Url newurl = url;
  43. REQUIRE(newurl == url);
  44. }
  45. TEST_CASE("testUrl2")
  46. {
  47. const std::string src = "http://oss.example.com";
  48. Url url(src);
  49. REQUIRE(url.scheme() == "http");
  50. REQUIRE(url.userName() == "");
  51. REQUIRE(url.password() == "");
  52. REQUIRE(url.host() == "oss.example.com");
  53. REQUIRE(url.path() == "/");
  54. REQUIRE(url.port() == -1);
  55. REQUIRE(url.query() == "");
  56. REQUIRE(url.fragment() == "");
  57. }