1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-27 18:21:14 +03:00

Import mimecraft as-is.

This commit is contained in:
Jesse Wilson
2014-06-07 09:44:56 -07:00
parent 349251ffb6
commit 6a2242ce30
10 changed files with 891 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
// Copyright 2013 Square, Inc.
package com.squareup.okhttp;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import org.junit.Test;
import static com.squareup.okhttp.TestUtils.UTF_8;
import static org.junit.Assert.assertEquals;
public class ComplexExamplesTest {
@Test public void fieldAndTwoFiles() throws Exception {
String expected = ""
+ "--AaB03x\r\n"
+ "Content-Disposition: form-data; name=\"submit-name\"\r\n"
+ "Content-Length: 5\r\n"
+ "\r\n"
+ "Larry\r\n"
+ "--AaB03x\r\n"
+ "Content-Disposition: form-data; name=\"files\"\r\n"
+ "Content-Type: multipart/mixed; boundary=BbC04y\r\n"
+ "\r\n"
+ "--BbC04y\r\n"
+ "Content-Disposition: file; filename=\"file1.txt\"\r\n"
+ "Content-Type: text/plain\r\n"
+ "Content-Length: 29\r\n"
+ "\r\n"
+ "... contents of file1.txt ...\r\n"
+ "--BbC04y\r\n"
+ "Content-Disposition: file; filename=\"file2.gif\"\r\n"
+ "Content-Type: image/gif\r\n"
+ "Content-Length: 29\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n"
+ "... contents of file2.gif ...\r\n"
+ "--BbC04y--\r\n"
+ "--AaB03x--";
Multipart m = new Multipart.Builder("AaB03x") //
.type(Multipart.Type.FORM) //
.addPart(new Part.Builder() //
.contentDisposition("form-data; name=\"submit-name\"") //
.body("Larry") //
.build()) //
.addPart(new Part.Builder() //
.contentDisposition("form-data; name=\"files\"") //
.body(new Multipart.Builder("BbC04y") //
.addPart(new Part.Builder() //
.contentDisposition("file; filename=\"file1.txt\"") //
.contentType("text/plain") //
.body("... contents of file1.txt ...") //
.build()) //
.addPart(new Part.Builder() //
.contentDisposition("file; filename=\"file2.gif\"") //
.contentType("image/gif") //
.contentEncoding("binary") //
.body("... contents of file2.gif ...") //
.build()) //
.build()) //
.build()) //
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
m.writeBodyTo(out);
String actual = new String(out.toByteArray(), UTF_8);
assertEquals(expected, actual);
assertEquals(Collections.singletonMap("Content-Type", "multipart/form-data; boundary=AaB03x"),
m.getHeaders());
}
}

View File

@@ -0,0 +1,47 @@
// Copyright 2013 Square, Inc.
package com.squareup.okhttp;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import org.junit.Test;
import static com.squareup.okhttp.TestUtils.UTF_8;
import static org.junit.Assert.assertEquals;
public class FormWriterTest {
@Test public void urlEncoding() throws Exception {
FormEncoding fe = new FormEncoding.Builder() //
.add("a&b", "c=d") //
.add("space, the", "final frontier") //
.build();
assertEquals(Collections.singletonMap("Content-Type", "application/x-www-form-urlencoded"),
fe.getHeaders());
ByteArrayOutputStream out = new ByteArrayOutputStream();
fe.writeBodyTo(out);
String actual = new String(out.toByteArray(), UTF_8);
assertEquals("a%26b=c%3Dd&space%2C+the=final+frontier", actual);
}
@Test public void encodedPairs() throws Exception {
FormEncoding fe1 = new FormEncoding.Builder() //
.add("sim", "ple") //
.build();
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
fe1.writeBodyTo(out1);
String actual1 = new String(out1.toByteArray(), UTF_8);
assertEquals("sim=ple", actual1);
FormEncoding fe2 = new FormEncoding.Builder() //
.add("sim", "ple") //
.add("hey", "there") //
.add("help", "me") //
.build();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
fe2.writeBodyTo(out2);
String actual2 = new String(out2.toByteArray(), UTF_8);
assertEquals("sim=ple&hey=there&help=me", actual2);
}
}

View File

