aboutsummaryrefslogtreecommitdiff
path: root/t/01-grammar.t
blob: cf4e51f76f5234dad6a48217c94e552587603c29 (plain)
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
#! /usr/bin/env perl6

use v6.d;

use URL::Grammar;
use Test;

plan 3;

subtest "https://www.tyil.nl", {
	plan 8;

	my $match = URL::Grammar.parse("https://www.tyil.nl");

	is ~$match<scheme>, "https", "Scheme is 'https'";
	nok $match<userinfo><username>, "URL contains no username";
	nok $match<userinfo><password>, "URL contains no password";
	is ~$match<host><hostname>, "www.tyil.nl", "Hostname is 'www.tyil.nl'";
	nok $match<host><port>, "URL contains no port";
	nok $match<path>, "URL contains no path";
	nok $match<query>, "URL contains no query";
	nok $match<fragment>, "URL contains no fragment";
};

subtest "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", {
	plan 8;

	my $match = URL::Grammar.parse("https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module");

	is ~$match<scheme>, "https", "Scheme is 'https'";
	is ~$match<userinfo><username>, "tyil", "Username is 'tyil'";
	is ~$match<userinfo><password>, "donthackme", "Password is 'donthackme'";
	is ~$match<host><hostname>, "www.tyil.nl", "Hostname is 'www.tyil.nl'";
	is +$match<host><port>, 8443, "Port is 8443";
	is ~$match<fragment>, "module", "Fragment is 'module'";

	subtest "path", {
		plan 4;

		my @path = $match<path><part>;

		is @path.elems, 3, "Path consists of 3 parts";
		is @path[0], "a", "Part 0 is 'a'";
		is @path[1], "path", "Part 1 is 'path'";
		is @path[2], "part", "Part 2 is 'part'";
	}

	subtest "query", {
		plan 5;

		my @query = $match<query><part>;

		is @query.elems, 2, "Query consists of 2 parts";
		is @query[0]<key>, "foo", "Part 0's key is 'foo'";
		is @query[0]<value>, "bar", "Part 0's value is 'bar'";
		is @query[1]<key>, "perl", "Part 1's key is 'perl'";
		is @query[1]<value>, "6", "Part 1's value is '6'";
	}
}

subtest 'https://custom.endpoint/foo/bar/', {
	plan 11;

	my $match = URL::Grammar.parse('https://custom.endpoint/foo/bar/');

	is ~$match<scheme>, "https", "Scheme is 'https'";
	nok $match<userinfo><username>, 'URL contains no username';
	nok $match<userinfo><password>, 'URL contains no password';
	is ~$match<host><hostname>, 'custom.endpoint', "Hostname is 'custom.endpoint'";
	nok $match<host><port>, 'URL contains no port';
	ok $match<path>, 'URL contains a path';
	is $match<path><part>.elems, 2, 'Path contains 2 elements';
	is ~$match<path><part>[0], 'foo', 'Path part 0 is "foo"';
	is ~$match<path><part>[1], 'bar', 'Path part 0 is "bar"';
	nok $match<query>, 'URL contains no query';
	nok $match<fragment>, 'URL contains no fragment';
}

# vim: ft=perl6 noet