Skip to content

Commit

Permalink
Added more uploadImage tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreitbart committed Apr 25, 2016
1 parent 8771c14 commit 9e251bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
36 changes: 36 additions & 0 deletions serverError_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func TestImgurNotSuccess(t *testing.T) {
if err == nil {
t.Error("GetInfoFromURL() should have failed, but didn't")
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
}
}

func TestJsonError(t *testing.T) {
Expand Down Expand Up @@ -96,6 +103,14 @@ func TestJsonError(t *testing.T) {
if err == nil || ge != nil {
t.Error("GetInfoFromURL() should have failed, but didn't")
}

var im []byte
img, _, err = client.UploadImage(im, "", "binary", "t", "d")

if err == nil || img != nil {
t.Error("UploadImage() should have failed, but didn't")
}

}

func TestServerError(t *testing.T) {
Expand Down Expand Up @@ -142,6 +157,13 @@ func TestServerError(t *testing.T) {
if err == nil {
t.Error("GetInfoFromURL() should have failed, but didn't")
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
}
}

func TestImgurError(t *testing.T) {
Expand Down Expand Up @@ -188,6 +210,13 @@ func TestImgurError(t *testing.T) {
if err == nil {
t.Error("GetInfoFromURL() should have failed, but didn't")
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
}
}

func TestServerDown(t *testing.T) {
Expand Down Expand Up @@ -234,4 +263,11 @@ func TestServerDown(t *testing.T) {
if err == nil {
t.Error("GetInfoFromURL() should have failed, but didn't")
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
}
}
10 changes: 3 additions & 7 deletions uploadImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (client *Client) UploadImage(image []byte, album string, dtype string, titl
}

if !img.Success {
fmt.Printf("form: %v\n", form)
return nil, img.Status, errors.New("Upload to imgur failed with status: " + strconv.Itoa(img.Status))
}

Expand Down Expand Up @@ -109,21 +108,18 @@ func (client *Client) UploadImageFromFile(filename string, album string, title s
client.Log.Infof("*** IMAGE UPLOAD ***\n")
f, err := os.Open(filename)
if err != nil {
err2 := fmt.Errorf("Could not open file %v - Error: %v", filename, err)
return nil, 500, err2
return nil, 500, fmt.Errorf("Could not open file %v - Error: %v", filename, err)
}
defer f.Close()
fileinfo, err := f.Stat()
if err != nil {
err2 := fmt.Errorf("Could not stat file %v - Error: %v", filename, err)
return nil, 500, err2
return nil, 500, fmt.Errorf("Could not stat file %v - Error: %v", filename, err)
}
size := fileinfo.Size()
b := make([]byte, size)
n, err := f.Read(b)
if err != nil || int64(n) != size {
err2 := fmt.Errorf("Could not read file %v - Error: %v", filename, err)
return nil, 500, err2
return nil, 500, fmt.Errorf("Could not read file %v - Error: %v", filename, err)
}

//base := base64.StdEncoding.EncodeToString(b)
Expand Down
2 changes: 1 addition & 1 deletion uploadImage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestUploadImageSimulated(t *testing.T) {
client.Log = new(klogger.CLILogger)
client.ImgurClientID = "testing"

ii, status, err := client.UploadImageFromFile("test_data/testImage.jpg", "", title, descr)
ii, status, err := client.UploadImageFromFile("test_data/testImage.jpg", "ALBUMID", title, descr)

if err != nil || ii == nil {
t.Errorf("UploadImageFromFile() failed with error: %v", err)
Expand Down

0 comments on commit 9e251bc

Please sign in to comment.