/** * * Copyright 2014 Andriy Tsykholyas * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jivesoftware.smackx.hoxt.provider; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.hoxt.packet.AbstractHttpOverXmpp; import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq; import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp; import org.jivesoftware.smackx.shim.packet.Header; import org.jivesoftware.smackx.shim.packet.HeadersExtension; import org.junit.Test; import org.xmlpull.v1.XmlPullParser; import java.util.Collection; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** * Tests correct headers and data parsing in 'req' and 'resp' elements. */ public class AbstractHttpOverXmppProviderTest { @Test public void areRespHeadersParsedCorrectly() throws Exception { String string = "" + "" + "
Fri, 03 May 2013 13:52:10 GMT-4
" + "
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE
" + "
0
" + "
" + "
"; Map expectedHeaders = new HashMap(); expectedHeaders.put("Date", "Fri, 03 May 2013 13:52:10 GMT-4"); expectedHeaders.put("Allow", "OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE"); expectedHeaders.put("Content-Length", "0"); HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider(); XmlPullParser parser = PacketParserUtils.getParserFor(string); IQ iq = provider.parse(parser); assertTrue(iq instanceof HttpOverXmppResp); HttpOverXmppResp body = ((HttpOverXmppResp) iq); checkHeaders(body.getHeaders(), expectedHeaders); } @Test public void areReqHeadersParsedCorrectly() throws Exception { String string = "" + "" + "
clayster.com
" + "
" + "
"; Map expectedHeaders = new HashMap(); expectedHeaders.put("Host", "clayster.com"); HttpOverXmppReqProvider provider = new HttpOverXmppReqProvider(); XmlPullParser parser = PacketParserUtils.getParserFor(string); IQ iq = provider.parse(parser); assertTrue(iq instanceof HttpOverXmppReq); HttpOverXmppReq body = ((HttpOverXmppReq) iq); checkHeaders(body.getHeaders(), expectedHeaders); } @Test public void isTextDataParsedCorrectly() throws Exception { String expectedText = "@prefix dc: ." + "@base ." + " dc:title \"HTTP over XMPP\";" + "dc:creator ;" + "dc:publisher ."; String encodedText = "@prefix dc: <http://purl.org/dc/elements/1.1/>." + "@base <http://clayster.com/>." + "<xep> dc:title \"HTTP over XMPP\";" + "dc:creator <PeterWaher>;" + "dc:publisher <XSF>."; String string = "" + "
Clayster
" + "" + encodedText + "
"; AbstractHttpOverXmpp.Text text = (AbstractHttpOverXmpp.Text) parseAbstractBody( string, "resp").getData().getChild(); assertEquals(expectedText, text.getText()); } @Test public void isXmlDataParsedCorrectly() throws Exception { String expectedXml = "" // no xmlns here + "" + "" + "HTTP over XMPP" + "" + "" + "http://clayster.com/PeterWaher" + "" + "" + "" + ""; String encodedXml = "" + "" + "" + "HTTP over XMPP" + "" + "" + "http://clayster.com/PeterWaher" + "" + "" + "" + ""; String string = "" + "
Clayster
" + "" + encodedXml + "
"; AbstractHttpOverXmpp.Xml xmlProviderValue = (AbstractHttpOverXmpp.Xml) parseAbstractBody( string, "resp").getData().getChild(); assertEquals(expectedXml, xmlProviderValue.getText()); } @Test public void isBase64DataParsedCorrectly() throws Exception { String base64Data = "iVBORw0KGgoAAAANSUhEUgAAASwAAAGQCAYAAAAUdV17AAAAAXNSR0 ... tVWJd+e+y1AAAAABJRU5ErkJggg=="; String string = "" + "
Clayster
" + "" + base64Data + "
"; AbstractHttpOverXmpp.Base64 base64ProviderValue = (AbstractHttpOverXmpp.Base64) parseAbstractBody( string, "resp").getData().getChild(); assertEquals(base64Data, base64ProviderValue.getText()); } @Test public void isChunkedBase64DataParsedCorrectly() throws Exception { String streamId = "Stream0001"; String chunkBase64Data = " "; String string = "" + "
Clayster
" + "" + chunkBase64Data + "
"; AbstractHttpOverXmpp.ChunkedBase64 chunkedBase64Value = (AbstractHttpOverXmpp.ChunkedBase64) parseAbstractBody( string, "resp").getData().getChild(); assertEquals(streamId, chunkedBase64Value.getStreamId()); } @Test public void isIbbDataParsedCorrectly() throws Exception { String sid = "Stream0002"; String ibbData = " "; String string = "" + "
Clayster
" + "" + ibbData + "
"; AbstractHttpOverXmpp.Ibb ibbValue = (AbstractHttpOverXmpp.Ibb) parseAbstractBody( string, "resp").getData().getChild(); assertEquals(sid, ibbValue.getSid()); } // TODO The method name makes no sense after the HOXT re-design, change to parseHttpOverXmppResp() private HttpOverXmppResp parseAbstractBody(String string, String tag) throws Exception { HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider(); XmlPullParser parser = PacketParserUtils.getParserFor(string, tag); IQ iq = provider.parse(parser); assertTrue(iq instanceof HttpOverXmppResp); return (HttpOverXmppResp) iq; } private void checkHeaders(HeadersExtension headers, Map expectedHeaders) { Collection
collection = headers.getHeaders(); assertEquals(collection.size(), expectedHeaders.size()); for (Header header : collection) { assertTrue(expectedHeaders.containsKey(header.getName())); assertEquals(expectedHeaders.get(header.getName()), header.getValue()); } } }