summaryrefslogtreecommitdiffstats
path: root/api4/openGraph_test.go
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-05-10 12:44:18 +0200
committerGeorge Goldberg <george@gberg.me>2017-05-10 11:44:18 +0100
commit16581ae431ffeae97db18eb8672232505a7ce3c0 (patch)
treeb87317febf6f2ee4588fa3c8b48ed7a1cf81c048 /api4/openGraph_test.go
parentb9f4ced52b65d147b57d7028f6df4935a6aea8f9 (diff)
downloadchat-16581ae431ffeae97db18eb8672232505a7ce3c0.tar.gz
chat-16581ae431ffeae97db18eb8672232505a7ce3c0.tar.bz2
chat-16581ae431ffeae97db18eb8672232505a7ce3c0.zip
implement open graph metadata for apiV4 (#6343)
Diffstat (limited to 'api4/openGraph_test.go')
-rw-r--r--api4/openGraph_test.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/api4/openGraph_test.go b/api4/openGraph_test.go
new file mode 100644
index 000000000..958abf604
--- /dev/null
+++ b/api4/openGraph_test.go
@@ -0,0 +1,73 @@
+// 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/platform/utils"
+)
+
+func TestGetOpenGraphMetadata(t *testing.T) {
+ th := Setup().InitBasic()
+ Client := th.Client
+
+ enableLinkPreviews := *utils.Cfg.ServiceSettings.EnableLinkPreviews
+ defer func() {
+ *utils.Cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews
+ }()
+ *utils.Cfg.ServiceSettings.EnableLinkPreviews = true
+
+ 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,
+ ))
+ }
+ }
+
+ *utils.Cfg.ServiceSettings.EnableLinkPreviews = false
+ _, resp := Client.OpenGraph(ts.URL + "/og-data/")
+ CheckNotImplementedStatus(t, resp)
+}