@@ -0,0 +1,63 @@
// Copyright 2013 Square, Inc.
package com.squareup.okhttp;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import org.junit.Test;
import static com.squareup.okhttp.TestUtils.UTF_8;
import static org.junit.Assert.assertEquals;
public class MultipartWriterTest {
@Test(expected = IllegalStateException.class)
public void onePartRequired() throws Exception {
new Multipart.Builder().build();
}
@Test public void singlePart() throws Exception {
String expected = "" //
+ "--123\r\n" //
+ "\r\n" //
+ "Hello, World!\r\n" //
+ "--123--";
Multipart m = new Multipart.Builder("123")
.addPart(new TestPart("Hello, World!"))
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
m.writeBodyTo(out);
String actual = new String(out.toByteArray(), UTF_8);
assertEquals(expected, actual);
assertEquals(Collections.singletonMap("Content-Type", "multipart/mixed; boundary=123"),
m.getHeaders());
}
@Test public void threeParts() throws Exception {
String expected = ""
+ "--123\r\n"
+ "\r\n"
+ "Quick\r\n"
+ "--123\r\n"
+ "\r\n"
+ "Brown\r\n"
+ "--123\r\n"
+ "\r\n"
+ "Fox\r\n"
+ "--123--";
Multipart m = new Multipart.Builder("123")
.addPart(new TestPart("Quick"))
.addPart(new TestPart("Brown"))
.addPart(new TestPart("Fox"))
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
m.writeBodyTo(out);
String actual = new String(out.toByteArray(), UTF_8);
assertEquals(expected, actual);
assertEquals(Collections.singletonMap("Content-Type", "multipart/mixed; boundary=123"),
m.getHeaders());
}
}

View File

@@ -0,0 +1,152 @@
// Copyright 2013 Square, Inc.
package com.squareup.okhttp;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class PartTest {
@Test(expected = IllegalStateException.class)
public void noBodyThrowsException() {
new Part.Builder().build();
}
@Test public void settingHeaderTwiceThrowsException() {
try {
new Part.Builder().contentEncoding("foo").contentEncoding("bar");
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentLanguage("foo").contentLanguage("bar");
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentLength(42).contentLength(108);
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentType("foo").contentType("bar");
fail();
} catch (IllegalStateException expected) {
}
}
@Test public void settingBodyTwiceThrowsException() {
try {
new Part.Builder().body("foo").body("bar");
fail();
} catch (IllegalStateException expected) {
}
try {
byte[] bytes = { 'a', 'b' };
new Part.Builder().body(bytes).body(bytes);
fail();
} catch (IllegalStateException expected) {
}
try {
InputStream is = new ByteArrayInputStream(new byte[0]);
new Part.Builder().body(is).body(is);
fail();
} catch (IllegalStateException expected) {
}
try {
File file = new File("/foo/bar");
new Part.Builder().body(file).body(file);
fail();
} catch (IllegalStateException expected) {
}
}
@Test public void blankHeadersThrowsException() {
try {
new Part.Builder().contentType("");
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentLength(0);
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentLength(-1);
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentLanguage("");
fail();
} catch (IllegalStateException expected) {
}
try {
new Part.Builder().contentEncoding("");
fail();
} catch (IllegalStateException expected) {
}
}
@Test public void bodyBytesSetsContentLength() {
Part.Builder b = new Part.Builder();
assertEquals(0, b.headerLength);
b.body("1234567");
assertEquals(7, b.headerLength);
}
@Test public void bodyBytesOverridesLength() {
Part.Builder b = new Part.Builder();
assertEquals(0, b.headerLength);
b.contentLength(108);
assertEquals(108, b.headerLength);
b.body("1234567");
assertEquals(7, b.headerLength);
}
@Test public void completePart() throws Exception {
Part p = new Part.Builder() //
.contentDisposition("form-data; filename=\"foo.txt\"") //
.contentType("application/json") //
.contentLength(13) //
.contentLanguage("English") //
.contentEncoding("UTF-8") //
.body("{'foo':'bar'}") //
.build();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
p.writeBodyTo(baos);
String actual = new String(baos.toByteArray(), TestUtils.UTF_8);
assertEquals("{'foo':'bar'}", actual);
Map<String, String> expectedHeaders = new LinkedHashMap<String, String>();
expectedHeaders.put("Content-Disposition", "form-data; filename=\"foo.txt\"");
expectedHeaders.put("Content-Type", "application/json");
expectedHeaders.put("Content-Length", "13");
expectedHeaders.put("Content-Language", "English");
expectedHeaders.put("Content-Transfer-Encoding", "UTF-8");
assertEquals(expectedHeaders, p.getHeaders());
}
@Test public void multipartBodySetsType() throws Exception {
Multipart m = new Multipart.Builder().addPart(new TestPart("hi")).build();
try {
new Part.Builder().body(m).contentType("break me!");
fail();
} catch (IllegalStateException expected) {
}
Part p = new Part.Builder().body(m).build();
assertEquals(Collections.singletonMap("Content-Type", m.getHeaders().get("Content-Type")),
p.getHeaders());
}
}

View File

@@ -0,0 +1,25 @@
// Copyright 2013 Square, Inc.
package com.squareup.okhttp;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Map;
import static com.squareup.okhttp.TestUtils.UTF_8;
public class TestPart implements Part {
private final byte[] content;
public TestPart(String content) {
this.content = content.getBytes(UTF_8);
}
@Override public Map<String, String> getHeaders() {
return Collections.emptyMap();
}
@Override public void writeBodyTo(OutputStream out) throws IOException {
out.write(content);
}
}

View File

@@ -0,0 +1,11 @@
package com.squareup.okhttp;
import java.nio.charset.Charset;
final class TestUtils {
static final Charset UTF_8 = Charset.forName("UTF-8");
private TestUtils() {
// prevent instantiation
}
}