github.com/nhannv/mattermost-server@v5.11.1+incompatible/app/opengraph_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/dyatlov/go-opengraph/opengraph" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func BenchmarkForceHTMLEncodingToUTF8(b *testing.B) { 15 HTML := ` 16 <html> 17 <head> 18 <meta property="og:url" content="https://example.com/apps/mattermost"> 19 <meta property="og:image" content="https://images.example.com/image.png"> 20 </head> 21 </html> 22 ` 23 ContentType := "text/html; utf-8" 24 25 b.Run("with converting", func(b *testing.B) { 26 for i := 0; i < b.N; i++ { 27 r := forceHTMLEncodingToUTF8(strings.NewReader(HTML), ContentType) 28 29 og := opengraph.NewOpenGraph() 30 og.ProcessHTML(r) 31 } 32 }) 33 34 b.Run("without converting", func(b *testing.B) { 35 for i := 0; i < b.N; i++ { 36 og := opengraph.NewOpenGraph() 37 og.ProcessHTML(strings.NewReader(HTML)) 38 } 39 }) 40 } 41 42 func TestMakeOpenGraphURLsAbsolute(t *testing.T) { 43 for name, tc := range map[string]struct { 44 HTML string 45 RequestURL string 46 URL string 47 ImageURL string 48 }{ 49 "absolute URLs": { 50 HTML: ` 51 <html> 52 <head> 53 <meta property="og:url" content="https://example.com/apps/mattermost"> 54 <meta property="og:image" content="https://images.example.com/image.png"> 55 </head> 56 </html>`, 57 RequestURL: "https://example.com", 58 URL: "https://example.com/apps/mattermost", 59 ImageURL: "https://images.example.com/image.png", 60 }, 61 "URLs starting with /": { 62 HTML: ` 63 <html> 64 <head> 65 <meta property="og:url" content="/apps/mattermost"> 66 <meta property="og:image" content="/image.png"> 67 </head> 68 </html>`, 69 RequestURL: "http://example.com", 70 URL: "http://example.com/apps/mattermost", 71 ImageURL: "http://example.com/image.png", 72 }, 73 "HTTPS URLs starting with /": { 74 HTML: ` 75 <html> 76 <head> 77 <meta property="og:url" content="/apps/mattermost"> 78 <meta property="og:image" content="/image.png"> 79 </head> 80 </html>`, 81 RequestURL: "https://example.com", 82 URL: "https://example.com/apps/mattermost", 83 ImageURL: "https://example.com/image.png", 84 }, 85 "missing image URL": { 86 HTML: ` 87 <html> 88 <head> 89 <meta property="og:url" content="/apps/mattermost"> 90 </head> 91 </html>`, 92 RequestURL: "http://example.com", 93 URL: "http://example.com/apps/mattermost", 94 ImageURL: "", 95 }, 96 "relative URLs": { 97 HTML: ` 98 <html> 99 <head> 100 <meta property="og:url" content="index.html"> 101 <meta property="og:image" content="../resources/image.png"> 102 </head> 103 </html>`, 104 RequestURL: "http://example.com/content/index.html", 105 URL: "http://example.com/content/index.html", 106 ImageURL: "http://example.com/resources/image.png", 107 }, 108 } { 109 t.Run(name, func(t *testing.T) { 110 og := opengraph.NewOpenGraph() 111 if err := og.ProcessHTML(strings.NewReader(tc.HTML)); err != nil { 112 t.Fatal(err) 113 } 114 115 makeOpenGraphURLsAbsolute(og, tc.RequestURL) 116 117 if og.URL != tc.URL { 118 t.Fatalf("incorrect url, expected %v, got %v", tc.URL, og.URL) 119 } 120 121 if len(og.Images) > 0 { 122 if og.Images[0].URL != tc.ImageURL { 123 t.Fatalf("incorrect image url, expected %v, got %v", tc.ImageURL, og.Images[0].URL) 124 } 125 } else if tc.ImageURL != "" { 126 t.Fatalf("missing image url, expected %v, got nothing", tc.ImageURL) 127 } 128 }) 129 } 130 } 131 132 func TestOpenGraphDecodeHtmlEntities(t *testing.T) { 133 og := opengraph.NewOpenGraph() 134 og.Title = "Test's are the best.©" 135 og.Description = "Test's are the worst.©" 136 137 openGraphDecodeHtmlEntities(og) 138 139 assert.Equal(t, og.Title, "Test's are the best.©") 140 assert.Equal(t, og.Description, "Test's are the worst.©") 141 }