11package com .github .scribejava .core .httpclient .multipart ;
22
3- import org .hamcrest .core .StringStartsWith ;
43import static org .junit .Assert .assertEquals ;
54import static org .junit .Assert .assertNull ;
6- import org .junit .Rule ;
5+ import static org .junit .Assert .assertThrows ;
6+ import static org .junit .Assert .assertTrue ;
77import org .junit .Test ;
8- import org .junit .rules . ExpectedException ;
8+ import org .junit .function . ThrowingRunnable ;
99
1010public class MultipartPayloadTest {
1111
12- @ Rule
13- public ExpectedException thrown = ExpectedException .none ();
14-
1512 @ Test
1613 public void testValidCheckBoundarySyntax () {
1714 MultipartPayload .checkBoundarySyntax ("0aA'()+_,-./:=?" );
@@ -22,47 +19,27 @@ public void testValidCheckBoundarySyntax() {
2219
2320 @ Test
2421 public void testNonValidLastWhiteSpaceCheckBoundarySyntax () {
25- final String boundary = "0aA'()+_,-./:=? " ;
26- thrown .expect (IllegalArgumentException .class );
27- thrown .expectMessage (
28- StringStartsWith .startsWith ("{'boundary'='" + boundary + "'} has invaid syntax. Should be '" ));
29- MultipartPayload .checkBoundarySyntax (boundary );
22+ testBoundary ("0aA'()+_,-./:=? " );
3023 }
3124
3225 @ Test
3326 public void testNonValidEmptyCheckBoundarySyntax () {
34- final String boundary = "" ;
35- thrown .expect (IllegalArgumentException .class );
36- thrown .expectMessage (
37- StringStartsWith .startsWith ("{'boundary'='" + boundary + "'} has invaid syntax. Should be '" ));
38- MultipartPayload .checkBoundarySyntax (boundary );
27+ testBoundary ("" );
3928 }
4029
4130 @ Test
4231 public void testNonValidIllegalSymbolCheckBoundarySyntax () {
43- final String boundary = "0aA'()+_;,-./:=? " ;
44- thrown .expect (IllegalArgumentException .class );
45- thrown .expectMessage (
46- StringStartsWith .startsWith ("{'boundary'='" + boundary + "'} has invaid syntax. Should be '" ));
47- MultipartPayload .checkBoundarySyntax (boundary );
32+ testBoundary ("0aA'()+_;,-./:=? " );
4833 }
4934
5035 @ Test
5136 public void testNonValidTooLongCheckBoundarySyntax () {
52- final String boundary = "12345678901234567890123456789012345678901234567890123456789012345678901" ;
53- thrown .expect (IllegalArgumentException .class );
54- thrown .expectMessage (
55- StringStartsWith .startsWith ("{'boundary'='" + boundary + "'} has invaid syntax. Should be '" ));
56- MultipartPayload .checkBoundarySyntax (boundary );
37+ testBoundary ("12345678901234567890123456789012345678901234567890123456789012345678901" );
5738 }
5839
5940 @ Test
6041 public void testNonValidNullCheckBoundarySyntax () {
61- final String boundary = null ;
62- thrown .expect (IllegalArgumentException .class );
63- thrown .expectMessage (
64- StringStartsWith .startsWith ("{'boundary'='" + boundary + "'} has invaid syntax. Should be '" ));
65- MultipartPayload .checkBoundarySyntax (boundary );
42+ testBoundary (null );
6643 }
6744
6845 @ Test
@@ -118,4 +95,14 @@ public void testParseBoundaryFromHeader() {
11895 assertNull (MultipartPayload .parseBoundaryFromHeader ("multipart/subtype; boundary=;123" ));
11996 assertNull (MultipartPayload .parseBoundaryFromHeader ("multipart/subtype; boundary=\" \" 123" ));
12097 }
98+
99+ private static void testBoundary (final String boundary ) {
100+ final IllegalArgumentException thrown = assertThrows (IllegalArgumentException .class , new ThrowingRunnable () {
101+ @ Override
102+ public void run () throws Throwable {
103+ MultipartPayload .checkBoundarySyntax (boundary );
104+ }
105+ });
106+ assertTrue (thrown .getMessage ().startsWith ("{'boundary'='" + boundary + "'} has invalid syntax. Should be '" ));
107+ }
121108}
0 commit comments