Skip to content

Commit c7087e4

Browse files
Merge pull request kubernetes-client#240 from archcosmo/master
Fix issue kubernetes-client#233
2 parents 571fe0c + 0d33041 commit c7087e4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: kubernetes/src/main/java/io/kubernetes/client/JSON.java

+3
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ public class ByteArrayAdapter extends TypeAdapter<byte[]> {
126126

127127
@Override
128128
public void write(JsonWriter out, byte[] value) throws IOException {
129+
boolean oldHtmlSafe = out.isHtmlSafe();
130+
out.setHtmlSafe(false);
129131
if (value == null) {
130132
out.nullValue();
131133
} else {
132134
out.value(ByteString.of(value).base64());
133135
}
136+
out.setHtmlSafe(oldHtmlSafe);
134137
}
135138

136139
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.kubernetes.client;
2+
3+
import okio.ByteString;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.CoreMatchers.is;
7+
import static org.junit.Assert.*;
8+
9+
public class JSONTest {
10+
11+
@Test
12+
public void testSerializeByteArray() {
13+
final JSON json = new JSON();
14+
final String plainText = "string that contains '=' when encoded";
15+
final String base64String = json.serialize(plainText.getBytes());
16+
//serialize returns string surrounded by quotes: "\"[base64]\""
17+
final String pureString = base64String.replaceAll("^\"|\"$", "");
18+
final ByteString byteStr = ByteString.decodeBase64(pureString);
19+
20+
//Check encoded to valid base64
21+
assertNotNull(byteStr);
22+
23+
//Check encoded string correctly
24+
final String decodedText =new String(byteStr.toByteArray());
25+
assertThat(decodedText, is(plainText));
26+
}
27+
}

0 commit comments

Comments
 (0)