diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2020-10-13 13:45:20 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2020-10-13 14:03:02 +0300 |
commit | 0ef6b5982e26f5957fbe5eaf0332f73625a3d24f (patch) | |
tree | 76c9b0e5a16e9d19d0dd911029f77eec7512ddef /internal/serving/disk/zip | |
parent | 6755ba75aff4044f7dfa40578426195b9922907a (diff) |
Fix support for archives without directory structure
In case of archives that do not store directories
we would fail to automatically serve `index.html`
for a `/` type of request.
This makes us create directories when traversing
the file list.
```
Archive: public-without-dirs.zip
Length Date Time Name
--------- ---------- ----- ----
40 2020-09-15 02:47 public/subdir/hello.html
14 2020-09-15 03:35 public/subdir/2bp3Qzs9CCW7cGnxhghdavZ2bJDTzvu2mrj6O8Yqjm3YMRozRZULxBBKzJXCK16GlsvO1GlbCyONf2LTCndJU9cIr5T3PLDN7XnfG00lEmf9DWHPXiAbbi0v8ioSjnoTqdyjELVKuhsGRGxeV9RptLMyGnbpJx1w2uECiUQSHrRVQNuq2xoHLlk30UAmis1EhGXP5kKprzHxuavsKMdT4XRP0d79tie4tjqtfRsP4y60hmNS1vSujrxzhDa
33 2020-09-15 02:47 public/subdir/linked.html
31 2020-09-15 02:47 public/404.html
33 2020-09-15 02:47 public/index.html
258 2020-10-13 12:40 public/bad_symlink.html
18 2020-10-13 12:40 public/symlink.html
```
Diffstat (limited to 'internal/serving/disk/zip')
-rw-r--r-- | internal/serving/disk/zip/serving_test.go | 64 |
1 files changed, 46 insertions, 18 deletions
diff --git a/internal/serving/disk/zip/serving_test.go b/internal/serving/disk/zip/serving_test.go index 14fdabdf..e95432ae 100644 --- a/internal/serving/disk/zip/serving_test.go +++ b/internal/serving/disk/zip/serving_test.go @@ -13,32 +13,60 @@ import ( ) func TestZip_ServeFileHTTP(t *testing.T) { - testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip") + testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public-without-dirs.zip") defer cleanup() - s := Instance() - w := httptest.NewRecorder() - r := httptest.NewRequest("GET", "http://zip.gitlab.io/zip/index.html", nil) - handler := serving.Handler{ - Writer: w, - Request: r, - LookupPath: &serving.LookupPath{ - Prefix: "", - Path: testServerURL + "/public.zip", + tests := map[string]struct { + path string + expectedStatus int + expectedBody string + }{ + "accessing /index.html": { + path: "/index.html", + expectedStatus: http.StatusOK, + expectedBody: "zip.gitlab.io/project/index.html\n", + }, + "accessing /": { + path: "/", + expectedStatus: http.StatusOK, + expectedBody: "zip.gitlab.io/project/index.html\n", + }, + "accessing without /": { + path: "", + expectedStatus: http.StatusFound, + expectedBody: `<a href="//zip.gitlab.io/zip/">Found</a>.`, }, - SubPath: "/index.html", } - require.True(t, s.ServeFileHTTP(handler)) + s := Instance() + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "http://zip.gitlab.io/zip"+test.path, nil) + + handler := serving.Handler{ + Writer: w, + Request: r, + LookupPath: &serving.LookupPath{ + Prefix: "/zip/", + Path: testServerURL + "/public.zip", + }, + SubPath: test.path, + } - resp := w.Result() - defer resp.Body.Close() + require.True(t, s.ServeFileHTTP(handler)) - require.Equal(t, http.StatusOK, resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) - require.NoError(t, err) + resp := w.Result() + defer resp.Body.Close() - require.Contains(t, string(body), "zip.gitlab.io/project/index.html\n") + require.Equal(t, test.expectedStatus, resp.StatusCode) + body, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + require.Contains(t, string(body), test.expectedBody) + }) + } } var chdirSet = false |