Skip to content

Commit

Permalink
Test for direct image URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreitbart committed Apr 25, 2016
1 parent 13a0acc commit bf24c55
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions fromURL_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit bf24c55

Please # to comment.