copy_test.go 3.0 KB

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