basic.test.c (3745B)
1 #include "url-parser.h" 2 #include "test.h" 3 4 void test_http_url() { 5 struct parsed_url *purl = parse_url("http://www.example.com/path/to/resource?query=value#fragment"); 6 ASSERT("http scheme", purl != NULL); 7 ASSERT_STRING_EQUALS("http", purl->scheme); 8 ASSERT_STRING_EQUALS("www.example.com", purl->host); 9 ASSERT_STRING_EQUALS(NULL, purl->port); 10 ASSERT_STRING_EQUALS("/path/to/resource", purl->path); 11 ASSERT_STRING_EQUALS("query=value", purl->query); 12 ASSERT_STRING_EQUALS("fragment", purl->fragment); 13 ASSERT_STRING_EQUALS(NULL, purl->username); 14 ASSERT_STRING_EQUALS(NULL, purl->password); 15 parsed_url_free(purl); 16 } 17 18 void test_https_with_port() { 19 struct parsed_url *purl = parse_url("https://www.example.com:8080/path"); 20 ASSERT("https with port", purl != NULL); 21 ASSERT_STRING_EQUALS("https", purl->scheme); 22 ASSERT_STRING_EQUALS("www.example.com", purl->host); 23 ASSERT_STRING_EQUALS("8080", purl->port); 24 ASSERT_STRING_EQUALS("/path", purl->path); 25 parsed_url_free(purl); 26 } 27 28 void test_ftp_with_credentials() { 29 struct parsed_url *purl = parse_url("ftp://user:password@ftp.example.com/file.txt"); 30 ASSERT("ftp with credentials", purl != NULL); 31 ASSERT_STRING_EQUALS("ftp", purl->scheme); 32 ASSERT_STRING_EQUALS("ftp.example.com", purl->host); 33 ASSERT_STRING_EQUALS(NULL, purl->port); 34 ASSERT_STRING_EQUALS("/file.txt", purl->path); 35 ASSERT_STRING_EQUALS("user", purl->username); 36 ASSERT_STRING_EQUALS("password", purl->password); 37 parsed_url_free(purl); 38 } 39 40 void test_http_ipv6() { 41 struct parsed_url *purl = parse_url("http://[::1]:8080/path"); 42 ASSERT("http IPv6", purl != NULL); 43 ASSERT_STRING_EQUALS("http", purl->scheme); 44 ASSERT_STRING_EQUALS("::1", purl->host); 45 ASSERT_STRING_EQUALS("8080", purl->port); 46 ASSERT_STRING_EQUALS("/path", purl->path); 47 parsed_url_free(purl); 48 } 49 50 void test_query_only() { 51 struct parsed_url *purl = parse_url("http://example.com?foo=bar"); 52 ASSERT("query only", purl != NULL); 53 ASSERT_STRING_EQUALS("http", purl->scheme); 54 ASSERT_STRING_EQUALS("example.com", purl->host); 55 ASSERT_STRING_EQUALS(NULL, purl->port); 56 ASSERT_STRING_EQUALS(NULL, purl->path); 57 ASSERT_STRING_EQUALS("foo=bar", purl->query); 58 parsed_url_free(purl); 59 } 60 61 void test_fragment_only() { 62 struct parsed_url *purl = parse_url("http://example.com#section"); 63 ASSERT("fragment only", purl != NULL); 64 ASSERT_STRING_EQUALS("http", purl->scheme); 65 ASSERT_STRING_EQUALS("example.com", purl->host); 66 ASSERT_STRING_EQUALS(NULL, purl->port); 67 ASSERT_STRING_EQUALS(NULL, purl->path); 68 ASSERT_STRING_EQUALS(NULL, purl->query); 69 ASSERT_STRING_EQUALS("section", purl->fragment); 70 parsed_url_free(purl); 71 } 72 73 void test_no_path() { 74 struct parsed_url *purl = parse_url("http://www.example.com"); 75 ASSERT("no path", purl != NULL); 76 ASSERT_STRING_EQUALS("http", purl->scheme); 77 ASSERT_STRING_EQUALS("www.example.com", purl->host); 78 ASSERT_STRING_EQUALS(NULL, purl->port); 79 ASSERT_STRING_EQUALS(NULL, purl->path); 80 parsed_url_free(purl); 81 } 82 83 void test_username_only() { 84 struct parsed_url *purl = parse_url("ftp://user@ftp.example.com/file"); 85 ASSERT("username only", purl != NULL); 86 ASSERT_STRING_EQUALS("ftp", purl->scheme); 87 ASSERT_STRING_EQUALS("ftp.example.com", purl->host); 88 ASSERT_STRING_EQUALS("user", purl->username); 89 ASSERT_STRING_EQUALS(NULL, purl->password); 90 parsed_url_free(purl); 91 } 92 93 int main() { 94 RUN(test_http_url); 95 RUN(test_https_with_port); 96 RUN(test_ftp_with_credentials); 97 RUN(test_http_ipv6); 98 RUN(test_query_only); 99 RUN(test_fragment_only); 100 RUN(test_no_path); 101 RUN(test_username_only); 102 return TEST_REPORT(); 103 }