summaryrefslogtreecommitdiff
path: root/builder/dockerfile/copy_test.go
blob: 5fce0bd4eb32ba0301a8a4d61193b16a594c585d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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()

	var 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) {
	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)
		assert.Check(t, is.Equal(testcase.expected, filename))
	}
}