@@ -3,33 +3,112 @@ import 'package:test/test.dart';
3
3
4
4
void main () {
5
5
test ('uri generation' , () {
6
- final url = StandardUriDesign .pathOnly;
7
- expect (url .collection ('books' ).toString (), '/books' );
8
- expect (url .resource ('books' , '42' ).toString (), '/books/42' );
9
- expect (url .related ('books' , '42' , 'author' ).toString (), '/books/42/author' );
10
- expect (url .relationship ('books' , '42' , 'author' ).toString (),
6
+ final d = StandardUriDesign .pathOnly;
7
+ expect (d .collection ('books' ).toString (), '/books' );
8
+ expect (d .resource ('books' , '42' ).toString (), '/books/42' );
9
+ expect (d .related ('books' , '42' , 'author' ).toString (), '/books/42/author' );
10
+ expect (d .relationship ('books' , '42' , 'author' ).toString (),
11
11
'/books/42/relationships/author' );
12
12
});
13
13
14
14
test ('Authority is retained if exists in base' , () {
15
- final url = StandardUriDesign (Uri .parse ('https://example.com' ));
16
- expect (url .collection ('books' ).toString (), 'https://example.com/books' );
17
- expect (
18
- url. resource ( 'books' , '42' ). toString (), ' https://example.com/books/42' );
19
- expect (url .related ('books' , '42' , 'author' ).toString (),
20
- 'https://example.com/books/42/author' );
21
- expect (url .relationship ('books' , '42' , 'author' ).toString (),
22
- 'https://example.com/books/42/relationships/author' );
15
+ final d = StandardUriDesign (Uri .parse ('https://example.com:8080 ' ));
16
+ expect (d .collection ('books' ).toString (), 'https://example.com:8080 /books' );
17
+ expect (d. resource ( 'books' , '42' ). toString (),
18
+ ' https://example.com:8080 /books/42' );
19
+ expect (d .related ('books' , '42' , 'author' ).toString (),
20
+ 'https://example.com:8080 /books/42/author' );
21
+ expect (d .relationship ('books' , '42' , 'author' ).toString (),
22
+ 'https://example.com:8080 /books/42/relationships/author' );
23
23
});
24
24
25
- test ('Authority and path is retained if exists in base (directory path)' , () {
26
- final url = StandardUriDesign (Uri .parse ('https://example.com/foo/' ));
27
- expect (url .collection ('books' ).toString (), 'https://example.com/foo/books' );
28
- expect (url .resource ('books' , '42' ).toString (),
25
+ test ('Host and path is retained if exists in base (directory path)' , () {
26
+ final d = StandardUriDesign (Uri .parse ('https://example.com/foo/' ));
27
+ expect (d .collection ('books' ).toString (), 'https://example.com/foo/books' );
28
+ expect (d .resource ('books' , '42' ).toString (),
29
29
'https://example.com/foo/books/42' );
30
- expect (url .related ('books' , '42' , 'author' ).toString (),
30
+ expect (d .related ('books' , '42' , 'author' ).toString (),
31
31
'https://example.com/foo/books/42/author' );
32
- expect (url .relationship ('books' , '42' , 'author' ).toString (),
32
+ expect (d .relationship ('books' , '42' , 'author' ).toString (),
33
33
'https://example.com/foo/books/42/relationships/author' );
34
34
});
35
+ group ('Target matching' , () {
36
+ test ('Path only' , () {
37
+ final d = StandardUriDesign .pathOnly;
38
+ expect (d.matchTarget (Uri .parse ('/books' )), isA <Target >());
39
+ expect (d.matchTarget (Uri .parse ('/books/42' )), isA <ResourceTarget >());
40
+ expect (
41
+ d.matchTarget (Uri .parse ('/books/42/authors' )), isA <RelatedTarget >());
42
+ expect (d.matchTarget (Uri .parse ('/books/42/relationships/authors' )),
43
+ isA <RelationshipTarget >());
44
+ expect (d.matchTarget (Uri .parse ('/a/b/c/d' )), isNull);
45
+ });
46
+ test ('Path only, full url' , () {
47
+ final d = StandardUriDesign .pathOnly;
48
+ expect (
49
+ d.matchTarget (Uri .parse ('https://example.com/books' )), isA <Target >());
50
+ expect (d.matchTarget (Uri .parse ('https://example.com/books/42' )),
51
+ isA <ResourceTarget >());
52
+ expect (d.matchTarget (Uri .parse ('https://example.com/books/42/authors' )),
53
+ isA <RelatedTarget >());
54
+ expect (
55
+ d.matchTarget (
56
+ Uri .parse ('https://example.com/books/42/relationships/authors' )),
57
+ isA <RelationshipTarget >());
58
+ expect (d.matchTarget (Uri .parse ('https://example.com/a/b/c/d' )), isNull);
59
+ });
60
+ test ('Authority' , () {
61
+ final d = StandardUriDesign (Uri .parse ('https://example.com:8080' ));
62
+ expect (d.matchTarget (Uri .parse ('https://example.com:8080/books' )),
63
+ isA <Target >());
64
+ expect (d.matchTarget (Uri .parse ('https://example.com:8080/books/42' )),
65
+ isA <ResourceTarget >());
66
+ expect (
67
+ d.matchTarget (Uri .parse ('https://example.com:8080/books/42/authors' )),
68
+ isA <RelatedTarget >());
69
+ expect (
70
+ d.matchTarget (Uri .parse (
71
+ 'https://example.com:8080/books/42/relationships/authors' )),
72
+ isA <RelationshipTarget >());
73
+
74
+ expect (
75
+ d.matchTarget (Uri .parse ('https://example.com:8080/a/b/c/d' )), isNull);
76
+ expect (d.matchTarget (Uri .parse ('http://example.com:8080/books' )), isNull);
77
+ expect (d.matchTarget (Uri .parse ('https://foo.net:8080/books' )), isNull);
78
+ });
79
+
80
+ test ('Authority and path' , () {
81
+ final d = StandardUriDesign (Uri .parse ('https://example.com:8080/api' ));
82
+ expect (d.matchTarget (Uri .parse ('https://example.com:8080/api/books' )),
83
+ isA <Target >().having ((it) => it.type, 'type' , equals ('books' )));
84
+ expect (
85
+ d.matchTarget (Uri .parse ('https://example.com:8080/api/books/42' )),
86
+ isA <ResourceTarget >()
87
+ .having ((it) => it.type, 'type' , equals ('books' ))
88
+ .having ((it) => it.id, 'id' , equals ('42' )));
89
+ expect (
90
+ d.matchTarget (
91
+ Uri .parse ('https://example.com:8080/api/books/42/authors' )),
92
+ isA <RelatedTarget >()
93
+ .having ((it) => it.type, 'type' , equals ('books' ))
94
+ .having ((it) => it.id, 'id' , equals ('42' ))
95
+ .having (
96
+ (it) => it.relationship, 'relationship' , equals ('authors' )));
97
+ expect (
98
+ d.matchTarget (Uri .parse (
99
+ 'https://example.com:8080/api/books/42/relationships/authors' )),
100
+ isA <RelationshipTarget >()
101
+ .having ((it) => it.type, 'type' , equals ('books' ))
102
+ .having ((it) => it.id, 'id' , equals ('42' ))
103
+ .having (
104
+ (it) => it.relationship, 'relationship' , equals ('authors' )));
105
+
106
+ expect (
107
+ d.matchTarget (Uri .parse ('https://example.com:8080/a/b/c/d' )), isNull);
108
+ expect (d.matchTarget (Uri .parse ('http://example.com:8080/books' )), isNull);
109
+ expect (d.matchTarget (Uri .parse ('https://foo.net:8080/books' )), isNull);
110
+ expect (d.matchTarget (Uri .parse ('https://example.com:8080/foo/books' )),
111
+ isNull);
112
+ });
113
+ });
35
114
}
0 commit comments