|
| 1 | +local lpeg = require "lpeg" |
| 2 | +describe("IRI", function() |
| 3 | + local iri_lib = require "lpeg_patterns.iri" |
| 4 | + local absolute_IRI = iri_lib.absolute_IRI * lpeg.P(-1) |
| 5 | + local IRI = iri_lib.IRI * lpeg.P(-1) |
| 6 | + local iref = iri_lib.IRI_reference * lpeg.P(-1) |
| 7 | + local ipath = iri_lib.ipath * lpeg.P(-1) |
| 8 | + it("Should break down full IRIs correctly", function() |
| 9 | + assert.same({scheme="scheme", userinfo="userinfo", host="host", port=1234, path="/path", query="query", fragment="fragment"}, |
| 10 | + IRI:match "scheme://userinfo@host:1234/path?query#fragment") |
| 11 | + assert.same({scheme="scheme", userinfo="userinfo", host="host", port=1234, path="/path", query="query"}, |
| 12 | + IRI:match "scheme://userinfo@host:1234/path?query") |
| 13 | + assert.same({scheme="scheme", userinfo="userinfo", host="host", port=1234, path="/path"}, |
| 14 | + IRI:match "scheme://userinfo@host:1234/path") |
| 15 | + assert.same({scheme="scheme", host="host", port=1234, path="/path"}, |
| 16 | + IRI:match "scheme://host:1234/path") |
| 17 | + assert.same({scheme="scheme", host="host", path="/path"}, |
| 18 | + IRI:match "scheme://host/path") |
| 19 | + assert.same({scheme="scheme", path="/path"}, |
| 20 | + IRI:match "scheme:///path") |
| 21 | + assert.same({scheme="scheme"}, |
| 22 | + IRI:match "scheme://") |
| 23 | + end) |
| 24 | + it("Normalises to lower case scheme", function() |
| 25 | + assert.same({scheme="scheme"}, IRI:match "Scheme://") |
| 26 | + assert.same({scheme="scheme"}, IRI:match "SCHEME://") |
| 27 | + end) |
| 28 | + it("shouldn't allow fragments when using absolute_IRI", function() |
| 29 | + assert.falsy(absolute_IRI:match "scheme://userinfo@host:1234/path?query#fragment") |
| 30 | + assert.same({scheme="scheme", userinfo="userinfo", host="host", port=1234, path="/path", query="query"}, |
| 31 | + absolute_IRI:match "scheme://userinfo@host:1234/path?query") |
| 32 | + end) |
| 33 | + it("Should break down relative IRIs correctly", function() |
| 34 | + assert.same({scheme="scheme", userinfo="userinfo", host="host", port=1234, path="/path", query="query", fragment="fragment"}, |
| 35 | + iref:match "scheme://userinfo@host:1234/path?query#fragment") |
| 36 | + assert.same({userinfo="userinfo", host="host", port=1234, path="/path", query="query", fragment="fragment"}, |
| 37 | + iref:match "//userinfo@host:1234/path?query#fragment") |
| 38 | + assert.same({host="host", port=1234, path="/path", query="query", fragment="fragment"}, |
| 39 | + iref:match "//host:1234/path?query#fragment") |
| 40 | + assert.same({host="host", path="/path", query="query", fragment="fragment"}, |
| 41 | + iref:match "//host/path?query#fragment") |
| 42 | + assert.same({path="/path", query="query", fragment="fragment"}, |
| 43 | + iref:match "///path?query#fragment") |
| 44 | + assert.same({path="/path", query="query", fragment="fragment"}, |
| 45 | + iref:match "/path?query#fragment") |
| 46 | + assert.same({path="/path", fragment="fragment"}, |
| 47 | + iref:match "/path#fragment") |
| 48 | + assert.same({path="/path"}, |
| 49 | + iref:match "/path") |
| 50 | + assert.same({}, |
| 51 | + iref:match "") |
| 52 | + assert.same({query="query"}, |
| 53 | + iref:match "?query") |
| 54 | + assert.same({fragment="fragment"}, |
| 55 | + iref:match "#fragment") |
| 56 | + end) |
| 57 | + it("Should match file urls", function() |
| 58 | + assert.same({scheme="file", path="/var/log/messages"}, IRI:match "file:///var/log/messages") |
| 59 | + assert.same({scheme="file", path="/C:/Windows/"}, IRI:match "file:///C:/Windows/") |
| 60 | + end) |
| 61 | + it("Should decode unreserved percent characters path", function() |
| 62 | + assert.same("/underscore_character", ipath:match "/underscore%5Fcharacter") |
| 63 | + assert.same("/null%00byte", ipath:match "/null%00byte") |
| 64 | + end |
| 65 | +) it("Should fail on incorrect percent characters", function() |
| 66 | + assert.falsy(ipath:match "/bad%x0percent") |
| 67 | + assert.falsy(ipath:match "/%s") |
| 68 | + end) |
| 69 | + it("Should not introduce ambiguiuty by decoding percent encoded entities", function() |
| 70 | + assert.same({query="query%26with&ersand"}, iref:match "?query%26with&ersand") |
| 71 | + end) |
| 72 | + it("Should decode unreserved percent characters in query and fragment", function() |
| 73 | + assert.same({query="query%20with_escapes"}, iref:match "?query%20with%5Fescapes") |
| 74 | + assert.same({fragment="fragment%20with_escapes"}, iref:match "#fragment%20with%5Fescapes") |
| 75 | + end) |
| 76 | + it("Should match localhost", function() |
| 77 | + assert.same({host="localhost"}, iref:match "//localhost") |
| 78 | + assert.same({host="localhost"}, iref:match "//LOCALHOST") |
| 79 | + assert.same({host="localhost"}, iref:match "//l%4FcAlH%6fSt") |
| 80 | + assert.same({host="localhost", port=8000}, iref:match "//localhost:8000") |
| 81 | + assert.same({scheme="http", host="localhost", port=8000}, IRI:match "http://localhost:8000") |
| 82 | + end) |
| 83 | + it("Should work with IPv6", function() |
| 84 | + assert.same({host="0:0:0:0:0:0:0:1"}, iref:match "//[::1]") |
| 85 | + assert.same({host="0:0:0:0:0:0:0:1", port=80}, iref:match "//[::1]:80") |
| 86 | + end) |
| 87 | + it("IPvFuture", function() |
| 88 | + assert.same({host="v4.2", port=80}, iref:match "//[v4.2]:80") |
| 89 | + assert.same({host="v4.2", port=80}, iref:match "//[V4.2]:80") |
| 90 | + end) |
| 91 | + it("Should work with IPv6 zone local addresses", function() |
| 92 | + assert.same({host="0:0:0:0:0:0:0:1%eth0"}, iref:match "//[::1%25eth0]") |
| 93 | + end) |
| 94 | + it("Relative IRI does not match authority when scheme is missing", function() |
| 95 | + assert.same({path="example.com/"}, iref:match "example.com/") -- should end up in path |
| 96 | + assert.same({scheme="scheme", host="example.com", path="/"}, iref:match "scheme://example.com/") |
| 97 | + end) |
| 98 | + it("Should work with mailto URIs", function() |
| 99 | + assert.same({scheme="mailto", path="user@example.com"}, IRI:match "mailto:user@example.com") |
| 100 | + assert.same({scheme="mailto", path="someone@example.com,someoneelse@example.com"}, |
| 101 | + IRI:match "mailto:someone@example.com,someoneelse@example.com") |
| 102 | + assert.same({scheme="mailto", path="user@example.com", query="subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body"}, |
| 103 | + IRI:match "mailto:user@example.com?subject=This%20is%20the%20subject&cc=someone_else@example.com&body=This%20is%20the%20body") |
| 104 | + |
| 105 | + -- Examples from RFC-6068 |
| 106 | + -- Section 6.1 |
| 107 | + assert.same({scheme="mailto", path="chris@example.com"}, IRI:match "mailto:chris@example.com") |
| 108 | + assert.same({scheme="mailto", path="infobot@example.com", query="subject=current-issue"}, |
| 109 | + IRI:match "mailto:infobot@example.com?subject=current-issue") |
| 110 | + assert.same({scheme="mailto", path="infobot@example.com", query="body=send%20current-issue"}, |
| 111 | + IRI:match "mailto:infobot@example.com?body=send%20current-issue") |
| 112 | + assert.same({scheme="mailto", path="infobot@example.com", query="body=send%20current-issue%0D%0Asend%20index"}, |
| 113 | + IRI:match "mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index") |
| 114 | + assert.same({scheme="mailto", path="list@example.org", query="In-Reply-To=%3C3469A91.D10AF4C@example.com%3E"}, |
| 115 | + IRI:match "mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E") |
| 116 | + assert.same({scheme="mailto", path="majordomo@example.com", query="body=subscribe%20bamboo-l"}, |
| 117 | + IRI:match "mailto:majordomo@example.com?body=subscribe%20bamboo-l") |
| 118 | + assert.same({scheme="mailto", path="joe@example.com", query="cc=bob@example.com&body=hello"}, |
| 119 | + IRI:match "mailto:joe@example.com?cc=bob@example.com&body=hello") |
| 120 | + assert.same({scheme="mailto", path="gorby%25kremvax@example.com"}, IRI:match "mailto:gorby%25kremvax@example.com") |
| 121 | + assert.same({scheme="mailto", path="unlikely%3Faddress@example.com", query="blat=foop"}, |
| 122 | + IRI:match "mailto:unlikely%3Faddress@example.com?blat=foop") |
| 123 | + assert.same({scheme="mailto", path="Mike%26family@example.org"}, IRI:match "mailto:Mike%26family@example.org") |
| 124 | + -- Section 6.2 |
| 125 | + assert.same({scheme="mailto", path=[[%22not%40me%22@example.org]]}, IRI:match "mailto:%22not%40me%22@example.org") |
| 126 | + assert.same({scheme="mailto", path=[[%22oh%5C%5Cno%22@example.org]]}, IRI:match "mailto:%22oh%5C%5Cno%22@example.org") |
| 127 | + assert.same({scheme="mailto", path=[[%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org]]}, |
| 128 | + IRI:match "mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org") |
| 129 | + end) |
| 130 | + it("Should work with xmpp URIs", function() |
| 131 | + -- Examples from RFC-5122 |
| 132 | + assert.same({scheme="xmpp", path="node@example.com"}, IRI:match "xmpp:node@example.com") |
| 133 | + assert.same({scheme="xmpp", userinfo="guest", host="example.com"}, IRI:match "xmpp://guest@example.com") |
| 134 | + assert.same({scheme="xmpp", userinfo="guest", host="example.com", path="/support@example.com", query="message"}, |
| 135 | + IRI:match "xmpp://guest@example.com/support@example.com?message") |
| 136 | + assert.same({scheme="xmpp", path="support@example.com", query="message"}, IRI:match "xmpp:support@example.com?message") |
| 137 | + |
| 138 | + assert.same({scheme="xmpp", path="example-node@example.com"}, IRI:match "xmpp:example-node@example.com") |
| 139 | + assert.same({scheme="xmpp", path="example-node@example.com/some-resource"}, IRI:match "xmpp:example-node@example.com/some-resource") |
| 140 | + assert.same({scheme="xmpp", path="example.com"}, IRI:match "xmpp:example.com") |
| 141 | + assert.same({scheme="xmpp", path="example-node@example.com", query="message"}, IRI:match "xmpp:example-node@example.com?message") |
| 142 | + assert.same({scheme="xmpp", path="example-node@example.com", query="message;subject=Hello%20World"}, |
| 143 | + IRI:match "xmpp:example-node@example.com?message;subject=Hello%20World") |
| 144 | + assert.same({scheme="xmpp", path=[[nasty!%23$%25()*+,-.;=%3F%5B%5C%5D%5E_%60%7B%7C%7D~node@example.com]]}, |
| 145 | + IRI:match "xmpp:nasty!%23$%25()*+,-.;=%3F%5B%5C%5D%5E_%60%7B%7C%7D~node@example.com") |
| 146 | + assert.same({scheme="xmpp", path=[[node@example.com/repulsive%20!%23%22$%25&'()*+,-.%2F:;%3C=%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~resource]]}, |
| 147 | + IRI:match [[xmpp:node@example.com/repulsive%20!%23%22$%25&'()*+,-.%2F:;%3C=%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~resource]]) |
| 148 | + assert.same({scheme="xmpp", path="ji%C5%99i@%C4%8Dechy.example/v%20Praze"}, IRI:match "xmpp:ji%C5%99i@%C4%8Dechy.example/v%20Praze") |
| 149 | + end) |
| 150 | +end) |
0 commit comments