2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2017-05-25 17:03:29 -04:00
|
|
|
|
|
|
|
import (
|
2017-08-14 20:33:31 -04:00
|
|
|
"net/http"
|
2017-05-25 17:03:29 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-23 17:25:00 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/fs"
|
2017-05-25 17:03:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsExistingDirectory(t *testing.T) {
|
2017-08-23 17:25:00 -04:00
|
|
|
tmpfile := fs.NewFile(t, "file-exists-test", fs.WithContent("something"))
|
2017-05-25 17:03:29 -04:00
|
|
|
defer tmpfile.Remove()
|
2017-08-23 17:25:00 -04:00
|
|
|
tmpdir := fs.NewDir(t, "dir-exists-test")
|
2017-05-25 17:03:29 -04:00
|
|
|
defer tmpdir.Remove()
|
|
|
|
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
|
|
|
path string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "directory exists",
|
2017-08-23 17:25:00 -04:00
|
|
|
path: tmpdir.Path(),
|
2017-05-25 17:03:29 -04:00
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "path doesn't exist",
|
|
|
|
path: "/bogus/path/does/not/exist",
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "file exists",
|
2017-08-23 17:25:00 -04:00
|
|
|
path: tmpfile.Path(),
|
2017-05-25 17:03:29 -04:00
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
2017-08-03 20:22:00 -04:00
|
|
|
result, err := isExistingDirectory(©Endpoint{driver: containerfs.NewLocalDriver(), path: testcase.path})
|
2018-03-13 15:28:34 -04:00
|
|
|
if !assert.Check(t, err) {
|
2017-05-25 17:03:29 -04:00
|
|
|
continue
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(testcase.expected, result), testcase.doc)
|
2017-05-25 17:03:29 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 20:33:31 -04:00
|
|
|
|
|
|
|
func TestGetFilenameForDownload(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
path string
|
|
|
|
disposition string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "http://www.example.com/",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "http://www.example.com/xyz",
|
|
|
|
expected: "xyz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "http://www.example.com/xyz.html",
|
|
|
|
expected: "xyz.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "http://www.example.com/xyz/",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "http://www.example.com/xyz/uvw",
|
|
|
|
expected: "uvw",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "http://www.example.com/xyz/uvw.html",
|
|
|
|
expected: "uvw.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "http://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)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(testcase.expected, filename))
|
2017-08-14 20:33:31 -04:00
|
|
|
}
|
|
|
|
}
|