-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser_test.go
571 lines (462 loc) Β· 15.2 KB
/
parser_test.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package parser_test
import (
"io/ioutil"
"strings"
"testing"
parser "github.com/ammit/go-metaparser"
)
const html = `
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title>Go Meta Parser</title>
<meta name="description" content="Go Meta Description">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="site.webmanifest">
<link rel="apple-touch-icon" href="icon.png">
<link rel="icon" type="image/svg+xml" href="https://example.com/icon.svg">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<!-- og:basic -->
<meta property="og:title" content="sample title" />
<meta property="og:description" content="sample description" />
<meta property="og:determiner" content="determiner" />
<meta property="og:type" content="demo" />
<meta property="og:url" content="http://example.com" />
<meta property="og:locale" content="en_GB" />
<meta property="og:locale:alternate" content="fr_FR" />
<meta property="og:locale:alternate" content="es_ES" />
<meta property="og:site_name" content="sample site_name" />
<!-- og:image -->
<meta property="og:image" content="http://example.com/ogp.jpg" />
<meta property="og:image:secure_url" content="https://secure.example.com/ogp.jpg" />
<meta property="og:image:type" content="image/jpeg" />
<meta property="og:image:width" content="400" />
<meta property="og:image:height" content="300" />
<meta property="og:image:alt" content="A shiny red apple with a bite taken out" />
<!-- og:video -->
<meta property="og:video" content="http://example.com/movie.swf" />
<meta property="og:video:secure_url" content="https://secure.example.com/movie.swf" />
<meta property="og:video:type" content="application/x-shockwave-flash" />
<meta property="og:video:width" content="400" />
<meta property="og:video:height" content="300" />
<!-- (new) video:actor/movie/episod -->
<meta property="video:actor" content="http://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d" />
<meta property="video:actor:role" content="xyz">
<meta property="video:actor" content="http://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d" />
<meta property="video:actor:role" content="xyz">
<meta property="video:director" content="http://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d" />
<meta property="video:writer" content="http://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d" />
<meta property="video:duration" content="1236">
<meta property="video:release_date" content="1236">
<meta property="video:tag" content="a">
<meta property="video:tag" content="b">
<meta property="video:series" content="http://open.spotify.com/series/1dfeR4HaWDbWqFHLkxsg1d">
<!-- og:audio -->
<meta property="og:audio" content="http://example.com/sound.mp3" />
<meta property="og:audio:secure_url" content="https://secure.example.com/sound.mp3" />
<meta property="og:audio:type" content="audio/mpeg" />
<!-- (new) music:music/album/musician/playlist/radio_station -->
<meta property="music:musician" content="http://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d">
<meta property="music:musician" content="http://open.spotify.com/artist/0oSGxfWSnnOXhD2fKuz2Gy">
<meta property="music:album" content="http://open.spotify.com/album/7rq68qYz66mNdPfidhIEFa">
<meta property="music:album:track" content="2">
<meta property="music:duration" content="236">
<meta property="music:release_date" content="2011-04-19">
<meta property="music:song" content="http://open.spotify.com/track/2aSFLiDPreOVP6KHiWk4lF">
<meta property="music:song:disc" content="1">
<meta property="music:song:track" content="2">
<meta property="music:creator" content="http://open.spotify.com/user/austinhaugen"/>
<!-- article: -->
<meta property="article:published_time" content="test" />
<meta property="article:modified_time" content="test" />
<meta property="article:expiration_time" content="test" />
<meta property="article:author" content="test" />
<meta property="article:author" content="test2" />
<meta property="article:section" content="test" />
<meta property="article:tag" content="test" />
<meta property="article:tag" content="tes2" />
<!-- book: -->
<meta property="book:author" content="book author" />
<meta property="book:isbn" content="isbn" />
<meta property="book:release_date" content="test" />
<meta property="book:tag" content="test" />
<meta property="book:tag" content="tes2" />
<!-- profile: -->
<meta property="profile:first_name" content="firstname" />
<meta property="profile:last_name" content="lastname" />
<meta property="profile:username" content="username" />
<meta property="profile:gender" content="male" />
<!-- twitter: -->
<meta property="twitter:card" content="test" />
<meta property="twitter:site" content="test" />
<meta property="twitter:site:id" content="test" />
<meta property="twitter:creator" content="test" />
<meta property="twitter:creator:id" content="test" />
<meta property="twitter:description" content="test" />
<meta property="twitter:title" content="test" />
<meta property="twitter:image" content="test" />
<meta property="twitter:image:alt" content="test" />
<meta property="twitter:player" content="test" />
<meta property="twitter:player:height" content="10" />
<meta property="twitter:player:width" content="10" />
<meta property="twitter:player:stream" content="test" />
<meta property="twitter:app:name:iphone" content="test" />
<meta property="twitter:app:url:iphone" content="test" />
<meta property="twitter:app:id:iphone" content="1" />
<meta property="twitter:app:name:ipad" content="test" />
<meta property="twitter:app:url:ipad" content="test" />
<meta property="twitter:app:id:ipad" content="1" />
<meta property="twitter:app:name:googleplay" content="test" />
<meta property="twitter:app:url:googleplay" content="test" />
<meta property="twitter:app:id:googleplay" content="1" />
</head>
<body>
</body>
`
const titleHtml = `
<!doctype html>
<html class="no-js" lang="">
<head>
<title id="pageTitle">Go Meta Parser</title>
</head>
<body>
</body>
`
func TestParserParseHTML(t *testing.T) {
p := parser.New()
err := p.ParseHTML(ioutil.NopCloser(strings.NewReader(html)))
if err != nil {
t.Fatal(err)
}
if p.Title != "Go Meta Parser" {
t.Error("title parsed incorrectly")
}
if p.Description != "Go Meta Description" {
t.Error("description parsed incorrectly")
}
if p.OpenGraph.Title != "sample title" {
t.Error("Open Graph title parsed incorrectly")
}
if p.OpenGraph.Description != "sample description" {
t.Error("Open Graph description parsed incorrectly")
}
if p.OpenGraph.Determiner != "determiner" {
t.Error("determiner parsed incorrectly")
}
if p.OpenGraph.Type != "demo" {
t.Error("type parsed incorrectly")
}
if p.OpenGraph.URL != "http://example.com" {
t.Error("url parsed incorrectly")
}
if p.OpenGraph.Locale != "en_GB" {
t.Error("locale parsed incorrectly")
}
if len(p.OpenGraph.LocalesAlternate) == 0 {
t.Error("locales_alternate parsed incorrectly")
}
if p.OpenGraph.SiteName != "sample site_name" {
t.Error("site_name parsed incorrectly")
}
// og:image
if len(p.Images) == 0 {
t.Error("images parsed incorrectly")
} else {
if len(p.Images[0].URL) == 0 {
t.Error("image url parsed incorrectly")
}
if len(p.Images[0].SecureURL) == 0 {
t.Error("image secure_url parsed incorrectly")
}
if len(p.Images[0].Type) == 0 {
t.Error("image type parsed incorrectly")
}
if p.Images[0].Width == 0 {
t.Error("image width parsed incorrectly")
}
if p.Images[0].Height == 0 {
t.Error("image height parsed incorrectly")
}
if len(p.Images[0].Alt) == 0 {
t.Error("image alt parsed incorrectly")
}
}
// og:video
if len(p.Videos) == 0 {
t.Error("videos parsed incorrectly")
} else {
if len(p.Videos[0].URL) == 0 {
t.Error("video url parsed incorrectly")
}
if len(p.Videos[0].SecureURL) == 0 {
t.Error("video secure_url parsed incorrectly")
}
if len(p.Videos[0].Type) == 0 {
t.Error("video type parsed incorrectly")
}
if p.Videos[0].Width == 0 {
t.Error("video width parsed incorrectly")
}
if p.Videos[0].Height == 0 {
t.Error("video height parsed incorrectly")
}
// video:
if len(p.Videos[0].Actors) == 0 {
t.Error("video actors parsed incorrectly")
} else {
if len(p.Videos[0].Actors[0].URL) == 0 {
t.Error("video actors url parsed incorrectly")
}
if len(p.Videos[0].Actors[0].Role) == 0 {
t.Error("video actors role parsed incorrectly")
}
}
if len(p.Videos[0].Director) == 0 {
t.Error("video director parsed incorrectly")
}
if len(p.Videos[0].Writer) == 0 {
t.Error("video writer parsed incorrectly")
}
if p.Videos[0].Duration == 0 {
t.Error("video duration parsed incorrectly")
}
if len(p.Videos[0].ReleaseDate) == 0 {
t.Error("video release_date parsed incorrectly")
}
if len(p.Videos[0].Series) == 0 {
t.Error("video series parsed incorrectly")
}
if len(p.Videos[0].Tags) == 0 {
t.Error("video tags parsed incorrectly")
} else {
if len(p.Videos[0].Tags[0]) == 0 {
t.Error("video tags tag parsed incorrectly")
}
}
}
// og:audio
if len(p.Audios) == 0 {
t.Error("audios parsed incorrectly")
} else {
if len(p.Audios[0].URL) == 0 {
t.Error("audio url parsed incorrectly")
}
if len(p.Audios[0].SecureURL) == 0 {
t.Error("audio secure_url parsed incorrectly")
}
if len(p.Audios[0].Type) == 0 {
t.Error("audio type parsed incorrectly")
}
}
// music
if len(p.Music.Musicians) == 0 {
t.Error("musicians parsed incorrectly")
}
if len(p.Music.Album.URL) == 0 {
t.Error("Music Album URL parsed incorrectly")
}
if p.Music.Album.Track == 0 {
t.Error("Music Album Track parsed incorrectly")
}
if p.Music.Duration == 0 {
t.Error("Music duration parsed incorrectly")
}
if len(p.Music.ReleaseDate) == 0 {
t.Error("Music Release Date parsed incorrectly")
}
if len(p.Music.Creator) == 0 {
t.Error("Music creator parsed incorrectly")
}
if len(p.Music.Songs) == 0 {
t.Error("Music songs parsed incorrectly")
} else {
if len(p.Music.Songs[0].URL) == 0 {
t.Error("Music song url parsed incorrectly")
}
if p.Music.Songs[0].Disc == 0 {
t.Error("Music song disc parsed incorrectly")
}
if p.Music.Songs[0].Track == 0 {
t.Error("Music song track parsed incorrectly")
}
}
// article
if len(p.Article.PublishedTime) == 0 {
t.Error("article published_time parsed incorrectly")
}
if len(p.Article.ModifiedTime) == 0 {
t.Error("article modified_time parsed incorrectly")
}
if len(p.Article.ExpirationTime) == 0 {
t.Error("article expiration_time parsed incorrectly")
}
if len(p.Article.Section) == 0 {
t.Error("article section parsed incorrectly")
}
if len(p.Article.Tags) == 0 {
t.Error("Article tags parsed incorrectly")
} else {
if len(p.Article.Tags[0]) == 0 {
t.Error("Article tags tag parsed incorrectly")
}
}
if len(p.Article.Authors) == 0 {
t.Error("Article Authors parsed incorrectly")
} else {
if len(p.Article.Authors[0]) == 0 {
t.Error("Article Authors Author parsed incorrectly")
}
}
// books
if len(p.Book.ReleaseDate) == 0 {
t.Error("article released_date parsed incorrectly")
}
if len(p.Book.Isbn) == 0 {
t.Error("article isbin parsed incorrectly")
}
if len(p.Book.Tags) == 0 {
t.Error("Book tags parsed incorrectly")
} else {
if len(p.Book.Tags[0]) == 0 {
t.Error("Book tags tag parsed incorrectly")
}
}
if len(p.Book.Authors) == 0 {
t.Error("Book Authors parsed incorrectly")
} else {
if len(p.Book.Authors[0]) == 0 {
t.Error("Book Authors Author parsed incorrectly")
}
}
// profile
if len(p.Profile.FirstName) == 0 {
t.Error("profile first_name parsed incorrectly")
}
if len(p.Profile.LastName) == 0 {
t.Error("profile last_name parsed incorrectly")
}
if len(p.Profile.Username) == 0 {
t.Error("profile username parsed incorrectly")
}
if len(p.Profile.Gender) == 0 {
t.Error("profile gender parsed incorrectly")
}
// twitter
if len(p.Twitter.Card) == 0 {
t.Error("twitter:card parsed incorrectly")
}
if len(p.Twitter.Site) == 0 {
t.Error("twitter:site parsed incorrectly")
}
if len(p.Twitter.SiteID) == 0 {
t.Error("twitter:site:id parsed incorrectly")
}
if len(p.Twitter.Creator) == 0 {
t.Error("twitter:creator parsed incorrectly")
}
if len(p.Twitter.CreatorID) == 0 {
t.Error("twitter:creator:id parsed incorrectly")
}
if len(p.Twitter.Description) == 0 {
t.Error("twitter:description parsed incorrectly")
}
if len(p.Twitter.Title) == 0 {
t.Error("twitter:title parsed incorrectly")
}
if len(p.Twitter.Image) == 0 {
t.Error("twitter:image parsed incorrectly")
}
if len(p.Twitter.ImageAlt) == 0 {
t.Error("twitter:image:alt parsed incorrectly")
}
if len(p.Twitter.Player.URL) == 0 {
t.Error("twitter:player parsed incorrectly")
}
if p.Twitter.Player.Height == 0 {
t.Error("twitter:player:height parsed incorrectly")
}
if p.Twitter.Player.Width == 0 {
t.Error("twitter:player:width parsed incorrectly")
}
if len(p.Twitter.Player.Stream) == 0 {
t.Error("twitter:player:stream parsed incorrectly")
}
// log.Fatalf("%+v", p.Twitter.Apps)
if len(p.Twitter.Apps[0].Name) == 0 {
t.Error("twitter:app:name:iphone parsed incorrectly")
}
if len(p.Twitter.Apps[0].URL) == 0 {
t.Error("twitter:app:url:iphone parsed incorrectly")
}
if p.Twitter.Apps[0].ID == 0 {
t.Error("twitter:app:id:iphone parsed incorrectly")
}
if len(p.Twitter.Apps[0].Type) == 0 {
t.Error("twitter:app:type:iphone parsed incorrectly")
}
if len(p.Twitter.Apps[0].Name) == 0 {
t.Error("twitter:app:name:ipad parsed incorrectly")
}
if len(p.Twitter.Apps[0].URL) == 0 {
t.Error("twitter:app:url:ipad parsed incorrectly")
}
if p.Twitter.Apps[0].ID == 0 {
t.Error("twitter:app:id:ipad parsed incorrectly")
}
if len(p.Twitter.Apps[0].Type) == 0 {
t.Error("twitter:app:type:ipad parsed incorrectly")
}
if len(p.Twitter.Apps[0].Name) == 0 {
t.Error("twitter:app:name:googleplay parsed incorrectly")
}
if len(p.Twitter.Apps[0].URL) == 0 {
t.Error("twitter:app:url:googleplay parsed incorrectly")
}
if p.Twitter.Apps[0].ID == 0 {
t.Error("twitter:app:id:googleplay parsed incorrectly")
}
if len(p.Twitter.Apps[0].Type) == 0 {
t.Error("twitter:app:type:googleplay parsed incorrectly")
}
if len(p.Favicons) != 2 {
t.Error("Favicons parsed incorrectly")
}
if p.Favicons[0].URL != "icon.png" {
t.Error("Favion URL parsed incorrectly")
}
if p.Favicons[1].Type != "image/svg+xml" {
t.Error("Favicon type parsed incorrectly")
}
}
func TestParserParseHTMLWithResult(t *testing.T) {
p := parser.New()
result, err := p.ParseHTMLWithResult(ioutil.NopCloser(strings.NewReader(html)))
if err != nil {
t.Fatal(err)
}
if result.GetTitle() != result.OpenGraph.Title {
t.Error("GetTitle does not return correct title")
}
if result.GetDescription() != result.OpenGraph.Description {
t.Error("GetDescription does not return correct description")
}
}
func TestParserParseTitleWithAttribute(t *testing.T) {
p := parser.New()
result, err := p.ParseHTMLWithResult(ioutil.NopCloser(strings.NewReader(titleHtml)))
if err != nil {
t.Fatal(err)
}
if result.GetTitle() != "Go Meta Parser" {
t.Error("GetTitle does not return correct title")
}
}
func BenchmarkParser(b *testing.B) {
p := parser.New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = p.ParseHTML(ioutil.NopCloser(strings.NewReader(html)))
}
}