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

Add support for XEP-0363: HTTP File Upload.

Fixes SMACK-747
This commit is contained in:
Grigory Fedorov 2017-01-30 02:07:29 +05:00 committed by Florian Schmaus
parent a8c6de6b98
commit 72d4c8b611
17 changed files with 1073 additions and 1 deletions

View file

@ -0,0 +1,39 @@
/**
*
* Copyright © 2017 Grigory Fedorov
*
* 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.httpfileupload;
import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError;
import org.junit.Assert;
import org.junit.Test;
public class FileTooLargeErrorCreateTest {
String fileTooLargeErrorExtensionExample
= "<file-too-large xmlns='urn:xmpp:http:upload'>"
+ "<max-file-size>20000</max-file-size>"
+ "</file-too-large>";
@Test
public void checkFileTooLargeErrorExtensionCreation() {
FileTooLargeError fileTooLargeError = new FileTooLargeError(20000);
Assert.assertEquals(20000, fileTooLargeError.getMaxFileSize());
Assert.assertEquals(fileTooLargeErrorExtensionExample, fileTooLargeError.toXML().toString());
}
}

View file

@ -0,0 +1,58 @@
/**
*
* Copyright © 2017 Grigory Fedorov
*
* 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.httpfileupload;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError;
import org.junit.Assert;
import org.junit.Test;
public class FileTooLargeErrorProviderTest {
/**
* Example 7. Alternative response by the upload service if the file size was too large
* @see <a href="http://xmpp.org/extensions/xep-0363.html#errors">XEP-0363: HTTP File Upload 5. Error conditions</a>
*/
String slotErrorFileToLarge
= "<iq from='upload.montague.tld' "
+ "id='step_03' "
+ "to='romeo@montague.tld/garden' "
+ "type='error'>"
+ "<request xmlns='urn:xmpp:http:upload'>"
+ "<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'>"
+ "<max-file-size>20000</max-file-size>"
+ "</file-too-large>"
+ "</error>"
+ "</iq>";
@Test
public void checkSlotErrorFileToLarge() throws Exception {
IQ fileTooLargeErrorIQ = PacketParserUtils.parseStanza(slotErrorFileToLarge);
Assert.assertEquals(IQ.Type.error, fileTooLargeErrorIQ.getType());
FileTooLargeError fileTooLargeError = FileTooLargeError.from(fileTooLargeErrorIQ);
Assert.assertEquals(20000, fileTooLargeError.getMaxFileSize());
}
}

View file

@ -0,0 +1,46 @@
/**
*
* Copyright © 2017 Grigory Fedorov
*
* 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.httpfileupload;
import org.jivesoftware.smackx.httpfileupload.element.Slot;
import org.junit.Assert;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class SlotCreateTest {
String testSlot
= "<slot xmlns='urn:xmpp:http:upload'>"
+ "<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>";
@Test
public void checkSlotRequestCreation() throws MalformedURLException {
Slot slot = new Slot(new URL("https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"),
new URL("https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"));
Assert.assertEquals(new URL("https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"),
slot.getPutUrl());
Assert.assertEquals(new URL("https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"),
slot.getGetUrl());
Assert.assertEquals(testSlot, slot.getChildElementXML().toString());
}
}

View file

@ -0,0 +1,58 @@
/**
*
* Copyright © 2017 Grigory Fedorov
*
* 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.httpfileupload;
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;
public class SlotProviderTest {
/**
* Example 6. The upload service responds with a slot
* @see <a href="http://xmpp.org/extensions/xep-0363.html#request">XEP-0363: HTTP File Upload 4. Requesting a slot</a>
*/
String slotExample
= "<iq from='upload.montague.tld' "
+ "id='step_03' "
+ "to='romeo@montague.tld/garden' "
+ "type='result'>"
+ "<slot xmlns='urn:xmpp:http:upload'>"
+ "<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>"
+ "</iq>";
@Test
public void checkSlotProvider() throws Exception {
XmlPullParser parser = PacketParserUtils.getParserFor(slotExample);
Slot slot = new SlotProvider().parse(parser);
Assert.assertEquals(IQ.Type.result, slot.getType());
Assert.assertEquals(new URL("https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"),
slot.getPutUrl());
Assert.assertEquals(new URL("https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my_juliet.png"),
slot.getGetUrl());
}
}

View file

@ -0,0 +1,71 @@
/**
*
* Copyright © 2017 Grigory Fedorov
*
* 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.httpfileupload;
import org.jivesoftware.smackx.httpfileupload.element.SlotRequest;
import org.junit.Assert;
import org.junit.Test;
import org.jxmpp.stringprep.XmppStringprepException;
public class SlotRequestCreateTest {
String testRequest
= "<request xmlns='urn:xmpp:http:upload'>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "<content-type>image/jpeg</content-type>"
+ "</request>";
String testRequestWithoutContentType
= "<request xmlns='urn:xmpp:http:upload'>"
+ "<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");
Assert.assertEquals("my_juliet.png", slotRequest.getFilename());
Assert.assertEquals(23456, slotRequest.getSize());
Assert.assertEquals("image/jpeg", slotRequest.getContentType());
Assert.assertEquals(testRequest, slotRequest.getChildElementXML().toString());
}
@Test
public void checkSlotRequestCreationWithoutContentType() throws XmppStringprepException {
SlotRequest slotRequest = new SlotRequest("my_romeo.png", 52523);
Assert.assertEquals("my_romeo.png", slotRequest.getFilename());
Assert.assertEquals(52523, slotRequest.getSize());
Assert.assertEquals(null, slotRequest.getContentType());
Assert.assertEquals(testRequestWithoutContentType, slotRequest.getChildElementXML().toString());
}
@Test(expected = IllegalArgumentException.class)
public void checkSlotRequestCreationNegativeSize() {
new SlotRequest("my_juliet.png", -23456, "image/jpeg");
}
@Test(expected = IllegalArgumentException.class)
public void checkSlotRequestCreationZeroSize() {
new SlotRequest("my_juliet.png", 0, "image/jpeg");
}
}