From 0206be02753743afea81843e4b19cd5657cbad73 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Mon, 10 Oct 2016 08:10:14 -0500 Subject: [PATCH] hugolib: Prevent TestShortcodeTweet from accessing network Overload `getJSON` and return mock response. Also standardized error reporting strings in tests. Fixes #2359 --- hugolib/embedded_shortcodes_test.go | 39 +++++++++++++++++++---------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/hugolib/embedded_shortcodes_test.go b/hugolib/embedded_shortcodes_test.go index 7dda21d41..b0ce1a97a 100644 --- a/hugolib/embedded_shortcodes_test.go +++ b/hugolib/embedded_shortcodes_test.go @@ -14,6 +14,7 @@ package hugolib import ( + "encoding/json" "fmt" "net/url" "os" @@ -113,7 +114,7 @@ void do(); } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } } @@ -153,7 +154,7 @@ func TestShortcodeFigure(t *testing.T) { } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } } @@ -178,7 +179,7 @@ func TestShortcodeSpeakerdeck(t *testing.T) { } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } } @@ -213,7 +214,7 @@ func TestShortcodeYoutube(t *testing.T) { } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } } @@ -248,7 +249,7 @@ func TestShortcodeVimeo(t *testing.T) { } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } } @@ -277,25 +278,37 @@ func TestShortcodeGist(t *testing.T) { } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } } func TestShortcodeTweet(t *testing.T) { - if testing.Short() { - t.Skip("skipping Twitter test in short mode.") - } - for i, this := range []struct { - in, expected string + in, resp, expected string }{ { `{{< tweet 666616452582129664 >}}`, - "(?s)^

Hugo 0.15 will have 30%\\+ faster render times thanks to this commit https://t.co/FfzhM8bNhT #gohugo #golang https://t.co/ITbMNU2BUf

— Steve Francia \\(@spf13\\) November 17, 2015
.*?$", + `{"url":"https:\/\/twitter.com\/spf13\/status\/666616452582129664","author_name":"Steve Francia","author_url":"https:\/\/twitter.com\/spf13","html":"\u003Cblockquote class=\"twitter-tweet\"\u003E\u003Cp lang=\"en\" dir=\"ltr\"\u003EHugo 0.15 will have 30%+ faster render times thanks to this commit \u003Ca href=\"https:\/\/t.co\/FfzhM8bNhT\"\u003Ehttps:\/\/t.co\/FfzhM8bNhT\u003C\/a\u003E \u003Ca href=\"https:\/\/twitter.com\/hashtag\/gohugo?src=hash\"\u003E#gohugo\u003C\/a\u003E \u003Ca href=\"https:\/\/twitter.com\/hashtag\/golang?src=hash\"\u003E#golang\u003C\/a\u003E \u003Ca href=\"https:\/\/t.co\/ITbMNU2BUf\"\u003Ehttps:\/\/t.co\/ITbMNU2BUf\u003C\/a\u003E\u003C\/p\u003E— Steve Francia (@spf13) \u003Ca href=\"https:\/\/twitter.com\/spf13\/status\/666616452582129664\"\u003ENovember 17, 2015\u003C\/a\u003E\u003C\/blockquote\u003E\n\u003Cscript async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"\u003E\u003C\/script\u003E","width":550,"height":null,"type":"rich","cache_age":"3153600000","provider_name":"Twitter","provider_url":"https:\/\/twitter.com","version":"1.0"}`, + `(?s)^

Hugo 0.15 will have 30%. faster render times thanks to this commit https://t.co/FfzhM8bNhT #gohugo #golang https://t.co/ITbMNU2BUf

— Steve Francia .@spf13. November 17, 2015
.*?$`, }, } { + // overload getJSON to return mock API response from Twitter + tweetFuncMap := template.FuncMap{ + "getJSON": func(urlParts ...string) interface{} { + var v interface{} + err := json.Unmarshal([]byte(this.resp), &v) + if err != nil { + t.Fatalf("[%d] unexpected error in json.Unmarshal: %s", i, err) + return err + } + return v + }, + } + templ := tpl.New() + templ.Lookup("").Funcs(tweetFuncMap) + p, _ := pageFromString(simplePage, "simple.md") cacheFileID := viper.GetString("CacheDir") + url.QueryEscape("https://api.twitter.com/1/statuses/oembed.json?id=666616452582129664") defer os.Remove(cacheFileID) @@ -308,7 +321,7 @@ func TestShortcodeTweet(t *testing.T) { } if !matched { - t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output) + t.Errorf("[%d] unexpected rendering, got %s\n", i, output) } } }