2022-01-04 12:21:08 +03:00
|
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-08-01 16:01:03 +03:00
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
import net.urllib
|
|
|
|
|
|
2019-08-12 09:48:34 +03:00
|
|
|
|
fn test_net_urllib() {
|
2019-08-01 16:01:03 +03:00
|
|
|
|
test_query := 'Hellö Wörld@vlang'
|
|
|
|
|
assert urllib.query_escape(test_query) == 'Hell%C3%B6+W%C3%B6rld%40vlang'
|
|
|
|
|
|
|
|
|
|
test_url := 'https://joe:pass@www.mydomain.com:8080/som/url?param1=test1¶m2=test2&foo=bar#testfragment'
|
|
|
|
|
u := urllib.parse(test_url) or {
|
|
|
|
|
assert false
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-03-30 20:14:10 +03:00
|
|
|
|
assert u.scheme == 'https' && u.hostname() == 'www.mydomain.com' && u.port() == '8080'
|
|
|
|
|
&& u.path == '/som/url' && u.fragment == 'testfragment' && u.user.username == 'joe'
|
|
|
|
|
&& u.user.password == 'pass'
|
2019-08-01 16:01:03 +03:00
|
|
|
|
}
|
2019-11-06 22:35:56 +03:00
|
|
|
|
|
|
|
|
|
fn test_str() {
|
2021-03-30 20:14:10 +03:00
|
|
|
|
url := urllib.parse('https://en.wikipedia.org/wiki/Brazil_(1985_film)') or {
|
|
|
|
|
panic('unable to parse URL')
|
2019-11-06 22:35:56 +03:00
|
|
|
|
}
|
|
|
|
|
assert url.str() == 'https://en.wikipedia.org/wiki/Brazil_(1985_film)'
|
2021-01-18 15:20:06 +03:00
|
|
|
|
}
|
2020-01-16 20:16:11 +03:00
|
|
|
|
|
|
|
|
|
fn test_escape_unescape() {
|
|
|
|
|
original := 'те ст: т\\%'
|
2021-01-18 15:20:06 +03:00
|
|
|
|
escaped := urllib.query_escape(original)
|
2020-01-16 20:16:11 +03:00
|
|
|
|
assert escaped == '%D1%82%D0%B5+%D1%81%D1%82%3A+%D1%82%5C%25'
|
2021-03-30 20:14:10 +03:00
|
|
|
|
unescaped := urllib.query_unescape(escaped) or {
|
|
|
|
|
assert false
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-01-16 20:16:11 +03:00
|
|
|
|
assert unescaped == original
|
|
|
|
|
}
|
2021-03-30 20:14:10 +03:00
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse_query() {
|
2023-01-10 00:47:03 +03:00
|
|
|
|
q1 := urllib.parse_query('format=%22%25l%3A+%25c+%25t%22')!
|
|
|
|
|
q2 := urllib.parse_query('format="%l:+%c+%t"')!
|
2021-03-30 20:14:10 +03:00
|
|
|
|
// dump(q1)
|
|
|
|
|
// dump(q2)
|
2023-03-14 09:44:40 +03:00
|
|
|
|
assert q1.get('format')? == '"%l: %c %t"'
|
|
|
|
|
assert q2.get('format')? == '"%l: %c %t"'
|
2022-02-09 18:36:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse_query_orders() {
|
2023-01-10 00:47:03 +03:00
|
|
|
|
query_one := urllib.parse_query('https://someapi.com/endpoint?gamma=zalibaba&tau=1&alpha=alibaba&signature=alibaba123')!
|
2022-02-09 18:36:12 +03:00
|
|
|
|
qvalues := query_one.values()
|
|
|
|
|
assert qvalues == ['zalibaba', '1', 'alibaba', 'alibaba123']
|
2021-03-30 20:14:10 +03:00
|
|
|
|
}
|
2021-06-02 16:12:27 +03:00
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse_missing_host() {
|
2021-06-02 16:12:27 +03:00
|
|
|
|
// issue #10311
|
2023-01-10 00:47:03 +03:00
|
|
|
|
url := urllib.parse('http:///')!
|
2021-06-02 16:12:27 +03:00
|
|
|
|
assert url.str() == 'http://///'
|
|
|
|
|
}
|
2022-02-09 18:36:12 +03:00
|
|
|
|
|
|
|
|
|
// testing the case where the key as a null value
|
|
|
|
|
// e.g ?key=
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse_none_value() {
|
2023-01-10 00:47:03 +03:00
|
|
|
|
query_one := urllib.parse_query('gamma=zalibaba&tau=1&alpha=alibaba&signature=')!
|
2022-02-09 18:36:12 +03:00
|
|
|
|
qvalues := query_one.values()
|
|
|
|
|
qvalues_map := query_one.to_map()
|
|
|
|
|
assert qvalues == ['zalibaba', '1', 'alibaba']
|
|
|
|
|
assert qvalues_map == {
|
|
|
|
|
'gamma': ['zalibaba']
|
|
|
|
|
'tau': ['1']
|
|
|
|
|
'alpha': ['alibaba']
|
|
|
|
|
'signature': ['']
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testing the case where the query as empity value
|
|
|
|
|
// e.g https://www.vlang.dev?alibaba
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse_empty_query_one() {
|
2022-02-09 18:36:12 +03:00
|
|
|
|
query_str := 'alibaba'
|
2023-01-10 00:47:03 +03:00
|
|
|
|
query_one := urllib.parse_query(query_str)!
|
2022-02-09 18:36:12 +03:00
|
|
|
|
qvalues := query_one.values()
|
|
|
|
|
qvalues_map := query_one.to_map()
|
|
|
|
|
query_encode := query_one.encode()
|
|
|
|
|
assert qvalues == []
|
|
|
|
|
assert qvalues_map == {
|
|
|
|
|
'alibaba': ['']
|
|
|
|
|
}
|
|
|
|
|
assert query_str == query_encode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testing the case where the query as empity value
|
|
|
|
|
// e.g https://www.vlang.dev?
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse_empty_query_two() {
|
2022-02-09 18:36:12 +03:00
|
|
|
|
query_str := ''
|
2023-01-10 00:47:03 +03:00
|
|
|
|
query_one := urllib.parse_query(query_str)!
|
2022-02-09 18:36:12 +03:00
|
|
|
|
qvalues := query_one.values()
|
|
|
|
|
qvalues_map := query_one.to_map()
|
|
|
|
|
query_encode := query_one.encode()
|
|
|
|
|
assert qvalues == []
|
|
|
|
|
assert qvalues_map == {}
|
|
|
|
|
assert query_str == query_encode
|
|
|
|
|
}
|
2022-05-20 02:58:58 +03:00
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
|
fn test_parse() {
|
2022-05-20 02:58:58 +03:00
|
|
|
|
urls := [
|
|
|
|
|
'jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true',
|
|
|
|
|
'ftp://ftp.is.co.za/rfc/rfc1808.txt',
|
|
|
|
|
'http://www.ietf.org/rfc/rfc2396.txt#header1',
|
|
|
|
|
'ldap://[2001:db8::7]/c=GB?objectClass=one&objectClass=two',
|
|
|
|
|
'mailto:John.Doe@example.com',
|
|
|
|
|
'news:comp.infosystems.www.servers.unix',
|
|
|
|
|
'tel:+1-816-555-1212',
|
|
|
|
|
'telnet://192.0.2.16:80/',
|
|
|
|
|
'urn:oasis:names:specification:docbook:dtd:xml:4.1.2',
|
|
|
|
|
'foo://example.com:8042/over/there?name=ferret#nose',
|
2022-08-12 17:50:38 +03:00
|
|
|
|
'ftp://2001:0db8:85a3:0000:0000:8a2e:0370:7334/path/file.txt',
|
|
|
|
|
'ws://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:4000',
|
2022-05-20 02:58:58 +03:00
|
|
|
|
]
|
|
|
|
|
for url in urls {
|
|
|
|
|
_ := urllib.parse(url) or {
|
2022-05-20 08:49:51 +03:00
|
|
|
|
eprintln(err)
|
2022-05-20 02:58:58 +03:00
|
|
|
|
assert false
|
2022-05-20 08:49:51 +03:00
|
|
|
|
return
|
2022-05-20 02:58:58 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|