| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include <catch2/catch.hpp>
- #include <iostream>
- #include <gul/uri.h>
- using URI = gul::uri;
- SCENARIO("regex test 1")
- {
- // userinfo host port
- // ┌──┴───┐ ┌──────┴──────┐ ┌┴┐
- std::string str = "https://[email protected]:123/forum/questions/?tag=networking&order=newest#top";
- //└─┬─┘ └───────────┬──────────────┘└───────┬───────┘ └───────────┬─────────────┘ └┬┘
- //scheme authority path query fragment
- URI uri(str);
- REQUIRE( uri.scheme == "https");
- REQUIRE( uri.user == "john.doe");
- REQUIRE( uri.host == "www.example.com");
- REQUIRE( uri.password == "");
- REQUIRE( uri.port == "123");
- REQUIRE( uri.path == "/forum/questions/");
- REQUIRE( uri.query == "tag=networking&order=newest");
- REQUIRE( uri.fragment == "top");
- REQUIRE( uri.toString() == str);
- }
- SCENARIO("regex test 2")
- {
- std::string str = "ldap://[2001:db8::7]/c=GB?objectClass?one";
- // └┬─┘ └─────┬─────┘└─┬─┘ └──────┬──────┘
- // scheme authority path query
- URI uri(str);
- REQUIRE( uri.scheme == "ldap");
- REQUIRE( uri.user == "");
- REQUIRE( uri.host == "[2001:db8::7]");
- REQUIRE( uri.password == "");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "/c=GB");
- REQUIRE( uri.query == "objectClass?one");
- REQUIRE( uri.fragment == "");
- REQUIRE( uri.toString() == str);
- }
- SCENARIO("regex test 3")
- {
- std::string str = "urn:oasis:names:specification:docbook:dtd:xml:4.1.2";
- // └┬┘ └──────────────────────┬──────────────────────┘
- // scheme path
- URI uri(str);
- REQUIRE( uri.scheme == "urn");
- REQUIRE( uri.user == "");
- REQUIRE( uri.host == "");
- REQUIRE( uri.password == "");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "oasis:names:specification:docbook:dtd:xml:4.1.2");
- REQUIRE( uri.query == "");
- REQUIRE( uri.fragment == "");
- REQUIRE( uri.toString() == str);
- }
- SCENARIO("regex test 4")
- {
- std::string str = "data:application/octet-stream;base64,AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA";
- // └┬┘ └──────────────────────┬───────────────────────────────────────┘
- // scheme path
- URI uri(str);
- REQUIRE( uri.scheme == "data");
- REQUIRE( uri.user == "");
- REQUIRE( uri.host == "");
- REQUIRE( uri.password == "");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "application/octet-stream;base64,AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA");
- REQUIRE( uri.query == "");
- REQUIRE( uri.fragment == "");
- REQUIRE( uri.toString() == str);
- }
- SCENARIO("regex test 5")
- {
- std::string str = "file:/test.txt";
- // └┬┘ └──────────────────────┬───────────────────────────────────────┘
- // scheme path
- URI uri(str);
- REQUIRE( uri.scheme == "file");
- REQUIRE( uri.user == "");
- REQUIRE( uri.host == "");
- REQUIRE( uri.password == "");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "/test.txt");
- REQUIRE( uri.query == "");
- REQUIRE( uri.fragment == "");
- REQUIRE( uri.toString() == str);
- }
- SCENARIO("regex test 6: ssh absolute path")
- {
- std::string str = "ssh://username:password@localhost/home/user";
- // └┬┘ └──────────────────────┬───────────────────────────────────────┘
- // scheme path
- URI uri(str);
- REQUIRE( uri.scheme == "ssh");
- REQUIRE( uri.user == "username");
- REQUIRE( uri.password == "password");
- REQUIRE( uri.host == "localhost");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "/home/user");
- REQUIRE( uri.query == "");
- REQUIRE( uri.fragment == "");
- REQUIRE( uri.toString() == str);
- }
- SCENARIO("regex test 7: testing paths and authority")
- {
- {
- std::string str = "file:///this.txt";
- URI uri(str);
- REQUIRE( uri.scheme == "file");
- REQUIRE( uri.getAuthority() == "");
- REQUIRE( uri.user == "");
- REQUIRE( uri.password == "");
- REQUIRE( uri.host == "");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "/this.txt");
- REQUIRE( uri.query == "");
- REQUIRE( uri.fragment == "");
- }
- // This is an invalid
- {
- std::string str = "file://this.txt";
- URI uri(str);
- REQUIRE( uri.scheme == "file");
- REQUIRE( uri.getAuthority() == "this.txt");
- REQUIRE( uri.user == "");
- REQUIRE( uri.password == "");
- REQUIRE( uri.host == "this.txt");
- REQUIRE( uri.port == "");
- REQUIRE( uri.path == "");
- REQUIRE( uri.query == "");
- REQUIRE( uri.fragment == "");
- }
- }
- SCENARIO("Data URI")
- {
- //data:<mediatype>[;base64],<data>
- std::string str = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==";
- URI uri(str);
- REQUIRE( uri.scheme == "data");
- REQUIRE( uri.path == "text/plain;base64,SGVsbG8sIFdvcmxkIQ==");
- THEN("We can get string views to parts of the path")
- {
- REQUIRE( uri.getMediaEncoding() == "text/plain;base64");
- REQUIRE( uri.getMediaType() == "base64");
- REQUIRE( uri.getMediaData() == "SGVsbG8sIFdvcmxkIQ==");
- }
- }
|