2010-03-24 20:44:02 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2010 Jive Software.
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2010-03-24 20:44:02 +01:00
|
|
|
* 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.smack.packet;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
2010-03-24 20:44:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that verifies the correct behavior of creating result and error IQ packets.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2010-03-24 20:44:02 +01:00
|
|
|
* @see <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq">IQ Semantics</a>
|
|
|
|
* @author Guenther Niess
|
|
|
|
*/
|
|
|
|
public class IQResponseTest {
|
|
|
|
|
2014-11-07 21:12:01 +01:00
|
|
|
private static final String ELEMENT = "child";
|
|
|
|
private static final String NAMESPACE = "http://igniterealtime.org/protocol/test";
|
2010-03-24 20:44:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creating a simple and empty IQ response.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XmppStringprepException if the provided string is invalid.
|
2010-03-24 20:44:02 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2015-02-14 17:15:02 +01:00
|
|
|
public void testGeneratingSimpleResponse() throws XmppStringprepException {
|
2014-11-07 21:12:01 +01:00
|
|
|
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
|
2015-02-14 17:15:02 +01:00
|
|
|
request.setFrom(JidCreate.from("sender@test/Smack"));
|
|
|
|
request.setTo(JidCreate.from("receiver@test/Smack"));
|
2010-03-24 20:44:02 +01:00
|
|
|
|
|
|
|
final IQ result = IQ.createResultIQ(request);
|
|
|
|
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.result, result.getType());
|
2015-02-05 11:17:27 +01:00
|
|
|
assertNotNull(result.getStanzaId());
|
|
|
|
assertEquals(request.getStanzaId(), result.getStanzaId());
|
2010-03-24 20:44:02 +01:00
|
|
|
assertEquals(request.getFrom(), result.getTo());
|
|
|
|
assertEquals(request.getTo(), result.getFrom());
|
2014-11-07 21:12:01 +01:00
|
|
|
assertEquals("", result.getChildElementXML().toString());
|
2010-03-24 20:44:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creating a error response based on an IQ request.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XmppStringprepException if the provided string is invalid.
|
2010-03-24 20:44:02 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2015-02-14 17:15:02 +01:00
|
|
|
public void testGeneratingValidErrorResponse() throws XmppStringprepException {
|
2019-10-24 15:45:08 +02:00
|
|
|
final StanzaError error = StanzaError.getBuilder(StanzaError.Condition.bad_request).build();
|
2014-11-07 21:12:01 +01:00
|
|
|
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
|
|
|
|
|
2014-06-06 02:20:45 +02:00
|
|
|
request.setType(IQ.Type.set);
|
2015-02-14 17:15:02 +01:00
|
|
|
request.setFrom(JidCreate.from("sender@test/Smack"));
|
|
|
|
request.setTo(JidCreate.from("receiver@test/Smack"));
|
2010-03-24 20:44:02 +01:00
|
|
|
|
|
|
|
final IQ result = IQ.createErrorResponse(request, error);
|
|
|
|
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.error, result.getType());
|
2015-02-05 11:17:27 +01:00
|
|
|
assertNotNull(result.getStanzaId());
|
|
|
|
assertEquals(request.getStanzaId(), result.getStanzaId());
|
2010-03-24 20:44:02 +01:00
|
|
|
assertEquals(request.getFrom(), result.getTo());
|
2019-10-24 15:45:08 +02:00
|
|
|
assertEquals(error.toXML().toString(), result.getError().toXML().toString());
|
2014-11-07 21:12:01 +01:00
|
|
|
// TODO this test was never valid
|
|
|
|
// assertEquals(CHILD_ELEMENT, result.getChildElementXML());
|
2010-03-24 20:44:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* According to <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq"
|
|
|
|
* >RFC3920: IQ Semantics</a> we shouldn't respond to an IQ of type result.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XmppStringprepException if the provided string is invalid.
|
2010-03-24 20:44:02 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2015-02-14 17:15:02 +01:00
|
|
|
public void testGeneratingResponseBasedOnResult() throws XmppStringprepException {
|
2014-11-07 21:12:01 +01:00
|
|
|
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
|
|
|
|
|
2014-06-06 02:20:45 +02:00
|
|
|
request.setType(IQ.Type.result);
|
2015-02-14 17:15:02 +01:00
|
|
|
request.setFrom(JidCreate.from("sender@test/Smack"));
|
|
|
|
request.setTo(JidCreate.from("receiver@test/Smack"));
|
2010-03-24 20:44:02 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
IQ.createResultIQ(request);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail("It shouldn't be possible to generate a response for a result IQ!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* According to <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq"
|
|
|
|
* >RFC3920: IQ Semantics</a> we shouldn't respond to an IQ of type error.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XmppStringprepException if the provided string is invalid.
|
2010-03-24 20:44:02 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2015-02-14 17:15:02 +01:00
|
|
|
public void testGeneratingErrorBasedOnError() throws XmppStringprepException {
|
2019-10-24 15:45:08 +02:00
|
|
|
final StanzaError error = StanzaError.getBuilder(StanzaError.Condition.bad_request).build();
|
2014-11-07 21:12:01 +01:00
|
|
|
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
|
|
|
|
|
2014-06-06 02:20:45 +02:00
|
|
|
request.setType(IQ.Type.error);
|
2015-02-14 17:15:02 +01:00
|
|
|
request.setFrom(JidCreate.from("sender@test/Smack"));
|
|
|
|
request.setTo(JidCreate.from("receiver@test/Smack"));
|
2010-03-24 20:44:02 +01:00
|
|
|
request.setError(error);
|
|
|
|
|
|
|
|
try {
|
|
|
|
IQ.createErrorResponse(request, error);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail("It shouldn't be possible to generate a response for a error IQ!");
|
|
|
|
}
|
|
|
|
}
|