From bf24c5572af00eb2b4eb127b5423bd2210390e00 Mon Sep 17 00:00:00 2001 From: Jens Breitbart Date: Mon, 25 Apr 2016 15:37:05 +0200 Subject: [PATCH] Test for direct image URLs. --- fromURL_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/fromURL_test.go b/fromURL_test.go index 575517b..ab8ca51 100644 --- a/fromURL_test.go +++ b/fromURL_test.go @@ -350,3 +350,114 @@ func TestGetFromURLImageNoID(t *testing.T) { t.FailNow() } } + +func TestGetURLDirectImageSimulated(t *testing.T) { + httpC, server := testHTTPClientJSON("{\"data\":{\"id\":\"ClF8rLe\",\"title\":null,\"description\":null,\"datetime\":1451248840,\"type\":\"image\\/jpeg\",\"animated\":false,\"width\":2448,\"height\":3264,\"size\":1071339,\"views\":176,\"bandwidth\":188555664,\"vote\":null,\"favorite\":false,\"nsfw\":null,\"section\":null,\"account_url\":null,\"account_id\":null,\"in_gallery\":false,\"link\":\"http:\\/\\/i.imgur.com\\/ClF8rLe.jpg\"},\"success\":true,\"status\":200}") + defer server.Close() + + client := new(Client) + client.HTTPClient = httpC + client.Log = new(klogger.CLILogger) + client.ImgurClientID = "testing" + + ge, status, err := client.GetInfoFromURL("http://i.imgur.com/ClF8rLe.jpg") + + if err != nil { + t.Errorf("GetInfoFromURL() failed with error: %v", err) + t.FailNow() + } + + if ge.Album != nil || ge.GAlbum != nil { + t.Error("GetInfoFromURL() failed. Returned wrong type.") + t.FailNow() + } + + if ge.Image == nil && ge.GImage == nil { + t.FailNow() + } + + if ge.Image != nil { + img := ge.Image + + if img.Animated != false || img.Bandwidth != 188555664 || img.Datetime != 1451248840 || img.Description != "" || img.Height != 3264 || img.Width != 2448 || img.ID != "ClF8rLe" || img.Link != "http://i.imgur.com/ClF8rLe.jpg" || img.Views != 176 { + t.Fail() + } + } + + if ge.GImage != nil { + img := ge.GImage + + if img.Animated != false || img.Bandwidth != 188555664 || img.Datetime != 1451248840 || img.Description != "" || img.Height != 3264 || img.Width != 2448 || img.ID != "ClF8rLe" || img.Link != "http://i.imgur.com/ClF8rLe.jpg" || img.Views != 176 { + t.Fail() + } + } + + if status != 200 { + t.Fail() + } +} + +func TestGetURLDirectImageReal(t *testing.T) { + key := os.Getenv("IMGURCLIENTID") + if key == "" { + t.Skip("IMGURCLIENTID environment variable not set.") + } + + client := new(Client) + client.HTTPClient = new(http.Client) + client.Log = new(klogger.CLILogger) + client.ImgurClientID = key + + ge, status, err := client.GetInfoFromURL("http://i.imgur.com/ClF8rLe.jpg") + + if err != nil { + t.Errorf("GetInfoFromURL() failed with error: %v", err) + t.FailNow() + } + + if ge.Album != nil || ge.GAlbum != nil { + t.Error("GetInfoFromURL() failed. Returned wrong type.") + t.FailNow() + } + + if ge.Image == nil && ge.GImage == nil { + t.FailNow() + } + + if ge.Image != nil { + img := ge.Image + + if img.Animated != false || img.Datetime != 1451248840 || img.Description != "" || img.Height != 3264 || img.Width != 2448 || img.ID != "ClF8rLe" || img.Link != "http://i.imgur.com/ClF8rLe.jpg" { + t.Fail() + } + } + + if ge.GImage != nil { + img := ge.GImage + + if img.Animated != false || img.Datetime != 1451248840 || img.Description != "" || img.Height != 3264 || img.Width != 2448 || img.ID != "ClF8rLe" || img.Link != "http://i.imgur.com/ClF8rLe.jpg" { + t.Fail() + } + } + + if status != 200 { + t.Fail() + } +} + +func TestGetFromURLDirectImageNoID(t *testing.T) { + httpC, server := testHTTPClient500() + defer server.Close() + + client := new(Client) + client.HTTPClient = httpC + client.Log = new(klogger.CLILogger) + client.ImgurClientID = "testing" + + _, _, err := client.GetInfoFromURL("http://i.imgur.com/") + + if err == nil { + t.Error("GetInfoFromURL() did not failed but should have.") + t.FailNow() + } +}