1 | /*
|
---|
2 | * Copyright (c) 2014 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <pcut/pcut.h>
|
---|
30 | #include <str.h>
|
---|
31 | #include "../uri.h"
|
---|
32 |
|
---|
33 | typedef struct {
|
---|
34 | const char *scheme;
|
---|
35 | const char *user_info;
|
---|
36 | const char *user_credential;
|
---|
37 | const char *host;
|
---|
38 | const char *port;
|
---|
39 | const char *path;
|
---|
40 | const char *query;
|
---|
41 | const char *fragment;
|
---|
42 | } const_uri_t;
|
---|
43 |
|
---|
44 | static const_uri_t expected_uri;
|
---|
45 | static uri_t *parsed_uri;
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * In the following macros we assume that the expected and the parsed
|
---|
49 | * URI are in the global variables declared above.
|
---|
50 | */
|
---|
51 |
|
---|
52 | #define CHECK(item) \
|
---|
53 | PCUT_ASSERT_STR_EQUALS_OR_NULL(expected_uri.item, parsed_uri->item)
|
---|
54 |
|
---|
55 | #define PARSE_AND_CHECK(the_uri) \
|
---|
56 | do { \
|
---|
57 | parsed_uri = uri_parse(the_uri); \
|
---|
58 | PCUT_ASSERT_NOT_NULL(parsed_uri); \
|
---|
59 | PCUT_ASSERT_INT_EQUALS(0, !uri_validate(parsed_uri)); \
|
---|
60 | CHECK(scheme); \
|
---|
61 | CHECK(user_info); \
|
---|
62 | CHECK(user_credential); \
|
---|
63 | CHECK(host); \
|
---|
64 | CHECK(port); \
|
---|
65 | CHECK(path); \
|
---|
66 | CHECK(query); \
|
---|
67 | CHECK(fragment); \
|
---|
68 | } while (0)
|
---|
69 |
|
---|
70 | PCUT_INIT;
|
---|
71 |
|
---|
72 | PCUT_TEST_SUITE(uri_parse);
|
---|
73 |
|
---|
74 | PCUT_TEST_BEFORE
|
---|
75 | {
|
---|
76 | parsed_uri = NULL;
|
---|
77 | expected_uri.scheme = NULL;
|
---|
78 | expected_uri.user_info = NULL;
|
---|
79 | expected_uri.user_credential = NULL;
|
---|
80 | expected_uri.host = NULL;
|
---|
81 | expected_uri.port = NULL;
|
---|
82 | expected_uri.path = "";
|
---|
83 | expected_uri.query = NULL;
|
---|
84 | expected_uri.fragment = NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | PCUT_TEST_AFTER
|
---|
88 | {
|
---|
89 | if (parsed_uri != NULL) {
|
---|
90 | uri_destroy(parsed_uri);
|
---|
91 | parsed_uri = NULL;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | PCUT_TEST(only_hostname)
|
---|
96 | {
|
---|
97 | expected_uri.scheme = "http";
|
---|
98 | expected_uri.host = "localhost";
|
---|
99 |
|
---|
100 | PARSE_AND_CHECK("http://localhost");
|
---|
101 | }
|
---|
102 |
|
---|
103 | PCUT_TEST(hostname_with_user)
|
---|
104 | {
|
---|
105 | expected_uri.scheme = "http";
|
---|
106 | expected_uri.host = "localhost";
|
---|
107 | expected_uri.user_info = "user";
|
---|
108 |
|
---|
109 | PARSE_AND_CHECK("http://user@localhost");
|
---|
110 | }
|
---|
111 |
|
---|
112 | PCUT_TEST(hostname_with_user_and_password)
|
---|
113 | {
|
---|
114 | expected_uri.scheme = "https";
|
---|
115 | expected_uri.host = "localhost";
|
---|
116 | expected_uri.user_info = "user";
|
---|
117 | expected_uri.user_credential = "password";
|
---|
118 |
|
---|
119 | PARSE_AND_CHECK("https://user:password@localhost");
|
---|
120 | }
|
---|
121 |
|
---|
122 | PCUT_TEST(path_specification)
|
---|
123 | {
|
---|
124 | expected_uri.scheme = "http";
|
---|
125 | expected_uri.host = "localhost";
|
---|
126 | expected_uri.path = "/alpha";
|
---|
127 |
|
---|
128 | PARSE_AND_CHECK("http://localhost/alpha");
|
---|
129 | }
|
---|
130 |
|
---|
131 | PCUT_TEST(with_fragment)
|
---|
132 | {
|
---|
133 | expected_uri.scheme = "http";
|
---|
134 | expected_uri.host = "localhost";
|
---|
135 | expected_uri.path = "/alpha";
|
---|
136 | expected_uri.fragment = "fragment-name";
|
---|
137 |
|
---|
138 | PARSE_AND_CHECK("http://localhost/alpha#fragment-name");
|
---|
139 | }
|
---|
140 |
|
---|
141 | PCUT_EXPORT(uri_parse);
|
---|