github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/api4/openGraph_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/http/httptest" 10 "strings" 11 12 "testing" 13 14 "github.com/mattermost/mattermost-server/model" 15 ) 16 17 func TestGetOpenGraphMetadata(t *testing.T) { 18 th := Setup().InitBasic() 19 defer th.TearDown() 20 21 Client := th.Client 22 23 enableLinkPreviews := *th.App.Config().ServiceSettings.EnableLinkPreviews 24 allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections 25 defer func() { 26 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews }) 27 th.App.UpdateConfig(func(cfg *model.Config) { 28 cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections 29 }) 30 }() 31 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = true }) 32 th.App.UpdateConfig(func(cfg *model.Config) { 33 *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost 127.0.0.1" 34 }) 35 36 ogDataCacheMissCount := 0 37 38 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 39 ogDataCacheMissCount++ 40 41 if r.URL.Path == "/og-data/" { 42 fmt.Fprintln(w, ` 43 <html><head><meta property="og:type" content="article" /> 44 <meta property="og:title" content="Test Title" /> 45 <meta property="og:url" content="http://example.com/" /> 46 </head><body></body></html> 47 `) 48 } else if r.URL.Path == "/no-og-data/" { 49 fmt.Fprintln(w, `<html><head></head><body></body></html>`) 50 } 51 })) 52 53 for _, data := range [](map[string]interface{}){ 54 {"path": "/og-data/", "title": "Test Title", "cacheMissCount": 1}, 55 {"path": "/no-og-data/", "title": "", "cacheMissCount": 2}, 56 57 // Data should be cached for following 58 {"path": "/og-data/", "title": "Test Title", "cacheMissCount": 2}, 59 {"path": "/no-og-data/", "title": "", "cacheMissCount": 2}, 60 } { 61 62 openGraph, resp := Client.OpenGraph(ts.URL + data["path"].(string)) 63 CheckNoError(t, resp) 64 if strings.Compare(openGraph["title"], data["title"].(string)) != 0 { 65 t.Fatal(fmt.Sprintf( 66 "OG data title mismatch for path \"%s\". Expected title: \"%s\". Actual title: \"%s\"", 67 data["path"].(string), data["title"].(string), openGraph["title"], 68 )) 69 } 70 71 if ogDataCacheMissCount != data["cacheMissCount"].(int) { 72 t.Fatal(fmt.Sprintf( 73 "Cache miss count didn't match. Expected value %d. Actual value %d.", 74 data["cacheMissCount"].(int), ogDataCacheMissCount, 75 )) 76 } 77 } 78 79 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = false }) 80 _, resp := Client.OpenGraph(ts.URL + "/og-data/") 81 CheckNotImplementedStatus(t, resp) 82 }