| 1 |
package parse_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/lib/parse" |
| 9 |
) |
| 10 |
|
| 11 |
func TestHyperlinks(t *testing.T) { |
| 12 |
tests := []struct { |
| 13 |
name string |
| 14 |
text string |
| 15 |
links []string |
| 16 |
html bool |
| 17 |
}{ |
| 18 |
{ |
| 19 |
name: "http-link", |
| 20 |
text: "http://aerc-mail.org", |
| 21 |
links: []string{"http://aerc-mail.org"}, |
| 22 |
}, |
| 23 |
{ |
| 24 |
name: "https-link", |
| 25 |
text: "https://aerc-mail.org", |
| 26 |
links: []string{"https://aerc-mail.org"}, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "https-link-in-text", |
| 30 |
text: "text https://aerc-mail.org more text", |
| 31 |
links: []string{"https://aerc-mail.org"}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "https-link-in-parenthesis", |
| 35 |
text: "text (https://aerc-mail.org) more text", |
| 36 |
links: []string{"https://aerc-mail.org"}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "https-link-in-quotes", |
| 40 |
text: "text \"https://aerc-mail.org\" more text", |
| 41 |
links: []string{"https://aerc-mail.org"}, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "https-link-in-angle-brackets", |
| 45 |
text: "text <https://aerc-mail.org> more text", |
| 46 |
links: []string{"https://aerc-mail.org"}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "https-link-in-html", |
| 50 |
text: "<a href=\"https://aerc-mail.org\">", |
| 51 |
links: []string{"https://aerc-mail.org"}, |
| 52 |
html: true, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "https-link-twice", |
| 56 |
text: "text https://aerc-mail.org more text https://aerc-mail.org more text", |
| 57 |
links: []string{"https://aerc-mail.org"}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "https-link-markdown", |
| 61 |
text: "text [https://aerc-mail.org](https://aerc-mail.org) more text", |
| 62 |
links: []string{"https://aerc-mail.org"}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "multiple-links", |
| 66 |
text: "text https://aerc-mail.org more text http://git.sr.ht/~rjarry/aerc more text", |
| 67 |
links: []string{"https://aerc-mail.org", "http://git.sr.ht/~rjarry/aerc"}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "rfc", |
| 71 |
text: "text http://www.ietf.org/rfc/rfc2396.txt more text", |
| 72 |
links: []string{"http://www.ietf.org/rfc/rfc2396.txt"}, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "http-with-query-and-fragment", |
| 76 |
text: "text <http://example.com:8042/over/there?name=ferret#nose> more text", |
| 77 |
links: []string{"http://example.com:8042/over/there?name=ferret#nose"}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "http-with-at", |
| 81 |
text: "text http://cnn.example.com&[email protected]/top_story.htm more text", |
| 82 |
links: []string{"http://cnn.example.com&[email protected]/top_story.htm"}, |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "https-with-fragment", |
| 86 |
text: "text https://www.ics.uci.edu/pub/ietf/uri/#Related more text", |
| 87 |
links: []string{"https://www.ics.uci.edu/pub/ietf/uri/#Related"}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "https-in-html", |
| 91 |
text: "<div>text https://example.com/test<br>https://test.org/?a=b</div><br> text", |
| 92 |
links: []string{"https://example.com/test", "https://test.org/?a=b"}, |
| 93 |
html: true, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "https-with-query", |
| 97 |
text: "text https://www.example.com/index.php?id_sezione=360&sid=3a5ebc944f41daa6f849f730f1 more text", |
| 98 |
links: []string{"https://www.example.com/index.php?id_sezione=360&sid=3a5ebc944f41daa6f849f730f1"}, |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "https-onedrive", |
| 102 |
text: "I have a link like this in an email (I deleted a few characters here-and-there for privacy) https://1drv.ms/w/s!Ap-KLfhNxS4fRt6tIvw?e=dW8WLO", |
| 103 |
links: []string{"https://1drv.ms/w/s!Ap-KLfhNxS4fRt6tIvw?e=dW8WLO"}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "email", |
| 107 |
text: "You can reach me via the somewhat strange, but nonetheless valid, email [email protected]", |
| 108 |
links: []string{"mailto:[email protected]"}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "mailto", |
| 112 |
text: "You can reach me via the somewhat strange, but nonetheless valid, email mailto:[email protected]. Thank you", |
| 113 |
links: []string{"mailto:[email protected]"}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "mailto-ipv6", |
| 117 |
text: "You can reach me via the somewhat strange, but nonetheless valid, email mailto:~mpldr/list@[2001:db8::7]", |
| 118 |
links: []string{"mailto:~mpldr/list@[2001:db8::7]"}, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "mailto-ipv6-query", |
| 122 |
text: "You can reach me via the somewhat strange, but nonetheless valid, email mailto:~mpldr/list@[2001:db8::7]?subject=whazzup%3F", |
| 123 |
links: []string{"mailto:~mpldr/list@[2001:db8::7]?subject=whazzup%3F"}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "simple email in <a href>", |
| 127 |
text: `<a href="mailto:[email protected]" rel="noopener noreferrer">`, |
| 128 |
links: []string{"mailto:[email protected]"}, |
| 129 |
html: true, |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "simple email in <a> body", |
| 133 |
text: `<a href="#" rel="noopener noreferrer">[email protected]</a><br/><p>more text</p>`, |
| 134 |
links: []string{"mailto:[email protected]"}, |
| 135 |
html: true, |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "emails in <a> href and body", |
| 139 |
text: `<a href="mailto:[email protected]" rel="noopener noreferrer">[email protected]</a><br/><p>more text</p>`, |
| 140 |
links: []string{"mailto:[email protected]", "mailto:[email protected]"}, |
| 141 |
html: true, |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "email in <...>", |
| 145 |
text: `<div>01.02.2023, 10:11, "Firstname Lastname" <[email protected]>:</div>`, |
| 146 |
links: []string{"mailto:[email protected]"}, |
| 147 |
html: true, |
| 148 |
}, |
| 149 |
} |
| 150 |
|
| 151 |
for i, test := range tests { |
| 152 |
t.Run(test.name, func(t *testing.T) { |
| 153 |
// make sure reader is exact copy of input reader |
| 154 |
reader, parsedLinks := parse.HttpLinks(strings.NewReader(test.text), test.html) |
| 155 |
if _, err := io.ReadAll(reader); err != nil { |
| 156 |
t.Skipf("could not read text: %v", err) |
| 157 |
} |
| 158 |
|
| 159 |
// check correct parsed links |
| 160 |
if len(parsedLinks) != len(test.links) { |
| 161 |
t.Errorf("different number of links: got %d but expected %d", len(parsedLinks), len(test.links)) |
| 162 |
} |
| 163 |
linkMap := make(map[string]struct{}) |
| 164 |
for _, got := range parsedLinks { |
| 165 |
linkMap[got] = struct{}{} |
| 166 |
} |
| 167 |
for _, expected := range test.links { |
| 168 |
if _, ok := linkMap[expected]; !ok { |
| 169 |
t.Errorf("link[%d] not parsed: %s", i, expected) |
| 170 |
} |
| 171 |
} |
| 172 |
}) |
| 173 |
} |
| 174 |
} |