I'm running go 1.0.3 on my Ubuntu 12.04.1 Laptop and I've stumbled upon a problem where if I run some code in main(), it behaves much more differently than if I run it with go test.
Here's my example:
From main.go
package main
import (
"image"
"image/jpeg"
"fmt"
"myproj/htmlutil"
[some imports removed]
)
func main() {
img, err := htmlutil.GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
fmt.Println("There was a problem ",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
From myproj/htmlutil_test.go
package htmlutil
import (
"image"
"fmt"
"testing"
[some imports removed]
)
func TestGetImageFromURL(t *testing.T){
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
t.Fatalf("There was a problem %q",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
and the function that they call, GetResizedImageFromWeb(), is in myproj/htmlutil.go:
package htmlutil
import (
"errors"
"fmt"
"image"
"io/ioutil"
"net/http"
[some imports removed]
)
func GetResizedImageFromWeb(imageURL string) (image.Image, error) {
resp, err := http.Get(imageURL)
if err != nil {
return nil, errors.New(fmt.Sprint("There was a problem reading the site %q Debug[%s]",imageURL, err))
}
defer resp.Body.Close()
//Decode the image using image's general purpose decoder
image, s, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return resizeImage(image), nil
}
When I run "go run main.go" from the command line, I see the bounds of the image from the url and can save it as a jpg file on disk if I want using a function in main.go. However, when I run "go test" from the htmlutil package, I get the following error:
There was a problem "image: unknown format"
What is causing the problem to only fail in the unit tests? What am I doing wrong?
My only guess is that for what ever reason, the html.Get() isn't returning all the data in the testing scenario, but I'm still baffled as to why that happens.