123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package dockerfile // import "github.com/docker/docker/builder/dockerfile"
- import (
- "net/http"
- "testing"
- "gotest.tools/v3/assert"
- is "gotest.tools/v3/assert/cmp"
- "gotest.tools/v3/fs"
- )
- func TestIsExistingDirectory(t *testing.T) {
- tmpfile := fs.NewFile(t, "file-exists-test", fs.WithContent("something"))
- defer tmpfile.Remove()
- tmpdir := fs.NewDir(t, "dir-exists-test")
- defer tmpdir.Remove()
- testcases := []struct {
- doc string
- path string
- expected bool
- }{
- {
- doc: "directory exists",
- path: tmpdir.Path(),
- expected: true,
- },
- {
- doc: "path doesn't exist",
- path: "/bogus/path/does/not/exist",
- expected: false,
- },
- {
- doc: "file exists",
- path: tmpfile.Path(),
- expected: false,
- },
- }
- for _, testcase := range testcases {
- result, err := isExistingDirectory(testcase.path)
- if !assert.Check(t, err) {
- continue
- }
- assert.Check(t, is.Equal(testcase.expected, result), testcase.doc)
- }
- }
- func TestGetFilenameForDownload(t *testing.T) {
- testcases := []struct {
- path string
- disposition string
- expected string
- }{
- {
- path: "https://www.example.com/",
- expected: "",
- },
- {
- path: "https://www.example.com/xyz",
- expected: "xyz",
- },
- {
- path: "https://www.example.com/xyz.html",
- expected: "xyz.html",
- },
- {
- path: "https://www.example.com/xyz/",
- expected: "",
- },
- {
- path: "https://www.example.com/xyz/uvw",
- expected: "uvw",
- },
- {
- path: "https://www.example.com/xyz/uvw.html",
- expected: "uvw.html",
- },
- {
- path: "https://www.example.com/xyz/uvw/",
- expected: "",
- },
- {
- path: "/",
- expected: "",
- },
- {
- path: "/xyz",
- expected: "xyz",
- },
- {
- path: "/xyz.html",
- expected: "xyz.html",
- },
- {
- path: "/xyz/",
- expected: "",
- },
- {
- path: "/xyz/",
- disposition: "attachment; filename=xyz.html",
- expected: "xyz.html",
- },
- {
- disposition: "",
- expected: "",
- },
- {
- disposition: "attachment; filename=xyz",
- expected: "xyz",
- },
- {
- disposition: "attachment; filename=xyz.html",
- expected: "xyz.html",
- },
- {
- disposition: `attachment; filename="xyz"`,
- expected: "xyz",
- },
- {
- disposition: `attachment; filename="xyz.html"`,
- expected: "xyz.html",
- },
- {
- disposition: `attachment; filename="/xyz.html"`,
- expected: "xyz.html",
- },
- {
- disposition: `attachment; filename="/xyz/uvw"`,
- expected: "uvw",
- },
- {
- disposition: `attachment; filename="Naïve file.txt"`,
- expected: "Naïve file.txt",
- },
- }
- for _, testcase := range testcases {
- resp := http.Response{
- Header: make(map[string][]string),
- }
- if testcase.disposition != "" {
- resp.Header.Add("Content-Disposition", testcase.disposition)
- }
- filename := getFilenameForDownload(testcase.path, &resp)
- assert.Check(t, is.Equal(testcase.expected, filename))
- }
- }
|