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
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/mattermost/mattermost-server/model"
)
func TestGetOpenGraphMetadata(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
enableLinkPreviews := *th.App.Config().ServiceSettings.EnableLinkPreviews
allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections
defer func() {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews })
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections
})
}()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = true })
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost 127.0.0.1"
})
ogDataCacheMissCount := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ogDataCacheMissCount++
if r.URL.Path == "/og-data/" {
fmt.Fprintln(w, `
<html><head><meta property="og:type" content="article" />
<meta property="og:title" content="Test Title" />
<meta property="og:url" content="http://example.com/" />
</head><body></body></html>
`)
} else if r.URL.Path == "/no-og-data/" {
fmt.Fprintln(w, `<html><head></head><body></body></html>`)
}
}))
for _, data := range [](map[string]interface{}){
{"path": "/og-data/", "title": "Test Title", "cacheMissCount": 1},
{"path": "/no-og-data/", "title": "", "cacheMissCount": 2},
// Data should be cached for following
{"path": "/og-data/", "title": "Test Title", "cacheMissCount": 2},
{"path": "/no-og-data/", "title": "", "cacheMissCount": 2},
} {
openGraph, resp := Client.OpenGraph(ts.URL + data["path"].(string))
CheckNoError(t, resp)
if strings.Compare(openGraph["title"], data["title"].(string)) != 0 {
t.Fatal(fmt.Sprintf(
"OG data title mismatch for path \"%s\". Expected title: \"%s\". Actual title: \"%s\"",
data["path"].(string), data["title"].(string), openGraph["title"],
))
}
if ogDataCacheMissCount != data["cacheMissCount"].(int) {
t.Fatal(fmt.Sprintf(
"Cache miss count didn't match. Expected value %d. Actual value %d.",
data["cacheMissCount"].(int), ogDataCacheMissCount,
))
}
}
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = false })
_, resp := Client.OpenGraph(ts.URL + "/og-data/")
CheckNotImplementedStatus(t, resp)
}
|