Tests for DownloadTryCompression + error return bugfix.

This commit is contained in:
Andrey Smirnov
2013-12-17 19:33:22 +04:00
parent 4b3d74c64e
commit 20524a839d
2 changed files with 79 additions and 2 deletions
+5 -2
View File
@@ -164,12 +164,15 @@ func DownloadTryCompression(downloader Downloader, url string) (io.Reader, *os.F
var err error var err error
for _, method := range compressionMethods { for _, method := range compressionMethods {
file, err := DownloadTemp(downloader, url+method.extenstion) var file *os.File
file, err = DownloadTemp(downloader, url+method.extenstion)
if err != nil { if err != nil {
continue continue
} }
uncompressed, err := method.transformation(file) var uncompressed io.Reader
uncompressed, err = method.transformation(file)
if err != nil { if err != nil {
continue continue
} }
+74
View File
@@ -1,6 +1,8 @@
package utils package utils
import ( import (
"errors"
"io"
"io/ioutil" "io/ioutil"
. "launchpad.net/gocheck" . "launchpad.net/gocheck"
"os" "os"
@@ -101,3 +103,75 @@ func (s *DownloaderSuite) TestDownloadTempError(c *C) {
c.Assert(f, IsNil) c.Assert(f, IsNil)
c.Assert(err, ErrorMatches, "HTTP code 404.*") c.Assert(err, ErrorMatches, "HTTP code 404.*")
} }
const (
bzipData = "BZh91AY&SY\xcc\xc3q\xd4\x00\x00\x02A\x80\x00\x10\x02\x00\x0c\x00 \x00!\x9ah3M\x19\x97\x8b\xb9\"\x9c(Hfa\xb8\xea\x00"
gzipData = "\x1f\x8b\x08\x00\xc8j\xb0R\x00\x03+I-.\xe1\x02\x00\xc65\xb9;\x05\x00\x00\x00"
rawData = "test"
)
func (s *DownloaderSuite) TestDownloadTryCompression(c *C) {
var buf []byte
// bzip2 only available
buf = make([]byte, 4)
d := NewFakeDownloader()
d.ExpectResponse("http://example.com/file.bz2", bzipData)
r, file, err := DownloadTryCompression(d, "http://example.com/file")
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
// bzip2 not available, but gz is
buf = make([]byte, 4)
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectResponse("http://example.com/file.gz", gzipData)
r, file, err = DownloadTryCompression(d, "http://example.com/file")
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
// bzip2 & gzip not available, but raw is
buf = make([]byte, 4)
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.gz", errors.New("404"))
d.ExpectResponse("http://example.com/file", rawData)
r, file, err = DownloadTryCompression(d, "http://example.com/file")
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
// gzip available, but broken
buf = make([]byte, 4)
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectResponse("http://example.com/file.gz", "x")
d.ExpectResponse("http://example.com/file", "recovered")
r, file, err = DownloadTryCompression(d, "http://example.com/file")
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
c.Assert(string(buf), Equals, "reco")
c.Assert(d.Empty(), Equals, true)
}
func (s *DownloaderSuite) TestDownloadTryCompressionErrors(c *C) {
d := NewFakeDownloader()
_, _, err := DownloadTryCompression(d, "http://example.com/file")
c.Assert(err, ErrorMatches, "unexpected request.*")
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.gz", errors.New("404"))
d.ExpectError("http://example.com/file", errors.New("403"))
_, _, err = DownloadTryCompression(d, "http://example.com/file")
c.Assert(err, ErrorMatches, "403")
}