1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-08 22:21:08 +01:00

Fix and improve the HTTP File Upload implementation

Fix a few resource leaks. Improve the API and add an integration
test. Also add compability layer for XEP-0363: HTTP File Upload 0.2.

SMACK-747
This commit is contained in:
Florian Schmaus 2017-03-09 21:35:29 +01:00
parent 72d4c8b611
commit 09b6608a3a
26 changed files with 691 additions and 177 deletions

View file

@ -23,7 +23,7 @@ import org.junit.Test;
public class FileTooLargeErrorCreateTest {
String fileTooLargeErrorExtensionExample
= "<file-too-large xmlns='urn:xmpp:http:upload'>"
= "<file-too-large xmlns='urn:xmpp:http:upload:0'>"
+ "<max-file-size>20000</max-file-size>"
+ "</file-too-large>";

View file

@ -26,7 +26,7 @@ import java.net.URL;
public class SlotCreateTest {
String testSlot
= "<slot xmlns='urn:xmpp:http:upload'>"
= "<slot xmlns='urn:xmpp:http:upload:0'>"
+ "<put>https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png</put>"
+ "<get>https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png</get>"
+ "</slot>";

View file

@ -19,27 +19,28 @@ package org.jivesoftware.smackx.httpfileupload;
import org.jivesoftware.smackx.httpfileupload.element.SlotRequest;
import org.junit.Assert;
import org.junit.Test;
import org.jxmpp.jid.JidTestUtil;
import org.jxmpp.stringprep.XmppStringprepException;
public class SlotRequestCreateTest {
String testRequest
= "<request xmlns='urn:xmpp:http:upload'>"
= "<request xmlns='urn:xmpp:http:upload:0'>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "<content-type>image/jpeg</content-type>"
+ "</request>";
String testRequestWithoutContentType
= "<request xmlns='urn:xmpp:http:upload'>"
= "<request xmlns='urn:xmpp:http:upload:0'>"
+ "<filename>my_romeo.png</filename>"
+ "<size>52523</size>"
+ "</request>";
@Test
public void checkSlotRequestCreation() throws XmppStringprepException {
SlotRequest slotRequest = new SlotRequest("my_juliet.png", 23456, "image/jpeg");
SlotRequest slotRequest = new SlotRequest(JidTestUtil.DOMAIN_BARE_JID_1, "my_juliet.png", 23456, "image/jpeg");
Assert.assertEquals("my_juliet.png", slotRequest.getFilename());
Assert.assertEquals(23456, slotRequest.getSize());
@ -50,7 +51,7 @@ public class SlotRequestCreateTest {
@Test
public void checkSlotRequestCreationWithoutContentType() throws XmppStringprepException {
SlotRequest slotRequest = new SlotRequest("my_romeo.png", 52523);
SlotRequest slotRequest = new SlotRequest(JidTestUtil.DOMAIN_BARE_JID_1, "my_romeo.png", 52523);
Assert.assertEquals("my_romeo.png", slotRequest.getFilename());
Assert.assertEquals(52523, slotRequest.getSize());
@ -61,11 +62,11 @@ public class SlotRequestCreateTest {
@Test(expected = IllegalArgumentException.class)
public void checkSlotRequestCreationNegativeSize() {
new SlotRequest("my_juliet.png", -23456, "image/jpeg");
new SlotRequest(JidTestUtil.DOMAIN_BARE_JID_1, "my_juliet.png", -23456, "image/jpeg");
}
@Test(expected = IllegalArgumentException.class)
public void checkSlotRequestCreationZeroSize() {
new SlotRequest("my_juliet.png", 0, "image/jpeg");
new SlotRequest(JidTestUtil.DOMAIN_BARE_JID_1, "my_juliet.png", 0, "image/jpeg");
}
}

View file

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.httpfileupload;
package org.jivesoftware.smackx.httpfileupload.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -33,14 +33,14 @@ public class FileTooLargeErrorProviderTest {
+ "id='step_03' "
+ "to='romeo@montague.tld/garden' "
+ "type='error'>"
+ "<request xmlns='urn:xmpp:http:upload'>"
+ "<request xmlns='urn:xmpp:http:upload:0'>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "</request>"
+ "<error type='modify'>"
+ "<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' />"
+ "<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>File too large. The maximum file size is 20000 bytes</text>"
+ "<file-too-large xmlns='urn:xmpp:http:upload'>"
+ "<file-too-large xmlns='urn:xmpp:http:upload:0'>"
+ "<max-file-size>20000</max-file-size>"
+ "</file-too-large>"
+ "</error>"

View file

@ -14,15 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.httpfileupload;
package org.jivesoftware.smackx.httpfileupload.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.httpfileupload.element.Slot;
import org.jivesoftware.smackx.httpfileupload.provider.SlotProvider;
import org.junit.Assert;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import java.net.URL;
@ -38,7 +36,7 @@ public class SlotProviderTest {
+ "id='step_03' "
+ "to='romeo@montague.tld/garden' "
+ "type='result'>"
+ "<slot xmlns='urn:xmpp:http:upload'>"
+ "<slot xmlns='urn:xmpp:http:upload:0'>"
+ "<put>https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png</put>"
+ "<get>https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png</get>"
+ "</slot>"
@ -46,8 +44,7 @@ public class SlotProviderTest {
@Test
public void checkSlotProvider() throws Exception {
XmlPullParser parser = PacketParserUtils.getParserFor(slotExample);
Slot slot = new SlotProvider().parse(parser);
Slot slot = PacketParserUtils.parseStanza(slotExample);
Assert.assertEquals(IQ.Type.result, slot.getType());
Assert.assertEquals(new URL("https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"),