-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwitter.go
More file actions
129 lines (123 loc) · 3.39 KB
/
Copy pathtwitter.go
File metadata and controls
129 lines (123 loc) · 3.39 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package pageseo
import (
"slices"
"strings"
"testing"
)
const (
MetaTwitterPrefix = "twitter:"
MetaTwitterCard = MetaTwitterPrefix + "card"
MetaTwitterTitle = MetaTwitterPrefix + "title"
MetaTwitterDescription = MetaTwitterPrefix + "description"
MetaTwitterSite = MetaTwitterPrefix + "site"
MetaTwitterURL = MetaTwitterPrefix + "url"
MetaTwitterImage = MetaTwitterPrefix + "image"
MetaTwitterImageAlt = MetaTwitterPrefix + "image:alt"
MetaTwitterImageType = MetaTwitterPrefix + "image:type"
MetaTwitterImageHeight = MetaTwitterPrefix + "image:height"
MetaTwitterImageWidth = MetaTwitterPrefix + "image:width"
MetaTwitterPlayer = MetaTwitterPrefix + "player"
MetaTwitterPlayerURL = MetaTwitterPrefix + "player:url"
MetaTwitterPlayerWidth = MetaTwitterPrefix + "player:width"
MetaTwitterPlayerHeight = MetaTwitterPrefix + "player:height"
)
type twitter struct {
Card string
Title string
Description string
Site string
URL string
Image string
ImageAlt string
ImageType string
ImageHeight string
ImageWidth string
Player string
PlayerURL string
PlayerWidth string
PlayerHeight string
}
func TestTwitterMeta(
t testing.TB,
metaData map[string]string,
requirements HeadNodeConstraints,
) {
t.Helper()
card := twitter{}
unknownProperties := []string{}
for name, content := range metaData {
switch name {
case MetaTwitterCard:
card.Card = content
case MetaTwitterTitle:
card.Title = content
case MetaTwitterDescription:
card.Description = content
case MetaTwitterSite:
card.Site = content
case MetaTwitterURL:
card.URL = content
case MetaTwitterImage:
card.Image = content
case MetaTwitterImageAlt:
card.ImageAlt = content
case MetaTwitterImageType:
card.ImageType = content
case MetaTwitterImageHeight:
card.ImageHeight = content
case MetaTwitterImageWidth:
card.ImageWidth = content
case MetaTwitterPlayer:
card.Player = content
case MetaTwitterPlayerURL:
card.PlayerURL = content
case MetaTwitterPlayerWidth:
card.PlayerWidth = content
case MetaTwitterPlayerHeight:
card.PlayerHeight = content
default:
if strings.HasPrefix(name, MetaTwitterPrefix) {
unknownProperties = append(unknownProperties, name)
}
}
}
if len(unknownProperties) > 0 {
slices.Sort(unknownProperties)
t.Log("unknown Twitter properties:")
for _, property := range unknownProperties {
t.Log(" - ", property)
}
}
if card.Card == "" {
t.Error(MetaTwitterCard + " not found")
} else {
switch card.Card {
case "summary", "summary_large_image", "app", "player":
default:
t.Error(MetaTwitterCard, " is not valid")
}
}
if card.Title == "" {
t.Error(MetaTwitterTitle, " not found")
} else {
requirements.Title.apply(t, MetaTwitterTitle, card.Title)
}
if card.Description == "" {
t.Error(MetaTwitterDescription, " not found")
} else {
requirements.Description.apply(t, MetaTwitterDescription, card.Description)
}
// TODO: enforce head requirements
// else if err = r.TwitterCardDescription.Validate(card.Description); err != nil {
// t.Error(MetaTwitterDescription+" validation failed:", err)
// }
// if card.URL == "" {
// t.Error(MetaTwitterCard+" not found")
// }
if card.Site == "" {
t.Error(MetaTwitterSite + " not found")
}
if card.Image == "" {
t.Error(MetaTwitterImage + " not found")
}
}