@Test public void headersInMessageTest() throws Exception { // @formatter:off final String messageStanza = "<message xmlns='jabber:client' from='romeo@shakespeare.lit/orchard' to='juliet@capulet.com' type='chat'>" + "<body>Wherefore are thou?!?</body>" + "<headers xmlns='http://jabber.org/protocol/shim'>" + "<header name='Urgency'>high</header>" + "</headers>" + "</message>"; // @formatter:on XmlPullParser parser = TestUtils.getMessageParser(messageStanza); Message message = PacketParserUtils.parseMessage(parser); HeadersExtension headers = HeadersExtension.from(message); Header header = headers.getHeaders().get(0); assertEquals("Urgency", header.getName()); assertEquals("high", header.getValue()); } }
@Test public void consumeUnparsedInput() throws Exception { final String MESSAGE_EXCEPTION_ELEMENT = "<" + ThrowException.ELEMENT + " xmlns='" + ThrowException.NAMESPACE + "'>" + "<nothingInHere>" + "</nothingInHere>" + "</" + ThrowException.ELEMENT + ">"; XmlPullParser parser = TestUtils.getMessageParser( "<message from='user@server.example' to='francisco@denmark.lit' id='foo'>" + MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>"); int parserDepth = parser.getDepth(); CharSequence content = null; try { PacketParserUtils.parseMessage(parser); } catch (Exception e) { content = PacketParserUtils.parseContentDepth(parser, parserDepth, false); } assertThat(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>", equalsCharSequence(content)); }
/** * RFC6121 5.2.3 explicitly disallows mixed content in <body/> elements. Make sure that we throw * an exception if we encounter such an element. * * @throws Exception */ @Test(expected = XmlPullParserException.class) public void invalidMessageBodyContainingTagTest() throws Exception { String control = XMLBuilder.create("message") .namespace(StreamOpen.CLIENT_NAMESPACE) .a("from", "romeo@montague.lit/orchard") .a("to", "juliet@capulet.lit/balcony") .a("id", "zid615d9") .a("type", "chat") .a("xml:lang", "en") .e("body") .a("xmlns", "http://www.w3.org/1999/xhtml") .e("span") .a("style", "font-weight: bold;") .t("Bad Message Body") .asString(outputProperties); Message message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control)); fail("Should throw exception. Instead got message: " + message.toXML(null).toString()); }