File: test_uri.pl

package info (click to toggle)
swi-prolog 7.2.3%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,180 kB
  • ctags: 45,684
  • sloc: ansic: 330,358; perl: 268,104; sh: 6,795; java: 4,904; makefile: 4,561; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (144 lines) | stat: -rw-r--r-- 5,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
:- module(test_uri,
	  [ test_uri/0
	  ]).

:- asserta(user:file_search_path(library, '../plunit')).
:- asserta(user:file_search_path(library, '.')).
:- asserta(user:file_search_path(foreign, '.')).

:- use_module(library(uri)).
:- use_module(library(debug)).
:- use_module(library(plunit)).

test_uri :-
	run_tests([ uri,
		    iri,
		    uri_authority,
		    uri_query,
		    uri_encode
		  ]).

trip_uri_iri(IRI, X) :-
	uri_iri(URI, IRI),
	uri_iri(URI, X).

resolve(In, Out) :-
	uri_resolve(In, 'http://a/b/c/d;p?q', Out).

:- begin_tests(uri).

test(unicode_trip, X == IRI) :-
	IRI = 'http://a.b/\u041a',
	trip_uri_iri(IRI, X).
test(unicode_uri, IRI == URI) :-
	URI = 'http://a.b/\u041a',
	uri_iri(URI, IRI).
test(latin_uri, IRI == URI) :-
	URI = 'http://a.b/\u00a8',
	uri_iri(URI, IRI).

test(resolve, URI == 'g:h')		     :-	resolve('g:h', URI).
test(resolve, URI == 'http://a/b/c/g')	     :-	resolve('g', URI).
test(resolve, URI == 'http://a/b/c/g')	     :-	resolve('./g', URI).
test(resolve, URI == 'http://a/b/c/g/')	     :-	resolve('g/', URI).
test(resolve, URI == 'http://a/g')	     :-	resolve('/g', URI).
test(resolve, URI == 'http://g')	     :-	resolve('//g', URI).
test(resolve, URI == 'http://a/b/c/d;p?y')   :-	resolve('?y', URI).
test(resolve, URI == 'http://a/b/c/g?y')     :-	resolve('g?y', URI).
test(resolve, URI == 'http://a/b/c/d;p?q#s') :-	resolve('#s', URI).
test(resolve, URI == 'http://a/b/c/g#s')     :-	resolve('g#s', URI).
test(resolve, URI == 'http://a/b/c/g?y#s')   :-	resolve('g?y#s', URI).
test(resolve, URI == 'http://a/b/c/;x')	     :-	resolve(';x', URI).
test(resolve, URI == 'http://a/b/c/g;x')     :-	resolve('g;x', URI).
test(resolve, URI == 'http://a/b/c/g;x?y#s') :-	resolve('g;x?y#s', URI).
test(resolve, URI == 'http://a/b/c/d;p?q')   :-	resolve('', URI).
test(resolve, URI == 'http://a/b/c/')	     :-	resolve('.', URI).
test(resolve, URI == 'http://a/b/c/')	     :-	resolve('./', URI).
test(resolve, URI == 'http://a/b/')	     :-	resolve('..', URI).
test(resolve, URI == 'http://a/b/')	     :-	resolve('../', URI).
test(resolve, URI == 'http://a/b/g')	     :-	resolve('../g', URI).
test(resolve, URI == 'http://a/')	     :-	resolve('../..', URI).
test(resolve, URI == 'http://a/')	     :-	resolve('../../', URI).
test(resolve, URI == 'http://a/g')	     :-	resolve('../../g', URI).

:- end_tests(uri).

:- begin_tests(iri).

test(normalise_uri, NormalURI == 'example://a/b/c/%7Bfoo%7D') :-
	uri_normalized('eXAMPLE://a/./b/../b/%63/%7bfoo%7d', NormalURI).
test(normalise_iri, NormalIRI == 'example://a/b/c/%7Bfoo%7D') :-
	uri_normalized_iri('eXAMPLE://a/./b/../b/%63/%7bfoo%7d', NormalIRI).
test(normalise_iri, NormalIRI == 'http://a.b/a%3F?x') :-	% 3F = '?'
	uri_normalized_iri('http://a.b/a%3f?x', NormalIRI).
test(normalise_iri, NormalIRI == 'http://a.b/a?x=1&y=2') :-
	uri_normalized_iri('http://a.b/a?x=1&y=2', NormalIRI).
test(normalise_iri, NormalIRI == 'http://a.b/a?x=1&y=2#aap') :-
	uri_normalized_iri('http://a.b/a?x=1&y=2#aap', NormalIRI).
test(normalise_iri, NormalIRI == 'http://a.b/a?x=1&y=2#aap%20noot') :-
	uri_normalized_iri('http://a.b/a?x=1&y=2#aap+noot', NormalIRI).
test(normalise_iri, NormalIRI == 'http://a.b:3020/') :-
	uri_normalized_iri('http://a.b:3020/', NormalIRI).

:- end_tests(iri).

:- begin_tests(uri_query).

test(break, Q == [a=b,c=d]) :-
	uri_query_components('a=b&c=d', Q).
test(construct, QS == 'a=b&c=d') :-
	uri_query_components(QS, [a=b,c=d]).
test(encode, Q == [name=Value]) :-
	numlist(1, 1050, VL),
	atom_codes(Value, VL),
	uri_query_components(QS, [name=Value]),
	uri_query_components(QS, Q).

:- end_tests(uri_query).

:- begin_tests(uri_authority).

test(break, [User,Host,Port] == [jan,'swi-prolog.org', 3040]) :-
	uri_authority_components('jan@swi-prolog.org:3040', C),
	uri_authority_data(user, C, User),
	uri_authority_data(host, C, Host),
	uri_authority_data(port, C, Port).
test(break, [User,Pwd,Host,Port] == [jan,xxx,'swi-prolog.org', 3040]) :-
	uri_authority_components('jan:xxx@swi-prolog.org:3040', C),
	uri_authority_data(user, C, User),
	uri_authority_data(password, C, Pwd),
	uri_authority_data(host, C, Host),
	uri_authority_data(port, C, Port).
test(construct, Auth == 'jan@swi-prolog.org:3040') :-
	uri_authority_data(user, C, jan),
	uri_authority_data(host, C, 'swi-prolog.org'),
	uri_authority_data(port, C, 3040),
	uri_authority_components(Auth, C).

:- end_tests(uri_authority).

:- begin_tests(uri_encode).

test(query, X == '%3D%26') :-
	uri_encoded(query_value, '=&', X).
test(query, X == 'a%2Bb') :-
	uri_encoded(query_value, 'a+b', X).
test(query, X == 'a b') :-
	uri_encoded(query_value, X, 'a+b').
test(path, X == 'a+b') :-
	uri_encoded(path, 'a+b', X).
test(path, X == 'a+b') :-
	uri_encoded(path, X, 'a+b').
test(path, X == 'a%3Ab') :-
	uri_encoded(path, 'a:b', X).
test(path, X == '=&') :-
	uri_encoded(path, '=&', X).
test(path, X == '/a%20b%3F') :-
	uri_encoded(path, '/a b?', X).
test(path, X == '%25') :-
	uri_encoded(path, '%', X).
test(path, X == '%C3%B6%C3%A4%C3%BC%C3%B5') :-
	atom_codes(Path, [246, 228, 252, 245]),
	uri_encoded(path, Path, X).

:- end_tests(uri_encode).