This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffi_test.go
137 lines (123 loc) · 4.16 KB
/
ffi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ffi
import (
"io"
"testing"
)
func errorMismatch(err error, expect string) bool {
return (err == nil && expect != "") || (err != nil && expect == "") || (err != nil && err.Error() != expect)
}
func TestFilenameFromUrlString(t *testing.T) {
cases := []struct {
in, out, err string
}{
{"https://archivers.space/files/not/a/path/image.jpg", "image.jpg", ""},
{"https://www.epa.gov/sites/production/files/2017-02/tscainv_feb2017_csv.zip", "tscainv_feb2017_csv.zip", ""},
{"https://www.census.gov/topics/business/retail-trade.html", "retail-trade.html", ""},
{"https://www.census.gov/topics/education/educational-attainment.html", "educational-attainment.html", ""},
{"https://www.epa.gov/caddis-vol2", "caddis-vol2", ""},
{"https://www.epa.gov/caddis-vol4", "caddis-vol4", ""},
{"https://www.census.gov/topics/population/language-use.html", "language-use.html", ""},
{"https://www.census.gov/topics/business/classification-codes.html", "classification-codes.html", ""},
{"https://www.census.gov/topics/business/business-help.html", "business-help.html", ""},
{"https://www.epa.gov/sites/production/files/2017-02/", "2017-02", ""},
{"https://www.epa.gov/caddis-vol1", "caddis-vol1", ""},
{"https://www.epa.gov/sites/production/files/2017-02", "2017-02", ""},
{"https://www.census.gov/topics/population/age-and-sex.html", "age-and-sex.html", ""},
{"https://census.gov/library/publications/2009/compendia/statab/129ed/population.html#NAV_1529793603_0_accd", "population.html", ""},
{"https://www.census.gov/topics/education/school-enrollment.html?query=param", "school-enrollment.html", ""},
{"https://www.census.gov/topics/housing/housing-vacancies.html", "housing-vacancies.html", ""},
// {"https://qri.io/files/not/a/path/image", "", nil},
}
for i, c := range cases {
got, err := FilenameFromUrlString(c.in)
if errorMismatch(err, c.err) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d output mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}
func TestFilenameMimeType(t *testing.T) {
cases := []struct {
in, out, err string
}{
{"", "", "unrecognized MIME-Type: ''"},
}
for i, c := range cases {
got, err := FilenameMimeType(c.in)
if errorMismatch(err, c.err) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d output mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}
func TestSetExtension(t *testing.T) {
cases := []struct {
fn, mt, out, err string
}{
{"apples.html", "application/pdf", "apples.pdf", ""},
{"apples.html", "invalid/blech", "apples.html", "unrecognized MIME-Type: 'invalid/blech'"},
}
for i, c := range cases {
got, err := SetExtension(c.fn, c.mt)
if errorMismatch(err, c.err) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d output mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}
func TestMimeTypeExtension(t *testing.T) {
cases := []struct {
in, out, err string
}{
{"", "", "unrecognized MIME-Type: ''"},
}
for i, c := range cases {
got, err := MimeTypeExtension(c.in)
if errorMismatch(err, c.err) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d output mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}
func TestExtensionMimeType(t *testing.T) {
cases := []struct {
in, out, err string
}{
{"", "", "unrecognized extension: ''"},
}
for i, c := range cases {
got, err := ExtensionMimeType(c.in)
if errorMismatch(err, c.err) {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d output mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}
func TestMimeType(t *testing.T) {
cases := []struct {
in io.Reader
out string
err error
}{
// {"", "", nil},
}
for i, c := range cases {
got, err := MimeType(c.in)
if err != c.err {
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
}
if got != c.out {
t.Errorf("case %d output mismatch. expected: %s, got: %s", i, c.out, got)
}
}
}