copy_test.go 2.9 KB

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