1
0

cdv_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package httpheaders
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestContentDispositionValue(t *testing.T) {
  7. // Test cases for ContentDispositionValue function that generates content-disposition headers
  8. tests := []struct {
  9. name string
  10. url string
  11. filename string
  12. ext string
  13. returnAttachment bool
  14. expected string
  15. contentType string
  16. }{
  17. {
  18. name: "BasicURL",
  19. url: "http://example.com/test.jpg",
  20. filename: "",
  21. ext: "",
  22. contentType: "",
  23. returnAttachment: false,
  24. expected: "inline; filename=\"test.jpg\"",
  25. },
  26. {
  27. name: "EmptyFilename",
  28. url: "http://example.com/path/to/",
  29. filename: "",
  30. ext: "",
  31. contentType: "",
  32. returnAttachment: false,
  33. expected: "inline; filename=\"image\"",
  34. },
  35. {
  36. name: "EmptyFilenameWithExt",
  37. url: "http://example.com/path/to/",
  38. filename: "",
  39. ext: ".png",
  40. contentType: "",
  41. returnAttachment: false,
  42. expected: "inline; filename=\"image.png\"",
  43. },
  44. {
  45. name: "EmptyFilenameWithFilenameAndExt",
  46. url: "http://example.com/path/to/",
  47. filename: "example",
  48. ext: ".png",
  49. contentType: "",
  50. returnAttachment: false,
  51. expected: "inline; filename=\"example.png\"",
  52. },
  53. {
  54. name: "EmptyFilenameWithFilenameOverride",
  55. url: "http://example.com/path/to/",
  56. filename: "example",
  57. ext: ".jpg",
  58. contentType: "",
  59. returnAttachment: false,
  60. expected: "inline; filename=\"example.jpg\"",
  61. },
  62. {
  63. name: "PresentFilenameWithExtOverride",
  64. url: "http://example.com/path/to/face.png",
  65. filename: "",
  66. ext: ".jpg",
  67. contentType: "",
  68. returnAttachment: false,
  69. expected: "inline; filename=\"face.jpg\"",
  70. },
  71. {
  72. name: "PresentFilenameWithFilenameOverride",
  73. url: "http://example.com/path/to/123.png",
  74. filename: "face",
  75. ext: ".jpg",
  76. contentType: "",
  77. returnAttachment: false,
  78. expected: "inline; filename=\"face.jpg\"",
  79. },
  80. {
  81. name: "EmptyFilenameWithFallback",
  82. url: "http://example.com/path/to/",
  83. filename: "",
  84. ext: ".png",
  85. contentType: "",
  86. returnAttachment: false,
  87. expected: "inline; filename=\"image.png\"",
  88. },
  89. {
  90. name: "AttachmentDisposition",
  91. url: "http://example.com/test.jpg",
  92. filename: "",
  93. ext: "",
  94. contentType: "",
  95. returnAttachment: true,
  96. expected: "attachment; filename=\"test.jpg\"",
  97. },
  98. {
  99. name: "FilenameWithQuotes",
  100. url: "http://example.com/test.jpg",
  101. filename: "my\"file",
  102. ext: ".png",
  103. returnAttachment: false,
  104. contentType: "",
  105. expected: "inline; filename=\"my%22file.png\"",
  106. },
  107. {
  108. name: "FilenameWithContentType",
  109. url: "http://example.com/test",
  110. filename: "my\"file",
  111. ext: "",
  112. contentType: "image/png",
  113. returnAttachment: false,
  114. expected: "inline; filename=\"my%22file.png\"",
  115. },
  116. }
  117. for _, tc := range tests {
  118. t.Run(tc.name, func(t *testing.T) {
  119. result := ContentDispositionValue(tc.url, tc.filename, tc.ext, tc.contentType, tc.returnAttachment)
  120. require.Equal(t, tc.expected, result)
  121. })
  122. }
  123. }