示例#1
0
func TestFetchWithContentLength(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	progressCh := make(chan ProgressReport)
	done := make(chan bool)

	destDir, err := ioutil.TempDir(os.TempDir(), "content-length")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir), WithConcurrency(50))
	go func() {
		_, err := gf.Fetch(ts.URL, progressCh)
		assert.Ok(t, err)
		done <- true
	}()

	var total int64
	for p := range progressCh {
		//fmt.Printf("\r%d of %d", p.WrittenBytes, p.Total)
		total += p.WrittenBytes
	}
	assert.Equals(t, int64(10485760), total)
	<-done
}
示例#2
0
func TestUnpacking(t *testing.T) {
	image, err := os.Open("./fixtures/test.iso")
	defer image.Close()
	reader, err := NewReader(image)
	assert.Ok(t, err)

	tests := []struct {
		name    string
		isDir   bool
		content string
	}{
		{"/dir1", true, ""},
		{"/dir2", true, ""},
		{"/file.txt", false, "hola amigo\n"},
		{"/dir1/hello.txt", false, "hello there!"},
		{"/dir2/dir3", true, ""},
		{"/dir2/dir3/blah.txt", false, "do you feel me?\n"},
	}

	count := 0
	for {
		fi, err := reader.Next()
		if err == io.EOF {
			break
		}
		assert.Ok(t, err)

		f := fi.(*File)
		//fmt.Printf("%s\n", tests[count].name)
		assert.Equals(t, tests[count].name, f.Name())
		assert.Equals(t, tests[count].isDir, f.IsDir())

		rawBytes := f.Sys()
		if !f.IsDir() {
			assert.Cond(t, rawBytes != nil, "when it is file, content should not be nil")
			content, err := ioutil.ReadAll(rawBytes.(io.Reader))
			assert.Ok(t, err)
			//fmt.Printf("%s -> %s\n", tests[count].name, content)
			assert.Equals(t, tests[count].content, string(content[:]))
		} else {
			assert.Equals(t, nil, rawBytes)
		}
		count++
	}
	assert.Equals(t, 6, count)
}
示例#3
0
func TestEtagSupport(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		w.Header().Add("Etag", "7h153746154w350m3")
		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	destDir, err := ioutil.TempDir(os.TempDir(), "etag")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir), WithConcurrency(1), WithETag())

	// Fetches file for the first tim
	_, err = gf.Fetch(ts.URL+"/test", nil)
	assert.Ok(t, err)

	progressCh := make(chan ProgressReport)
	go func() {
		// Attempts to fetch file once again.
		_, err := gf.Fetch(ts.URL+"/test", progressCh)
		assert.Ok(t, err)
	}()

	var progressCount int
	for range progressCh {
		progressCount++
	}

	// Since the file was already downloaded, there shouldn't be any download
	// progress reported.
	assert.Equals(t, 0, progressCount)

	// Cleans up ~/.gofetch work dir
	dir, err := homedir.Dir()
	assert.Ok(t, err)
	err = os.RemoveAll(filepath.Join(dir, ".gofetch"))
	assert.Ok(t, err)
}
示例#4
0
func TestWithChecksum(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	destDir, err := ioutil.TempDir(os.TempDir(), "checksum-test")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir), WithChecksum("sha512", "4ff6e159db38d46a665f26e9f82b98134238c0457cc82727a5258b7184773e4967068cc0eecf3928ecd079f3aea6e22aac024847c6d76c0329c4635c4b6ae327"))
	_, err = gf.Fetch(ts.URL+"/test", nil)
	assert.Ok(t, err)
}
示例#5
0
func TestFetchWithoutContentLength(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			return
		}
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		_, err = io.Copy(w, file)
		assert.Ok(t, err)
	}))
	defer ts.Close()

	progressCh := make(chan ProgressReport)
	done := make(chan bool)

	destDir, err := ioutil.TempDir(os.TempDir(), "no-content-length")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	gf := New(WithDestDir(destDir))
	go func() {
		_, err := gf.Fetch(ts.URL, progressCh)
		assert.Ok(t, err)
		done <- true
	}()

	var total int64
	for p := range progressCh {
		total += p.WrittenBytes
		assert.Equals(t, int64(-1), p.Total)
	}
	assert.Equals(t, int64(10485760), total)
	<-done
	// Now we can close the test server and let the deferred function to run.
}
示例#6
0
func TestResume(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		file, err := os.Open("./fixtures/test")
		assert.Ok(t, err)
		assert.Cond(t, file != nil, "Failed loading fixture file")
		defer file.Close()

		http.ServeContent(w, r, file.Name(), time.Time{}, file)
	}))
	defer ts.Close()

	destDir, err := ioutil.TempDir(os.TempDir(), "resume")
	assert.Ok(t, err)
	defer os.RemoveAll(destDir)

	chunksDir := filepath.Join(destDir, path.Base(ts.URL)+".chunks")
	err = os.MkdirAll(chunksDir, 0760)
	assert.Ok(t, err)

	fixtureFile, err := os.Open("./fixtures/test-resume")
	assert.Ok(t, err)

	chunkFile, err := os.Create(filepath.Join(chunksDir, "0"))
	assert.Ok(t, err)

	_, err = io.Copy(chunkFile, fixtureFile)
	assert.Ok(t, err)

	fixtureFile.Close()
	chunkFile.Close()

	done := make(chan bool)
	progressCh := make(chan ProgressReport)
	var file *os.File
	gf := New(WithDestDir(destDir), WithConcurrency(1))
	go func() {
		var err error
		file, err = gf.Fetch(ts.URL, progressCh)
		assert.Ok(t, err)
		done <- true
	}()

	var total int64
	for p := range progressCh {
		//fmt.Printf("\r%d of %d", p.WrittenBytes, p.Total)
		total += p.WrittenBytes
	}
	// It should report the complete file size through the channel
	assert.Equals(t, int64(10485760), total)
	<-done
	// Fetch finished and we can now use the file without causing data races.

	// Checks that the downloaded file has the same size as the test fixture
	fi, err := file.Stat()
	assert.Ok(t, err)
	defer file.Close()
	assert.Equals(t, int64(10485760), fi.Size())

	// Checks file integrity
	hasher := sha512.New()
	_, err = io.Copy(hasher, file)
	assert.Ok(t, err)

	result := fmt.Sprintf("%x", hasher.Sum(nil))
	assert.Equals(t, "4ff6e159db38d46a665f26e9f82b98134238c0457cc82727a5258b7184773e4967068cc0eecf3928ecd079f3aea6e22aac024847c6d76c0329c4635c4b6ae327", result)
	file.Close()
}