1 package de.timroes; 2 3 import static org.junit.Assert.*; 4 5 import de.timroes.axmlrpc.*; 6 import de.timroes.axmlrpc.serializer.SerializerHandler; 7 import org.junit.Test; 8 9 import java.io.ByteArrayInputStream; 10 import java.io.InputStream; 11 import java.nio.charset.StandardCharsets; 12 import java.util.Date; 13 import java.util.Map; 14 import java.util.TreeMap; 15 16 public class TestResponseParser { 17 public final static String xmlDecl = "<?xml version=\"1.0\"?>"; 18 public final static SerializerHandler sh = new SerializerHandler(XMLRPCClient.FLAGS_NONE); 19 20 21 @Test testSimpleResponse()22 public void testSimpleResponse() throws Exception { 23 ResponseParser sut = new ResponseParser(); 24 Object actual = sut.parse(sh, strToStream(xmlDecl + 25 "<methodResponse>" + 26 " <params>" + 27 " <param>" + 28 " <value><string>toto</string></value>" + 29 " </param>" + 30 " </params>" + 31 "</methodResponse>"), false); 32 assertEquals("toto", actual); 33 } 34 35 @Test testWithTrailingWhitespaceInTags()36 public void testWithTrailingWhitespaceInTags() throws Exception { 37 ResponseParser sut = new ResponseParser(); 38 Object actual = sut.parse(sh, strToStream(xmlDecl + 39 "<methodResponse >" + 40 " <params>" + 41 " <param>" + 42 " <value><string>toto</string></value>" + 43 " </param >" + 44 " </params>" + 45 "</methodResponse>"), false); 46 assertEquals("toto", actual); 47 } 48 49 @Test testWithTrailingEndlineInTags()50 public void testWithTrailingEndlineInTags() throws Exception { 51 ResponseParser sut = new ResponseParser(); 52 Object actual = sut.parse(sh, strToStream(xmlDecl + 53 "<methodResponse\n>" + 54 " <params>" + 55 " <param>" + 56 " <value><string>toto</string></value>" + 57 " </param\n>" + 58 " </params>" + 59 "</methodResponse>"), false); 60 assertEquals("toto", actual); 61 } 62 63 @Test testWithTrailingTabInTags()64 public void testWithTrailingTabInTags() throws Exception { 65 ResponseParser sut = new ResponseParser(); 66 Object actual = sut.parse(sh, strToStream(xmlDecl + 67 "<methodResponse\t>" + 68 " <params>" + 69 " <param>" + 70 " <value><string>toto</string></value>" + 71 " </param\t>" + 72 " </params>" + 73 "</methodResponse>"), false); 74 assertEquals("toto", actual); 75 } 76 77 @Test testResponseWithNonAsciiCharacter()78 public void testResponseWithNonAsciiCharacter() throws Exception { 79 ResponseParser sut = new ResponseParser(); 80 Object actual = sut.parse(sh, strToStream(xmlDecl + 81 "<methodResponse>" + 82 " <params>" + 83 " <param>" + 84 " <value><string>Aéris</string></value>" + 85 " </param>" + 86 " </params>" + 87 "</methodResponse>"), false); 88 assertEquals("Aéris", actual); 89 } 90 91 @Test testUTF16Response()92 public void testUTF16Response() throws Exception { 93 ResponseParser sut = new ResponseParser(); 94 Object actual = sut.parse(sh, bytesToStream((xmlDecl + 95 "<methodResponse>" + 96 " <params>" + 97 " <param>" + 98 " <value><string>toto</string></value>" + 99 " </param>" + 100 " </params>" + 101 "</methodResponse>").getBytes(StandardCharsets.UTF_16)), false); 102 assertEquals("toto", actual); 103 } 104 105 @Test testResponseWithComplexValue()106 public void testResponseWithComplexValue() throws Exception { 107 ResponseParser sut = new ResponseParser(); 108 Object actual = sut.parse(sh, strToStream(xmlDecl + 109 "<methodResponse>" + 110 " <params>" + 111 " <param>" + 112 " <value>" + 113 " <struct>" + 114 " <member><name>intValue</name><value><i4>12</i4></value></member>" + 115 " <member><name>otherIntValue</name><value><int>13</int></value></member>" + 116 " <member><name>boolValue</name><value><boolean>1</boolean></value></member>" + 117 " <member><name>strValue</name><value><string>toto</string></value></member>" + 118 " <member><name>doubleValue</name><value><double>12.4</double></value></member>" + 119 " <member><name>dateValue</name><value><dateTime.iso8601>20200908T0440Z</dateTime.iso8601></value></member>" + 120 // Don't test base64 because it seems assertEqual will do a reference equals on arrray of bytes so it's not easily testable here 121 // so we test it in a test below 122 //" <member><name>base64Value</name><value><base64>QWVyaXM=</base64></value></member>" + 123 " <member><name>nestedValue</name><value><struct> " + 124 " <member><name>innerStrValue</name><value><string>inner</string></value></member>" + 125 " </struct></value></member>" + 126 " </struct>" + 127 " </value>" + 128 " </param>" + 129 " </params>" + 130 "</methodResponse>"), false); 131 132 Map<Object, Object> expected = new TreeMap<>(); 133 expected.put("intValue", 12); 134 expected.put("otherIntValue", 13); 135 expected.put("boolValue", true); 136 expected.put("strValue", "toto"); 137 expected.put("doubleValue", 12.4); 138 expected.put("dateValue", new Date(1599540000000L)); 139 Map<Object, Object> innerStruct = new TreeMap<>(); 140 innerStruct.put("innerStrValue", "inner"); 141 expected.put("nestedValue", innerStruct); 142 assertEquals(expected, actual); 143 } 144 145 @Test testResponseWithBase64Value()146 public void testResponseWithBase64Value() throws Exception { 147 ResponseParser sut = new ResponseParser(); 148 Object actual = sut.parse(sh, strToStream(xmlDecl + 149 "<methodResponse>" + 150 " <params>" + 151 " <param>" + 152 " <value>" + 153 " <base64>QWVyaXM=</base64>" + 154 " </value>" + 155 " </param>" + 156 " </params>" + 157 "</methodResponse>"), false); 158 159 String actualAsStr = new String((byte[]) actual, StandardCharsets.UTF_8); 160 assertEquals("Aeris", actualAsStr); 161 } 162 163 @Test 164 /** 165 * I'm not sure it really makes sense to support this case. But since I'm adding tests on code which existed 166 * for years, I guess it's better to avoid breaking retro compatibility. 167 */ testAcceptMissingHeader()168 public void testAcceptMissingHeader() throws Exception { 169 ResponseParser sut = new ResponseParser(); 170 Object actual = sut.parse(sh, strToStream("<methodResponse>" + 171 " <params>" + 172 " <param>" + 173 " <value><string>toto</string></value>" + 174 " </param>" + 175 " </params>" + 176 "</methodResponse>"), false); 177 assertEquals("toto", actual); 178 } 179 180 @Test testXmlrpcError()181 public void testXmlrpcError() throws Exception { 182 ResponseParser sut = new ResponseParser(); 183 try { 184 sut.parse(sh, strToStream("<methodResponse>" + 185 " <fault>" + 186 " <value>" + 187 " <struct>" + 188 " <member>" + 189 " <name>faultCode</name>" + 190 " <value><int>4</int></value>" + 191 " </member>" + 192 " <member>" + 193 " <name>faultString</name>" + 194 " <value><string>error X occurred</string></value>" + 195 " </member>" + 196 " </struct>" + 197 " </value>" + 198 " </fault>" + 199 "</methodResponse>"), false); 200 fail("The previous call should have thrown"); 201 } catch (XMLRPCServerException e){ 202 assertEquals("error X occurred [4]", e.getMessage()); 203 assertEquals(4, e.getErrorNr()); 204 } 205 } 206 207 @Test testResponseWithXmlComment()208 public void testResponseWithXmlComment() throws Exception { 209 ResponseParser sut = new ResponseParser(); 210 Object actual = sut.parse(sh, strToStream("<methodResponse>" + 211 " <params>" + 212 " <param>" + 213 " <!--value><string>toto</string></value-->" + 214 " <value><string>tata</string></value>" + 215 " </param>" + 216 " </params>" + 217 "</methodResponse>"), false); 218 assertEquals("tata", actual); 219 } 220 221 @Test testResponseWithInlineXmlComment()222 public void testResponseWithInlineXmlComment() throws Exception { 223 ResponseParser sut = new ResponseParser(); 224 Object actual = sut.parse(sh, strToStream("<methodResponse>" + 225 " <params>" + 226 " <param>" + 227 " <value><string>ti<!--blah-->ti</string></value>" + 228 " </param>" + 229 " </params>" + 230 "</methodResponse>"), false); 231 assertEquals("titi", actual); 232 } 233 234 @Test testResponseWithSpecialChars()235 public void testResponseWithSpecialChars() throws Exception { 236 ResponseParser sut = new ResponseParser(); 237 Object actual = sut.parse(sh, strToStream("<methodResponse>" + 238 " <params>" + 239 " <param>" + 240 " <value><string>to<to</string></value>" + 241 " </param>" + 242 " </params>" + 243 "</methodResponse>"), false); 244 assertEquals("to<to", actual); 245 } 246 247 @Test(expected = XMLRPCException.class) testErrorMissingMethodResponseTag()248 public void testErrorMissingMethodResponseTag() throws Exception { 249 ResponseParser sut = new ResponseParser(); 250 Object actual = sut.parse(sh, strToStream( 251 " <params>" + 252 " <param>" + 253 " <value><string>toto</string></value>" + 254 " </param>" + 255 " </params>"), false); 256 } 257 258 @Test(expected = XMLRPCException.class) testErrorMissingParamsTag()259 public void testErrorMissingParamsTag() throws Exception { 260 ResponseParser sut = new ResponseParser(); 261 Object actual = sut.parse(sh, strToStream("<methodResponse>" + 262 " <param>" + 263 " <value><string>toto</string></value>" + 264 " </param>" + 265 "</methodResponse>"), false); 266 } 267 268 @Test(expected = XMLRPCException.class) testErrorMissingParamTag()269 public void testErrorMissingParamTag() throws Exception { 270 ResponseParser sut = new ResponseParser(); 271 Object actual = sut.parse(sh, strToStream("<methodResponse>" + 272 " <params>" + 273 " <value><string>toto</string></value>" + 274 " </params>" + 275 "</methodResponse>"), false); 276 } 277 strToStream(String str)278 private static InputStream strToStream(String str){ 279 return bytesToStream(str.getBytes(StandardCharsets.UTF_8)); 280 } 281 bytesToStream(byte[] bytes)282 private static InputStream bytesToStream(byte[] bytes){ 283 return new ByteArrayInputStream(bytes); 284 } 285 } 286