1package download
2
3import "testing"
4
5func TestShouldForceDownloadForURI(t *testing.T) {
6 tests := []struct {
7 name string
8 uri string
9 expected bool
10 }{
11 {name: "pdf path", uri: "https://example.com/file.pdf", expected: true},
12 {name: "iso path", uri: "https://example.com/linux.iso?mirror=1", expected: true},
13 {name: "archive path", uri: "https://example.com/release.tar.gz", expected: true},
14 {name: "plain html", uri: "https://example.com/index.html", expected: false},
15 {name: "empty", uri: "", expected: false},
16 }
17
18 for _, tt := range tests {
19 t.Run(tt.name, func(t *testing.T) {
20 if got := ShouldForceDownloadForURI(tt.uri); got != tt.expected {
21 t.Fatalf("ShouldForceDownloadForURI(%q) = %v, want %v", tt.uri, got, tt.expected)
22 }
23 })
24 }
25}
26
27func TestShouldForceDownloadForMIMEType(t *testing.T) {
28 tests := []struct {
29 name string
30 mimeType string
31 expected bool
32 }{
33 {name: "pdf", mimeType: "application/pdf", expected: true},
34 {name: "pdf with params", mimeType: "application/pdf; charset=binary", expected: true},
35 {name: "iso image", mimeType: "application/x-iso9660-image", expected: true},
36 {name: "html", mimeType: "text/html", expected: false},
37 {name: "empty", mimeType: "", expected: false},
38 }
39
40 for _, tt := range tests {
41 t.Run(tt.name, func(t *testing.T) {
42 if got := ShouldForceDownloadForMIMEType(tt.mimeType); got != tt.expected {
43 t.Fatalf("ShouldForceDownloadForMIMEType(%q) = %v, want %v", tt.mimeType, got, tt.expected)
44 }
45 })
46 }
47}
48
49func TestShouldForceDownload(t *testing.T) {
50 if !ShouldForceDownload("https://example.com/file.iso", "") {
51 t.Fatal("expected URI extension to trigger forced download")
52 }
53 if !ShouldForceDownload("", "application/pdf") {
54 t.Fatal("expected MIME type to trigger forced download")
55 }
56 if ShouldForceDownload("https://example.com/", "text/html") {
57 t.Fatal("did not expect regular HTML to trigger forced download")
58 }
59}