summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dyatlov/go-opengraph/opengraph/opengraph_test.go
blob: 6af7f25d255e8a01e2da9903147eafb2c59867c8 (plain)
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
package opengraph_test

import (
	"strings"
	"testing"
	"time"

	"github.com/dyatlov/go-opengraph/opengraph"
)

const html = `
  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WordPress &#8250;   WordPress 4.3 &#8220;Billie&#8221;</title>

<!-- Jetpack Open Graph Tags -->
<meta property="og:type" content="article" />
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
<meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" />
<meta property="og:description" content="Version 4.3 of WordPress, named &quot;Billie&quot; in honor of jazz singer Billie Holiday, is available for download or update in your WordPress dashboard. New features in 4.3 make it even easier to format y..." />
<meta property="article:published_time" content="2015-08-18T19:12:38+00:00" />
<meta property="article:modified_time" content="2015-08-19T13:10:24+00:00" />
<meta property="og:site_name" content="WordPress News" />
<meta property="og:image" content="https://www.gravatar.com/avatar/2370ea5912750f4cb0f3c51ae1cbca55?d=mm&amp;s=180&amp;r=G" />
<meta property="og:locale" content="en_US" />
<meta name="twitter:site" content="@WordPress" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:creator" content="@WordPress" />
  `

func BenchmarkOpenGraph_ProcessHTML(b *testing.B) {
	og := opengraph.NewOpenGraph()
	b.ReportAllocs()
	b.SetBytes(int64(len(html)))
	for i := 0; i < b.N; i++ {
		if err := og.ProcessHTML(strings.NewReader(html)); err != nil {
			b.Fatal(err)
		}
	}
}

func TestOpenGraphProcessHTML(t *testing.T) {
	og := opengraph.NewOpenGraph()
	err := og.ProcessHTML(strings.NewReader(html))

	if err != nil {
		t.Fatal(err)
	}

	if og.Type != "article" {
		t.Error("type parsed incorrectly")
	}

	if len(og.Title) == 0 {
		t.Error("title parsed incorrectly")
	}

	if len(og.URL) == 0 {
		t.Error("url parsed incorrectly")
	}

	if len(og.Description) == 0 {
		t.Error("description parsed incorrectly")
	}

	if len(og.Images) == 0 {
		t.Error("images parsed incorrectly")
	} else {
		if len(og.Images[0].URL) == 0 {
			t.Error("image url parsed incorrectly")
		}
	}

	if len(og.Locale) == 0 {
		t.Error("locale parsed incorrectly")
	}

	if len(og.SiteName) == 0 {
		t.Error("site name parsed incorrectly")
	}

	if og.Article == nil {
		t.Error("articles parsed incorrectly")
	} else {
		ev, _ := time.Parse(time.RFC3339, "2015-08-18T19:12:38+00:00")
		if !og.Article.PublishedTime.Equal(ev) {
			t.Error("article published time parsed incorrectly")
		}
	}
}

func TestOpenGraphProcessMeta(t *testing.T) {
	og := opengraph.NewOpenGraph()

	og.ProcessMeta(map[string]string{"property": "og:type", "content": "book"})

	if og.Type != "book" {
		t.Error("wrong og:type processing")
	}

	og.ProcessMeta(map[string]string{"property": "book:isbn", "content": "123456"})

	if og.Book == nil {
		t.Error("wrong book type processing")
	} else {
		if og.Book.ISBN != "123456" {
			t.Error("wrong book isbn processing")
		}
	}

	og.ProcessMeta(map[string]string{"property": "article:section", "content": "testsection"})

	if og.Article != nil {
		t.Error("article processed when it should not be")
	}

	og.ProcessMeta(map[string]string{"property": "book:author:first_name", "content": "John"})

	if og.Book != nil {
		if len(og.Book.Authors) == 0 {
			t.Error("book author was not processed")
		} else {
			if og.Book.Authors[0].FirstName != "John" {
				t.Error("author first name was processed incorrectly")
			}
		}
	}
}