func TestZipFromHTTPSources(t *testing.T) { ts := tServer() defer ts.Close() url1 := fmt.Sprintf("%s/index.html", ts.URL) url2 := fmt.Sprintf("%s/posts", ts.URL) url3 := fmt.Sprintf("%s/api/data.json", ts.URL) out := new(bytes.Buffer) zip := NewZip(sources.HTTP) zip.Add(url1) zip.Add(url2, url3) n, err := zip.WriteTo(out) if err := zip.Close(); err != nil { t.Fatal(err) } assert.Nil(t, err) assert.Equal(t, int64(38), n) assert.Equal(t, int64(38), zip.N) assert.Equal(t, int64(38), zip.UncompressedSize) assert.Equal(t, int64(56), zip.CompressedSize) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"index.html", "Hello World!"}, {"posts", "Post Body"}, {"data.json", `{"data": ["one"]}`}, }) }
func TestEntryCreatedIfReadCloserIsNotNilOnError(t *testing.T) { c := ioutil.NopCloser(bytes.NewReader([]byte("uh-oh"))) sourceFn := tSourceFunc(c) out := new(bytes.Buffer) zip := NewZip(sourceFn) zip.Add("good", "error", "andgoodagain") _, err := zip.WriteTo(out) if err := zip.Close(); err != nil { t.Fatal(err) } assert.NotNil(t, err) assert.Equal(t, fmt.Sprintf("1 error(s):\n\n%s", "* uh-oh"), err.Error()) ze := err.(Error) assert.Equal(t, 1, len(err.(Error))) assert.Equal(t, "uh-oh", ze[0].Error()) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"good", "Good!"}, {"error", "uh-oh"}, {"andgoodagain", "Good!"}, }) }
func TestZipFromFSSources(t *testing.T) { out := new(bytes.Buffer) zip := NewZip(sources.FS) zip.Add("sample/file1.txt") zip.Add("sample/file2.txt") zip.Add("sample/file3.txt") n, err := zip.WriteTo(out) if err := zip.Close(); err != nil { t.Fatal(err) } assert.Nil(t, err) assert.Equal(t, int64(11), n) assert.Equal(t, int64(11), zip.N) assert.Equal(t, int64(11), zip.UncompressedSize) assert.Equal(t, int64(29), zip.CompressedSize) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"file1.txt", "One"}, {"file2.txt", "Two"}, {"file3.txt", "Three"}, }) }
func TestAddEntry(t *testing.T) { sourceFn := tSourceFunc(nil) out := new(bytes.Buffer) zip := NewZip(sourceFn) zip.Add("good", "error", "andgoodagain") n, err := zip.WriteTo(out) r := bytes.NewBufferString(err.Error()) z, er := zip.AddEntry("archivr-errors.txt", r) if er != nil { t.Fatal(er) } if err := zip.Close(); err != nil { t.Fatal(err) } assert.NotNil(t, err) assert.Equal(t, fmt.Sprintf("1 error(s):\n\n%s", "* uh-oh"), err.Error()) assert.Equal(t, int64(10), n) assert.Equal(t, int64(20), z) assert.Equal(t, n+z, zip.N) assert.Equal(t, int64(30), zip.UncompressedSize) assert.Equal(t, int64(48), zip.CompressedSize) ze := err.(Error) assert.Equal(t, 1, len(err.(Error))) assert.Equal(t, "uh-oh", ze[0].Error()) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"good", "Good!"}, {"andgoodagain", "Good!"}, {"archivr-errors.txt", "1 error(s):\n\n* uh-oh"}, }) }