Esempio n. 1
0
func Test_fetchGifCompareWithPng_Fail(t *testing.T) {

	ts := testutils.TestImgServer("gif")
	defer ts.Close()

	data, err := fetch(ts.URL)
	if err != nil {
		t.FailNow()
	}

	imgServer, err := gif.Decode(bytes.NewReader(data))
	if err != nil {
		t.FailNow()
	}

	file, _ := os.Open("testdata/test.png")
	defer file.Close()
	imgDisk, _, _ := image.Decode(file)

	if reflect.DeepEqual(imgDisk, imgServer) {
		_, file, line, _ := runtime.Caller(0)
		fmt.Printf("%s:%d:\n\ncall fetch(%#v)\n\texp: %#v\n\n\tgot: %#v\n\n",
			filepath.Base(file), line, ts.URL, imgDisk, imgServer)
		t.FailNow()
	}
}
Esempio n. 2
0
func Test_imageFile_testReturnedError(t *testing.T) {
	tisj := testutils.TestImgServer("jpg")
	defer tisj.Close()
	tisg := testutils.TestImgServer("gif")
	defer tisg.Close()

	type inputs struct {
		url string
		nr  string
	}

	tests := []struct {
		in   inputs
		want error
	}{
		//{in: inputs{tisg.URL, "1"}, want: fmt.Errorf("unsupported format: gif")}, // test gif -> fail
		{in: inputs{tisg.URL, "1"}, want: nil}, // test gif -> true (gif is supported now)
		{ // wrong MIME type  --> fail
			// cannot test against testServer as its port in the url is random
			in:   inputs{"https://www.cs.cmu.edu/~chuck/jokepg/joke_19970213_01.txt#", "1"},
			want: fmt.Errorf("has NOT mime content-type image")}, // test txt -> fail
		{ // invalid url, get error from fetch(url)
			// cannot test against testServer as its port in the url is random
			in:   inputs{"invalidURL", "1"},
			want: fmt.Errorf("fetch: Get invalidURL: unsupported protocol scheme \"\""),
		},
		{in: inputs{tisj.URL, "1"}, want: nil}, // test jpeg -> ok
	}

	for _, test := range tests {
		_, got := imageFile(test.in.url)
		if !reflect.DeepEqual(test.want, got) {
			_, file, line, _ := runtime.Caller(0)
			fmt.Printf("%s:%d:\n\ncall imageFile(%#v)\n\texp: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, test.in, test.want, got.Error())
			t.FailNow()
		}
	}
	testutils.Cleanup()
}