github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/opengraph_test.go (about)

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