Debian Codegraph @

hyperlinks_test.go

raw
1 //
2 // Copyright (c) 2016-2022 The Aurora Authors. All rights reserved.
3 // This program is free software. It comes without any warranty,
4 // to the extent permitted by applicable law. You can redistribute
5 // it and/or modify it under the terms of the Unlicense. See LICENSE
6 // file for more details or see below.
7 //
8
9 //
10 // This is free and unencumbered software released into the public domain.
11 //
12 // Anyone is free to copy, modify, publish, use, compile, sell, or
13 // distribute this software, either in source code form or as a compiled
14 // binary, for any purpose, commercial or non-commercial, and by any
15 // means.
16 //
17 // In jurisdictions that recognize copyright laws, the author or authors
18 // of this software dedicate any and all copyright interest in the
19 // software to the public domain. We make this dedication for the benefit
20 // of the public at large and to the detriment of our heirs and
21 // successors. We intend this dedication to be an overt act of
22 // relinquishment in perpetuity of all present and future rights to this
23 // software under copyright law.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 // OTHER DEALINGS IN THE SOFTWARE.
32 //
33 // For more information, please refer to <http://unlicense.org/>
34 //
35
36 package aurora
37
38 import (
39 "strings"
40 "testing"
41
42 "github.com/stretchr/testify/assert"
43 "github.com/stretchr/testify/require"
44 )
45
46 func TestHyperlinkParam_stringLen(t *testing.T) {
47 var param HyperlinkParam
48 assert.Equal(t, 1, param.stringLen())
49 param.Key, param.Value = "some", "thing"
50 assert.Equal(t, 1+len("some")+len("thing"), param.stringLen())
51 }
52
53 func TestHyperlinkParam_String(t *testing.T) {
54 var param HyperlinkParam
55 assert.Equal(t, "=", param.String())
56 param.Key, param.Value = "some", "thing"
57 assert.Equal(t, "some=thing", param.String())
58 }
59
60 func TestIsValidHyperlinkTarget(t *testing.T) {
61 assert.True(t, IsValidHyperlinkTarget("http://example.com/path?query=true"))
62 assert.True(t, IsValidHyperlinkTarget("mailto:[email protected]"))
63 assert.False(t, IsValidHyperlinkTarget("http://пример.тест/путь?запрос=да"))
64 assert.False(t, IsValidHyperlinkTarget("mailto:пользователь@пример.тест"))
65 }
66
67 func TestIsValidHyperlinkParam(t *testing.T) {
68 assert.True(t, IsValidHyperlinkParam("id"))
69 assert.True(t, IsValidHyperlinkParam("a_param-key"))
70 assert.False(t, IsValidHyperlinkParam("value:true"))
71 assert.False(t, IsValidHyperlinkParam("value=true"))
72 assert.False(t, IsValidHyperlinkParam("v1;v2"))
73 }
74
75 func TestHyperlinkID(t *testing.T) {
76 assert.Equal(t, HyperlinkParam{
77 Key: HyperlinkIDKey,
78 Value: "value",
79 }, HyperlinkID("value"))
80 }
81
82 func Test_unhex(t *testing.T) {
83 assert.Zero(t, unhex(0))
84 }
85
86 func TestHyperlinkEscape(t *testing.T) {
87 var val = "http://example.com/path?query=true"
88 assert.Equal(t, val, HyperlinkEscape(val))
89 val = "mailto:[email protected]"
90 assert.Equal(t, val, HyperlinkEscape(val))
91 val = "http://пример.тест/путь?запрос=да"
92 assert.True(t, IsValidHyperlinkTarget(HyperlinkEscape(val)))
93 val = "mailto:пользователь@пример.тест"
94 assert.True(t, IsValidHyperlinkTarget(HyperlinkEscape(val)))
95 }
96
97 func TestHyperlinkUnescape(t *testing.T) {
98 for _, val := range []string{
99 "http://example.com/path?query=true",
100 "mailto:[email protected]",
101 "http://пример.тест/путь?запрос=да",
102 "mailto:пользователь@пример.тест",
103 } {
104 var got, err = HyperlinkUnescape(HyperlinkEscape(val))
105 require.NoError(t, err)
106 assert.Equal(t, val, got)
107 }
108 var _, err = HyperlinkUnescape("%%%%")
109 assert.Error(t, err)
110 var (
111 val = "значение"
112 escaped = HyperlinkEscape(val)
113 back string
114 )
115 escaped = strings.ToLower(escaped)
116 back, err = HyperlinkUnescape(escaped)
117 assert.NoError(t, err)
118 assert.Equal(t, val, back)
119 }