parent
							
								
									f084b63286
								
							
						
					
					
						commit
						c97580035e
					
				|  | @ -28,129 +28,143 @@ import org.springframework.util.ReflectionUtils; | |||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||||
| 
 | ||||
| /** | ||||
|  * Unit tests for {@link ContentDisposition} | ||||
|  * | ||||
|  * @author Sebastien Deleuze | ||||
|  * @author Rossen Stoyanchev | ||||
|  */ | ||||
| public class ContentDispositionTests { | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseTest() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data") | ||||
| 				.name("foo").filename("foo.txt").size(123L).build()); | ||||
| 	} | ||||
| 	private static DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; | ||||
| 
 | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parse() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data") | ||||
| 				.name("foo").filename("foo.txt").size(123L).build()); | ||||
| 		assertThat(parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123")) | ||||
| 				.isEqualTo(ContentDisposition.builder("form-data") | ||||
| 						.name("foo") | ||||
| 						.filename("foo.txt") | ||||
| 						.size(123L) | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseType() { | ||||
| 		ContentDisposition disposition = ContentDisposition.parse("form-data"); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data").build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseUnquotedFilename() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("form-data; filename=unquoted"); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data").filename("unquoted").build()); | ||||
| 	public void parseFilenameUnquoted() { | ||||
| 		assertThat(parse("form-data; filename=unquoted")) | ||||
| 				.isEqualTo(ContentDisposition.builder("form-data") | ||||
| 						.filename("unquoted") | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test  // SPR-16091 | ||||
| 	public void parseFilenameWithSemicolon() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("attachment; filename=\"filename with ; semicolon.txt\""); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("attachment") | ||||
| 				.filename("filename with ; semicolon.txt").build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseAndIgnoreEmptyParts() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("form-data; name=\"foo\";; ; filename=\"foo.txt\"; size=123"); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data") | ||||
| 				.name("foo").filename("foo.txt").size(123L).build()); | ||||
| 		assertThat(parse("attachment; filename=\"filename with ; semicolon.txt\"")) | ||||
| 				.isEqualTo(ContentDisposition.builder("attachment") | ||||
| 						.filename("filename with ; semicolon.txt") | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseEncodedFilename() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("form-data").name("name") | ||||
| 				.filename("中文.txt", StandardCharsets.UTF_8).build()); | ||||
| 		assertThat(parse("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt")) | ||||
| 				.isEqualTo(ContentDisposition.builder("form-data") | ||||
| 						.name("name") | ||||
| 						.filename("中文.txt", StandardCharsets.UTF_8) | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test // gh-23077 | ||||
| 	public void parseWithEscapedQuote() { | ||||
| 		ContentDisposition disposition = ContentDisposition.parse( | ||||
| 				"form-data; name=\"file\"; filename=\"\\\"The Twilight Zone\\\".txt\"; size=123"); | ||||
| 		assertThat(ContentDisposition.builder("form-data").name("file") | ||||
| 				.filename("\\\"The Twilight Zone\\\".txt").size(123L).build()).isEqualTo(disposition); | ||||
| 		assertThat(parse("form-data; name=\"file\"; filename=\"\\\"The Twilight Zone\\\".txt\"; size=123")) | ||||
| 				.isEqualTo(ContentDisposition.builder("form-data") | ||||
| 						.name("file") | ||||
| 						.filename("\\\"The Twilight Zone\\\".txt") | ||||
| 						.size(123L) | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseEmpty() { | ||||
| 		assertThatIllegalArgumentException().isThrownBy(() -> | ||||
| 				ContentDisposition.parse("")); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseNoType() { | ||||
| 		assertThatIllegalArgumentException().isThrownBy(() -> | ||||
| 				ContentDisposition.parse(";")); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseInvalidParameter() { | ||||
| 		assertThatIllegalArgumentException().isThrownBy(() -> | ||||
| 				ContentDisposition.parse("foo;bar")); | ||||
| 	public void parseWithExtraSemicolons() { | ||||
| 		assertThat(parse("form-data; name=\"foo\";; ; filename=\"foo.txt\"; size=123")) | ||||
| 				.isEqualTo(ContentDisposition.builder("form-data") | ||||
| 						.name("foo") | ||||
| 						.filename("foo.txt") | ||||
| 						.size(123L) | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseDates() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("attachment; creation-date=\"Mon, 12 Feb 2007 10:15:30 -0500\"; " + | ||||
| 						"modification-date=\"Tue, 13 Feb 2007 10:15:30 -0500\"; " + | ||||
| 						"read-date=\"Wed, 14 Feb 2007 10:15:30 -0500\""); | ||||
| 		DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("attachment") | ||||
| 				.creationDate(ZonedDateTime.parse("Mon, 12 Feb 2007 10:15:30 -0500", formatter)) | ||||
| 				.modificationDate(ZonedDateTime.parse("Tue, 13 Feb 2007 10:15:30 -0500", formatter)) | ||||
| 				.readDate(ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter)).build()); | ||||
| 		ZonedDateTime creationTime = ZonedDateTime.parse("Mon, 12 Feb 2007 10:15:30 -0500", formatter); | ||||
| 		ZonedDateTime modificationTime = ZonedDateTime.parse("Tue, 13 Feb 2007 10:15:30 -0500", formatter); | ||||
| 		ZonedDateTime readTime = ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter); | ||||
| 
 | ||||
| 		assertThat( | ||||
| 				parse("attachment; " + | ||||
| 						"creation-date=\"" + creationTime.format(formatter) + "\"; " + | ||||
| 						"modification-date=\"" + modificationTime.format(formatter) + "\"; " + | ||||
| 						"read-date=\"" + readTime.format(formatter) + "\"")).isEqualTo( | ||||
| 				ContentDisposition.builder("attachment") | ||||
| 						.creationDate(creationTime) | ||||
| 						.modificationDate(modificationTime) | ||||
| 						.readDate(readTime) | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseInvalidDates() { | ||||
| 		ContentDisposition disposition = ContentDisposition | ||||
| 				.parse("attachment; creation-date=\"-1\"; modification-date=\"-1\"; " + | ||||
| 						"read-date=\"Wed, 14 Feb 2007 10:15:30 -0500\""); | ||||
| 		DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; | ||||
| 		assertThat(disposition).isEqualTo(ContentDisposition.builder("attachment") | ||||
| 				.readDate(ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter)).build()); | ||||
| 	public void parseIgnoresInvalidDates() { | ||||
| 		ZonedDateTime readTime = ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter); | ||||
| 
 | ||||
| 		assertThat( | ||||
| 				parse("attachment; " + | ||||
| 						"creation-date=\"-1\"; " + | ||||
| 						"modification-date=\"-1\"; " + | ||||
| 						"read-date=\"" + readTime.format(formatter) + "\"")).isEqualTo( | ||||
| 				ContentDisposition.builder("attachment") | ||||
| 						.readDate(readTime) | ||||
| 						.build()); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseEmpty() { | ||||
| 		assertThatIllegalArgumentException().isThrownBy(() -> parse("")); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseNoType() { | ||||
| 		assertThatIllegalArgumentException().isThrownBy(() -> parse(";")); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void parseInvalidParameter() { | ||||
| 		assertThatIllegalArgumentException().isThrownBy(() -> parse("foo;bar")); | ||||
| 	} | ||||
| 
 | ||||
| 	private static ContentDisposition parse(String input) { | ||||
| 		return ContentDisposition.parse(input); | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void headerValue() { | ||||
| 		ContentDisposition disposition = ContentDisposition.builder("form-data") | ||||
| 				.name("foo").filename("foo.txt").size(123L).build(); | ||||
| 		assertThat(disposition.toString()).isEqualTo("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); | ||||
| 		assertThat( | ||||
| 				ContentDisposition.builder("form-data") | ||||
| 						.name("foo") | ||||
| 						.filename("foo.txt") | ||||
| 						.size(123L) | ||||
| 						.build().toString()) | ||||
| 				.isEqualTo("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void headerValueWithEncodedFilename() { | ||||
| 		ContentDisposition disposition = ContentDisposition.builder("form-data") | ||||
| 				.name("name").filename("中文.txt", StandardCharsets.UTF_8).build(); | ||||
| 		assertThat(disposition.toString()).isEqualTo("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); | ||||
| 		assertThat( | ||||
| 				ContentDisposition.builder("form-data") | ||||
| 						.name("name") | ||||
| 						.filename("中文.txt", StandardCharsets.UTF_8) | ||||
| 						.build().toString()) | ||||
| 				.isEqualTo("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test  // SPR-14547 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue