2021-01-18 13:20:06 +01:00
|
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-08-01 15:01:03 +02: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 08:48:34 +02:00
|
|
|
|
fn test_net_urllib() {
|
2019-08-01 15:01:03 +02: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 19:14:10 +02: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 15:01:03 +02:00
|
|
|
|
}
|
2019-11-06 20:35:56 +01:00
|
|
|
|
|
|
|
|
|
fn test_str() {
|
2021-03-30 19:14:10 +02:00
|
|
|
|
url := urllib.parse('https://en.wikipedia.org/wiki/Brazil_(1985_film)') or {
|
|
|
|
|
panic('unable to parse URL')
|
2019-11-06 20:35:56 +01:00
|
|
|
|
}
|
|
|
|
|
assert url.str() == 'https://en.wikipedia.org/wiki/Brazil_(1985_film)'
|
2021-01-18 13:20:06 +01:00
|
|
|
|
}
|
2020-01-16 18:16:11 +01:00
|
|
|
|
|
|
|
|
|
fn test_escape_unescape() {
|
|
|
|
|
original := 'те ст: т\\%'
|
2021-01-18 13:20:06 +01:00
|
|
|
|
escaped := urllib.query_escape(original)
|
2020-01-16 18:16:11 +01:00
|
|
|
|
assert escaped == '%D1%82%D0%B5+%D1%81%D1%82%3A+%D1%82%5C%25'
|
2021-03-30 19:14:10 +02:00
|
|
|
|
unescaped := urllib.query_unescape(escaped) or {
|
|
|
|
|
assert false
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-01-16 18:16:11 +01:00
|
|
|
|
assert unescaped == original
|
|
|
|
|
}
|
2021-03-30 19:14:10 +02:00
|
|
|
|
|
|
|
|
|
fn test_parse_query() ? {
|
|
|
|
|
q1 := urllib.parse_query('format=%22%25l%3A+%25c+%25t%22') ?
|
|
|
|
|
q2 := urllib.parse_query('format="%l:+%c+%t"') ?
|
|
|
|
|
// dump(q1)
|
|
|
|
|
// dump(q2)
|
|
|
|
|
assert q1.data['format'].data == ['"%l: %c %t"']
|
|
|
|
|
assert q2.data['format'].data == ['"%l: %c %t"']
|
|
|
|
|
}
|