github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/builder/dockerfile/copy_test.go (about) 1 package dockerfile // import "github.com/Prakhar-Agarwal-byte/moby/builder/dockerfile" 2 3 import ( 4 "net/http" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 is "gotest.tools/v3/assert/cmp" 9 "gotest.tools/v3/fs" 10 ) 11 12 func TestIsExistingDirectory(t *testing.T) { 13 tmpfile := fs.NewFile(t, "file-exists-test", fs.WithContent("something")) 14 defer tmpfile.Remove() 15 tmpdir := fs.NewDir(t, "dir-exists-test") 16 defer tmpdir.Remove() 17 18 testcases := []struct { 19 doc string 20 path string 21 expected bool 22 }{ 23 { 24 doc: "directory exists", 25 path: tmpdir.Path(), 26 expected: true, 27 }, 28 { 29 doc: "path doesn't exist", 30 path: "/bogus/path/does/not/exist", 31 expected: false, 32 }, 33 { 34 doc: "file exists", 35 path: tmpfile.Path(), 36 expected: false, 37 }, 38 } 39 40 for _, testcase := range testcases { 41 result, err := isExistingDirectory(testcase.path) 42 if !assert.Check(t, err) { 43 continue 44 } 45 assert.Check(t, is.Equal(testcase.expected, result), testcase.doc) 46 } 47 } 48 49 func TestGetFilenameForDownload(t *testing.T) { 50 testcases := []struct { 51 path string 52 disposition string 53 expected string 54 }{ 55 { 56 path: "https://www.example.com/", 57 expected: "", 58 }, 59 { 60 path: "https://www.example.com/xyz", 61 expected: "xyz", 62 }, 63 { 64 path: "https://www.example.com/xyz.html", 65 expected: "xyz.html", 66 }, 67 { 68 path: "https://www.example.com/xyz/", 69 expected: "", 70 }, 71 { 72 path: "https://www.example.com/xyz/uvw", 73 expected: "uvw", 74 }, 75 { 76 path: "https://www.example.com/xyz/uvw.html", 77 expected: "uvw.html", 78 }, 79 { 80 path: "https://www.example.com/xyz/uvw/", 81 expected: "", 82 }, 83 { 84 path: "/", 85 expected: "", 86 }, 87 { 88 path: "/xyz", 89 expected: "xyz", 90 }, 91 { 92 path: "/xyz.html", 93 expected: "xyz.html", 94 }, 95 { 96 path: "/xyz/", 97 expected: "", 98 }, 99 { 100 path: "/xyz/", 101 disposition: "attachment; filename=xyz.html", 102 expected: "xyz.html", 103 }, 104 { 105 disposition: "", 106 expected: "", 107 }, 108 { 109 disposition: "attachment; filename=xyz", 110 expected: "xyz", 111 }, 112 { 113 disposition: "attachment; filename=xyz.html", 114 expected: "xyz.html", 115 }, 116 { 117 disposition: `attachment; filename="xyz"`, 118 expected: "xyz", 119 }, 120 { 121 disposition: `attachment; filename="xyz.html"`, 122 expected: "xyz.html", 123 }, 124 { 125 disposition: `attachment; filename="/xyz.html"`, 126 expected: "xyz.html", 127 }, 128 { 129 disposition: `attachment; filename="/xyz/uvw"`, 130 expected: "uvw", 131 }, 132 { 133 disposition: `attachment; filename="Naïve file.txt"`, 134 expected: "Naïve file.txt", 135 }, 136 } 137 for _, testcase := range testcases { 138 resp := http.Response{ 139 Header: make(map[string][]string), 140 } 141 if testcase.disposition != "" { 142 resp.Header.Add("Content-Disposition", testcase.disposition) 143 } 144 filename := getFilenameForDownload(testcase.path, &resp) 145 assert.Check(t, is.Equal(testcase.expected, filename)) 146 } 147 }