Skip to content

Commit

Permalink
add content-negociation and tests on zip download
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain committed Jan 31, 2017
1 parent c0cad5f commit 767d3fb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/vfs/download_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
}

func cleanDownloadStoreInterval() {
for _ = range time.Tick(downloadStoreCleanInterval) {
for range time.Tick(downloadStoreCleanInterval) {
cleanDownloadStore()
}
}
Expand Down
6 changes: 6 additions & 0 deletions web/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ func ArchiveDownloadCreateHandler(c echo.Context) error {
archive.Name = "archive"
}
instance := middlewares.GetInstance(c)

// if accept header is application/zip, send the archive immediately
if c.Request().Header.Get("Accept") == "application/zip" {
return archive.Serve(instance, c.Response())
}

secret, err := vfs.GetStore(instance.Domain).AddArchive(archive)
if err != nil {
return wrapVfsError(err)
Expand Down
44 changes: 43 additions & 1 deletion web/files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ func TestArchiveNoFiles(t *testing.T) {
assert.Equal(t, `"Can't create an archive with no files"`, string(msg))
}

func TestArchive(t *testing.T) {
func TestArchiveDirectDownload(t *testing.T) {
res1, data1 := createDir(t, "/files/?Name=archive&Type=directory")
if !assert.Equal(t, 201, res1.StatusCode) {
return
Expand All @@ -929,6 +929,7 @@ func TestArchive(t *testing.T) {
}
}

// direct download
body := bytes.NewBufferString(`{
"data": {
"attributes": {
Expand All @@ -940,6 +941,47 @@ func TestArchive(t *testing.T) {
}
}
}`)

req, err := http.NewRequest("POST", ts.URL+"/files/archive", body)
req.Header.Add("Content-Type", "application/zip")
req.Header.Add("Accept", "application/zip")
assert.NoError(t, err)
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
if !assert.Equal(t, 200, res.StatusCode) {
b, err2 := ioutil.ReadAll(res.Body)
fmt.Println(err2, string(b))
}
assert.Equal(t, "application/zip", res.Header.Get("Content-Type"))

}

func TestArchiveCreateAndDownload(t *testing.T) {
res1, data1 := createDir(t, "/files/?Name=archive2&Type=directory")
if !assert.Equal(t, 201, res1.StatusCode) {
return
}
dirID, _ := extractDirData(t, data1)
names := []string{"foo", "bar", "baz"}
for _, name := range names {
res2, _ := createDir(t, "/files/"+dirID+"?Name="+name+".jpg&Type=file")
if !assert.Equal(t, 201, res2.StatusCode) {
return
}
}

body := bytes.NewBufferString(`{
"data": {
"attributes": {
"files": [
"/archive/foo.jpg",
"/archive/bar.jpg",
"/archive/baz.jpg"
]
}
}
}`)

res, err := http.Post(ts.URL+"/files/archive", "application/vnd.api+json", body)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
Expand Down

0 comments on commit 767d3fb

Please sign in to comment.