2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include <test/test_ext_url.h>
18 #include <runtime/ext/ext_url.h>
19 #include <system/lib/systemlib.h>
21 ///////////////////////////////////////////////////////////////////////////////
23 bool TestExtUrl::RunTests(const std::string
&which
) {
26 RUN_TEST(test_base64_decode
);
27 RUN_TEST(test_base64_encode
);
28 RUN_TEST(test_get_headers
);
29 RUN_TEST(test_get_meta_tags
);
30 RUN_TEST(test_http_build_query
);
31 RUN_TEST(test_parse_url
);
32 RUN_TEST(test_rawurldecode
);
33 RUN_TEST(test_rawurlencode
);
34 RUN_TEST(test_urldecode
);
35 RUN_TEST(test_urlencode
);
40 ///////////////////////////////////////////////////////////////////////////////
42 bool TestExtUrl::test_base64_decode() {
43 VS(f_base64_decode("VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=="),
44 "This is an encoded string");
45 VERIFY(same(f_base64_decode("BgAYdjk="),
46 String("\006\0\030v9", 5, AttachLiteral
)));
47 VERIFY(!same(f_base64_decode("dGVzdA=="),
48 f_base64_decode("dGVzdA==CORRUPT")));
52 bool TestExtUrl::test_base64_encode() {
53 VS(f_base64_encode("This is an encoded string"),
54 "VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==");
55 VS(f_base64_encode(String("\006\0\030v9", 5, AttachLiteral
)), "BgAYdjk=");
59 bool TestExtUrl::test_get_headers() {
60 String url
= "http://www.example.com";
61 Array ret
= f_get_headers(url
);
62 //VS(ret[0], "HTTP/1.1 200 OK");
63 VERIFY(ret
.size() > 0);
64 ret
= f_get_headers(url
, 1);
65 //VS(ret["Connection"], "close");
66 VERIFY(!ret
["Connection"].toString().empty());
70 bool TestExtUrl::test_get_meta_tags() {
71 Array ret
= f_get_meta_tags("test/test_get_meta_tags.html");
73 VS(ret
["author"], "name");
74 VS(ret
["keywords"], "php documentation");
75 VS(ret
["description"], "a php manual");
76 VS(ret
["geo_position"], "49.33;-86.59");
80 bool TestExtUrl::test_http_build_query() {
82 Array data
= CREATE_MAP4("foo", "bar", "baz", "boom", "cow", "milk",
83 "php", "hypertext processor");
84 VS(f_http_build_query(data
),
85 "foo=bar&baz=boom&cow=milk&php=hypertext+processor");
86 VS(f_http_build_query(data
, "", "&"),
87 "foo=bar&baz=boom&cow=milk&php=hypertext+processor");
90 Array data
= Array(ArrayInit(6).
96 set("php", "hypertext processor").
98 VS(f_http_build_query(data
),
99 "0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor");
100 VS(f_http_build_query(data
, "myvar_"),
101 "myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&"
102 "php=hypertext+processor");
105 Array data
= Array(ArrayInit(4).
107 CREATE_MAP4("name", "Bob Smith",
110 "dob", "5/12/1956")).
112 CREATE_VECTOR4("golf", "opera", "poker", "rap")).
114 CREATE_MAP2("bobby", CREATE_MAP2("age",12,"sex","M"),
115 "sally", CREATE_MAP2("age", 8,"sex","F"))).
119 VS(f_http_build_query(data
, "flags_"),
120 "user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&"
121 "user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&"
122 "pastimes%5B1%5D=opera&pastimes%5B2%5D=poker&"
123 "pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&"
124 "children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&"
125 "children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO");
128 Object
obj(SystemLib::AllocStdClassObject());
129 obj
->o_set("foo", "bar");
130 obj
->o_set("baz", "boom");
131 VS(f_http_build_query(obj
), "foo=bar&baz=boom");
136 bool TestExtUrl::test_parse_url() {
137 String url
= "http://username:password@hostname/path?arg=value#anchor";
138 VS(f_print_r(f_parse_url(url
), true),
141 " [scheme] => http\n"
142 " [host] => hostname\n"
143 " [user] => username\n"
144 " [pass] => password\n"
146 " [query] => arg=value\n"
147 " [fragment] => anchor\n"
152 bool TestExtUrl::test_rawurldecode() {
153 VS(f_rawurldecode("foo%20bar%40baz"), "foo bar@baz");
154 VS(f_rawurldecode("foo+bar%40baz"), "foo+bar@baz");
158 bool TestExtUrl::test_rawurlencode() {
159 VS(f_rawurlencode("foo bar@baz"), "foo%20bar%40baz");
163 bool TestExtUrl::test_urldecode() {
164 VS(f_urldecode("foo+bar%40baz"), "foo bar@baz");
168 bool TestExtUrl::test_urlencode() {
169 VS(f_urlencode("foo bar@baz"), "foo+bar%40baz");