15
15
package com .google .api .client .http ;
16
16
17
17
import com .google .api .client .json .Json ;
18
+ import com .google .api .client .util .Charsets ;
18
19
import com .google .api .client .util .StringUtils ;
19
20
import java .io .ByteArrayOutputStream ;
20
21
import junit .framework .TestCase ;
26
27
*/
27
28
public class MultipartContentTest extends TestCase {
28
29
30
+ private static final String BOUNDARY = "__END_OF_PART__" ;
29
31
private static final String CRLF = "\r \n " ;
30
32
private static final String CONTENT_TYPE = Json .MEDIA_TYPE ;
31
- private static final String HEADERS =
32
- "Content-Length: 3"
33
- + CRLF
34
- + "Content-Type: application/json; charset=UTF-8"
35
- + CRLF
36
- + "content-transfer-encoding: binary"
37
- + CRLF ;
33
+ private static final String HEADERS = headers ("application/json; charset=UTF-8" , "foo" );
34
+
35
+ private static String headers (String contentType , String value ) {
36
+ return "Content-Length: " + value .length () + CRLF
37
+ + "Content-Type: " + contentType + CRLF
38
+ + "content-transfer-encoding: binary" + CRLF ;
39
+ }
40
+
41
+ public void testRandomContent () throws Exception {
42
+ MultipartContent content = new MultipartContent ();
43
+ String boundaryString = content .getBoundary ();
44
+ assertNotNull (boundaryString );
45
+ assertTrue (boundaryString .startsWith (BOUNDARY ));
46
+ assertTrue (boundaryString .endsWith ("__" ));
47
+ assertEquals ("multipart/related; boundary=" + boundaryString , content .getType ());
48
+
49
+ final String [][] VALUES = new String [][] {
50
+ {"Hello world" , "text/plain" },
51
+ {"<xml>Hi</xml>" , "application/xml" },
52
+ {"{x:1,y:2}" , "application/json" }
53
+ };
54
+ StringBuilder expectedStringBuilder = new StringBuilder ();
55
+ for (String [] valueTypePair : VALUES ) {
56
+ String contentValue = valueTypePair [0 ];
57
+ String contentType = valueTypePair [1 ];
58
+ content .addPart (new MultipartContent .Part (ByteArrayContent .fromString (contentType , contentValue )));
59
+ expectedStringBuilder .append ("--" ).append (boundaryString ).append (CRLF )
60
+ .append (headers (contentType , contentValue )).append (CRLF )
61
+ .append (contentValue ).append (CRLF );
62
+ }
63
+ expectedStringBuilder .append ("--" ).append (boundaryString ).append ("--" ).append (CRLF );
64
+ // write to string
65
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
66
+ content .writeTo (out );
67
+ String expectedContent = expectedStringBuilder .toString ();
68
+ assertEquals (expectedContent , out .toString (Charsets .UTF_8 .name ()));
69
+ assertEquals (StringUtils .getBytesUtf8 (expectedContent ).length , content .getLength ());
70
+ }
38
71
39
72
public void testContent () throws Exception {
40
- subtestContent ("--__END_OF_PART__ --" + CRLF , null );
73
+ subtestContent ("--" + BOUNDARY + " --" + CRLF , null );
41
74
subtestContent (
42
- "--__END_OF_PART__" + CRLF + HEADERS + CRLF + "foo" + CRLF + "--__END_OF_PART__--" + CRLF ,
43
- null ,
75
+ "--" + BOUNDARY + CRLF
76
+ + HEADERS + CRLF
77
+ + "foo" + CRLF
78
+ + "--" + BOUNDARY + "--" + CRLF ,
79
+ null ,
44
80
"foo" );
45
81
subtestContent (
46
- "--__END_OF_PART__"
47
- + CRLF
48
- + HEADERS
49
- + CRLF
50
- + "foo"
51
- + CRLF
52
- + "--__END_OF_PART__"
53
- + CRLF
54
- + HEADERS
55
- + CRLF
56
- + "bar"
57
- + CRLF
58
- + "--__END_OF_PART__--"
59
- + CRLF ,
60
- null ,
82
+ "--" + BOUNDARY + CRLF
83
+ + HEADERS + CRLF
84
+ + "foo" + CRLF
85
+ + "--" + BOUNDARY + CRLF
86
+ + HEADERS + CRLF
87
+ + "bar" + CRLF
88
+ + "--" + BOUNDARY + "--" + CRLF ,
89
+ null ,
61
90
"foo" ,
62
91
"bar" );
63
92
subtestContent (
64
- "--myboundary"
65
- + CRLF
66
- + HEADERS
67
- + CRLF
68
- + "foo"
69
- + CRLF
70
- + "--myboundary"
71
- + CRLF
72
- + HEADERS
73
- + CRLF
74
- + "bar"
75
- + CRLF
76
- + "--myboundary--"
77
- + CRLF ,
93
+ "--myboundary" + CRLF
94
+ + HEADERS + CRLF
95
+ + "foo" + CRLF
96
+ + "--myboundary" + CRLF
97
+ + HEADERS + CRLF
98
+ + "bar" + CRLF
99
+ + "--myboundary--" + CRLF ,
78
100
"myboundary" ,
79
101
"foo" ,
80
102
"bar" );
@@ -83,7 +105,7 @@ public void testContent() throws Exception {
83
105
private void subtestContent (String expectedContent , String boundaryString , String ... contents )
84
106
throws Exception {
85
107
// multipart content
86
- MultipartContent content = new MultipartContent ();
108
+ MultipartContent content = new MultipartContent (boundaryString == null ? BOUNDARY : boundaryString );
87
109
for (String contentValue : contents ) {
88
110
content .addPart (
89
111
new MultipartContent .Part (ByteArrayContent .fromString (CONTENT_TYPE , contentValue )));
@@ -94,11 +116,11 @@ private void subtestContent(String expectedContent, String boundaryString, Strin
94
116
// write to string
95
117
ByteArrayOutputStream out = new ByteArrayOutputStream ();
96
118
content .writeTo (out );
97
- assertEquals (expectedContent , out .toString ());
119
+ assertEquals (expectedContent , out .toString (Charsets . UTF_8 . name () ));
98
120
assertEquals (StringUtils .getBytesUtf8 (expectedContent ).length , content .getLength ());
99
121
assertEquals (
100
122
boundaryString == null
101
- ? "multipart/related; boundary=__END_OF_PART__"
123
+ ? "multipart/related; boundary=" + BOUNDARY
102
124
: "multipart/related; boundary=" + boundaryString ,
103
125
content .getType ());
104
126
}
0 commit comments