|
28 | 28 |
|
29 | 29 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 30 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
31 |
| -import static org.assertj.core.api.Assertions.assertThatThrownBy; |
32 | 31 |
|
33 | 32 | /**
|
34 | 33 | * Unit tests for {@link ContentDisposition}
|
35 |
| - * |
36 | 34 | * @author Sebastien Deleuze
|
| 35 | + * @author Rossen Stoyanchev |
37 | 36 | */
|
38 | 37 | public class ContentDispositionTests {
|
39 | 38 |
|
40 |
| - @Test |
41 |
| - public void parseTest() { |
42 |
| - ContentDisposition disposition = ContentDisposition |
43 |
| - .parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); |
44 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data") |
45 |
| - .name("foo").filename("foo.txt").size(123L).build()); |
46 |
| - } |
| 39 | + private static DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; |
| 40 | + |
47 | 41 |
|
48 | 42 | @Test
|
49 | 43 | public void parse() {
|
50 |
| - ContentDisposition disposition = ContentDisposition |
51 |
| - .parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); |
52 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data") |
53 |
| - .name("foo").filename("foo.txt").size(123L).build()); |
| 44 | + assertThat(parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123")) |
| 45 | + .isEqualTo(ContentDisposition.builder("form-data") |
| 46 | + .name("foo") |
| 47 | + .filename("foo.txt") |
| 48 | + .size(123L) |
| 49 | + .build()); |
54 | 50 | }
|
55 | 51 |
|
56 | 52 | @Test
|
57 |
| - public void parseType() { |
58 |
| - ContentDisposition disposition = ContentDisposition.parse("form-data"); |
59 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data").build()); |
| 53 | + public void parseFilenameUnquoted() { |
| 54 | + assertThat(parse("form-data; filename=unquoted")) |
| 55 | + .isEqualTo(ContentDisposition.builder("form-data") |
| 56 | + .filename("unquoted") |
| 57 | + .build()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test // SPR-16091 |
| 61 | + public void parseFilenameWithSemicolon() { |
| 62 | + assertThat(parse("attachment; filename=\"filename with ; semicolon.txt\"")) |
| 63 | + .isEqualTo(ContentDisposition.builder("attachment") |
| 64 | + .filename("filename with ; semicolon.txt") |
| 65 | + .build()); |
60 | 66 | }
|
61 | 67 |
|
62 | 68 | @Test
|
63 |
| - public void parseUnquotedFilename() { |
64 |
| - ContentDisposition disposition = ContentDisposition |
65 |
| - .parse("form-data; filename=unquoted"); |
66 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data").filename("unquoted").build()); |
| 69 | + public void parseEncodedFilename() { |
| 70 | + assertThat(parse("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt")) |
| 71 | + .isEqualTo(ContentDisposition.builder("form-data") |
| 72 | + .name("name") |
| 73 | + .filename("中文.txt", StandardCharsets.UTF_8) |
| 74 | + .build()); |
67 | 75 | }
|
68 | 76 |
|
69 |
| - @Test // SPR-16091 |
70 |
| - public void parseFilenameWithSemicolon() { |
71 |
| - ContentDisposition disposition = ContentDisposition |
72 |
| - .parse("attachment; filename=\"filename with ; semicolon.txt\""); |
73 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("attachment") |
74 |
| - .filename("filename with ; semicolon.txt").build()); |
| 77 | + @Test // gh-23077 |
| 78 | + public void parseWithEscapedQuote() { |
| 79 | + assertThat(parse("form-data; name=\"file\"; filename=\"\\\"The Twilight Zone\\\".txt\"; size=123")) |
| 80 | + .isEqualTo(ContentDisposition.builder("form-data") |
| 81 | + .name("file") |
| 82 | + .filename("\\\"The Twilight Zone\\\".txt") |
| 83 | + .size(123L) |
| 84 | + .build()); |
75 | 85 | }
|
76 | 86 |
|
77 | 87 | @Test
|
78 |
| - public void parseAndIgnoreEmptyParts() { |
79 |
| - ContentDisposition disposition = ContentDisposition |
80 |
| - .parse("form-data; name=\"foo\";; ; filename=\"foo.txt\"; size=123"); |
81 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data") |
82 |
| - .name("foo").filename("foo.txt").size(123L).build()); |
| 88 | + public void parseWithExtraSemicolons() { |
| 89 | + assertThat(parse("form-data; name=\"foo\";; ; filename=\"foo.txt\"; size=123")) |
| 90 | + .isEqualTo(ContentDisposition.builder("form-data") |
| 91 | + .name("foo") |
| 92 | + .filename("foo.txt") |
| 93 | + .size(123L) |
| 94 | + .build()); |
83 | 95 | }
|
84 | 96 |
|
85 | 97 | @Test
|
86 |
| - public void parseEncodedFilename() { |
87 |
| - ContentDisposition disposition = ContentDisposition |
88 |
| - .parse("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); |
89 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data").name("name") |
90 |
| - .filename("中文.txt", StandardCharsets.UTF_8).build()); |
| 98 | + public void parseDates() { |
| 99 | + ZonedDateTime creationTime = ZonedDateTime.parse("Mon, 12 Feb 2007 10:15:30 -0500", formatter); |
| 100 | + ZonedDateTime modificationTime = ZonedDateTime.parse("Tue, 13 Feb 2007 10:15:30 -0500", formatter); |
| 101 | + ZonedDateTime readTime = ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter); |
| 102 | + |
| 103 | + assertThat( |
| 104 | + parse("attachment; " + |
| 105 | + "creation-date=\"" + creationTime.format(formatter) + "\"; " + |
| 106 | + "modification-date=\"" + modificationTime.format(formatter) + "\"; " + |
| 107 | + "read-date=\"" + readTime.format(formatter) + "\"")).isEqualTo( |
| 108 | + ContentDisposition.builder("attachment") |
| 109 | + .creationDate(creationTime) |
| 110 | + .modificationDate(modificationTime) |
| 111 | + .readDate(readTime) |
| 112 | + .build()); |
91 | 113 | }
|
92 | 114 |
|
93 |
| - @Test // gh-23077 |
94 |
| - public void parseWithEscapedQuote() { |
95 |
| - ContentDisposition disposition = ContentDisposition.parse( |
96 |
| - "form-data; name=\"file\"; filename=\"\\\"The Twilight Zone\\\".txt\"; size=123"); |
97 |
| - assertThat(ContentDisposition.builder("form-data").name("file") |
98 |
| - .filename("\\\"The Twilight Zone\\\".txt").size(123L).build()).isEqualTo(disposition); |
| 115 | + @Test |
| 116 | + public void parseIgnoresInvalidDates() { |
| 117 | + ZonedDateTime readTime = ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter); |
| 118 | + |
| 119 | + assertThat( |
| 120 | + parse("attachment; " + |
| 121 | + "creation-date=\"-1\"; " + |
| 122 | + "modification-date=\"-1\"; " + |
| 123 | + "read-date=\"" + readTime.format(formatter) + "\"")).isEqualTo( |
| 124 | + ContentDisposition.builder("attachment") |
| 125 | + .readDate(readTime) |
| 126 | + .build()); |
99 | 127 | }
|
100 | 128 |
|
101 | 129 | @Test
|
102 | 130 | public void parseEmpty() {
|
103 |
| - assertThatIllegalArgumentException().isThrownBy(() -> |
104 |
| - ContentDisposition.parse("")); |
| 131 | + assertThatIllegalArgumentException().isThrownBy(() -> parse("")); |
105 | 132 | }
|
106 | 133 |
|
107 | 134 | @Test
|
108 | 135 | public void parseNoType() {
|
109 |
| - assertThatIllegalArgumentException().isThrownBy(() -> |
110 |
| - ContentDisposition.parse(";")); |
| 136 | + assertThatIllegalArgumentException().isThrownBy(() -> parse(";")); |
111 | 137 | }
|
112 | 138 |
|
113 | 139 | @Test
|
114 | 140 | public void parseInvalidParameter() {
|
115 |
| - assertThatIllegalArgumentException().isThrownBy(() -> |
116 |
| - ContentDisposition.parse("foo;bar")); |
| 141 | + assertThatIllegalArgumentException().isThrownBy(() -> parse("foo;bar")); |
117 | 142 | }
|
118 | 143 |
|
119 |
| - @Test |
120 |
| - public void parseDates() { |
121 |
| - ContentDisposition disposition = ContentDisposition |
122 |
| - .parse("attachment; creation-date=\"Mon, 12 Feb 2007 10:15:30 -0500\"; " + |
123 |
| - "modification-date=\"Tue, 13 Feb 2007 10:15:30 -0500\"; " + |
124 |
| - "read-date=\"Wed, 14 Feb 2007 10:15:30 -0500\""); |
125 |
| - DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; |
126 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("attachment") |
127 |
| - .creationDate(ZonedDateTime.parse("Mon, 12 Feb 2007 10:15:30 -0500", formatter)) |
128 |
| - .modificationDate(ZonedDateTime.parse("Tue, 13 Feb 2007 10:15:30 -0500", formatter)) |
129 |
| - .readDate(ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter)).build()); |
| 144 | + private static ContentDisposition parse(String input) { |
| 145 | + return ContentDisposition.parse(input); |
130 | 146 | }
|
131 | 147 |
|
132 |
| - @Test |
133 |
| - public void parseInvalidDates() { |
134 |
| - ContentDisposition disposition = ContentDisposition |
135 |
| - .parse("attachment; creation-date=\"-1\"; modification-date=\"-1\"; " + |
136 |
| - "read-date=\"Wed, 14 Feb 2007 10:15:30 -0500\""); |
137 |
| - DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; |
138 |
| - assertThat(disposition).isEqualTo(ContentDisposition.builder("attachment") |
139 |
| - .readDate(ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter)).build()); |
140 |
| - } |
141 | 148 |
|
142 | 149 | @Test
|
143 | 150 | public void headerValue() {
|
144 |
| - ContentDisposition disposition = ContentDisposition.builder("form-data") |
145 |
| - .name("foo").filename("foo.txt").size(123L).build(); |
146 |
| - assertThat(disposition.toString()).isEqualTo("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); |
| 151 | + assertThat( |
| 152 | + ContentDisposition.builder("form-data") |
| 153 | + .name("foo") |
| 154 | + .filename("foo.txt") |
| 155 | + .size(123L) |
| 156 | + .build().toString()) |
| 157 | + .isEqualTo("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); |
147 | 158 | }
|
148 | 159 |
|
149 | 160 | @Test
|
150 | 161 | public void headerValueWithEncodedFilename() {
|
151 |
| - ContentDisposition disposition = ContentDisposition.builder("form-data") |
152 |
| - .name("name").filename("中文.txt", StandardCharsets.UTF_8).build(); |
153 |
| - assertThat(disposition.toString()).isEqualTo("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); |
| 162 | + assertThat( |
| 163 | + ContentDisposition.builder("form-data") |
| 164 | + .name("name") |
| 165 | + .filename("中文.txt", StandardCharsets.UTF_8) |
| 166 | + .build().toString()) |
| 167 | + .isEqualTo("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); |
154 | 168 | }
|
155 | 169 |
|
156 | 170 | @Test // SPR-14547
|
|
0 commit comments