use of okhttp3.ResponseBody in project retrofit by square.
the class RequestBuilderTest method multipartNullRemovesPart.
@Test
public void multipartNullRemovesPart() throws IOException {
class Example {
//
@Multipart
//
@POST("/foo/bar/")
Call<ResponseBody> method(@Part("ping") String ping, @Part("fizz") String fizz) {
return null;
}
}
Request request = buildRequest(Example.class, "pong", null);
assertThat(request.method()).isEqualTo("POST");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
RequestBody body = request.body();
Buffer buffer = new Buffer();
body.writeTo(buffer);
String bodyString = buffer.readUtf8();
assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"ping\"").contains("\r\npong\r\n--");
}
use of okhttp3.ResponseBody in project retrofit by square.
the class RequestBuilderTest method getWithQueryNameParam.
@Test
public void getWithQueryNameParam() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@QueryName String ping) {
return null;
}
}
Request request = buildRequest(Example.class, "pong");
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?pong");
assertThat(request.body()).isNull();
}
use of okhttp3.ResponseBody in project retrofit by square.
the class RequestBuilderTest method getWithEncodedPathParam.
@Test
public void getWithEncodedPathParam() {
class Example {
//
@GET("/foo/bar/{ping}/")
Call<ResponseBody> method(@Path(value = "ping", encoded = true) String ping) {
return null;
}
}
Request request = buildRequest(Example.class, "po%20ng");
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/po%20ng/");
assertThat(request.body()).isNull();
}
use of okhttp3.ResponseBody in project okhttp by square.
the class JavaApiConverterTest method createJavaUrlConnection_responseHeadersOk.
@Test
public void createJavaUrlConnection_responseHeadersOk() throws Exception {
ResponseBody responseBody = createResponseBody("BodyText");
Response okResponse = new Response.Builder().request(createArbitraryOkRequest()).protocol(Protocol.HTTP_1_1).code(200).message("Fantastic").addHeader("A", "c").addHeader("B", "d").addHeader("A", "e").addHeader("Content-Length", Long.toString(responseBody.contentLength())).body(responseBody).build();
HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse);
assertEquals(200, httpUrlConnection.getResponseCode());
assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
assertEquals(responseBody.contentLength(), httpUrlConnection.getContentLength());
// Check retrieval by string key.
assertEquals("HTTP/1.1 200 Fantastic", httpUrlConnection.getHeaderField(null));
assertEquals("e", httpUrlConnection.getHeaderField("A"));
// The RI and OkHttp supports case-insensitive matching for this method.
assertEquals("e", httpUrlConnection.getHeaderField("a"));
// Check retrieval using a Map.
Map<String, List<String>> responseHeaders = httpUrlConnection.getHeaderFields();
assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), responseHeaders.get(null));
assertEquals(newSet("c", "e"), newSet(responseHeaders.get("A")));
// OkHttp supports case-insensitive matching here. The RI does not.
assertEquals(newSet("c", "e"), newSet(responseHeaders.get("a")));
// Check the Map iterator contains the expected mappings.
assertHeadersContainsMapping(responseHeaders, null, "HTTP/1.1 200 Fantastic");
assertHeadersContainsMapping(responseHeaders, "A", "c", "e");
assertHeadersContainsMapping(responseHeaders, "B", "d");
// Check immutability of the headers Map.
try {
responseHeaders.put("N", Arrays.asList("o"));
fail("Modified an unmodifiable view.");
} catch (UnsupportedOperationException expected) {
}
try {
responseHeaders.get("A").add("f");
fail("Modified an unmodifiable view.");
} catch (UnsupportedOperationException expected) {
}
// Check retrieval of headers by index.
assertEquals(null, httpUrlConnection.getHeaderFieldKey(0));
assertEquals("HTTP/1.1 200 Fantastic", httpUrlConnection.getHeaderField(0));
// After header zero there may be additional entries provided at the beginning or end by the
// implementation. It's probably important that the relative ordering of the headers is
// preserved, particularly if there are multiple value for the same key.
int i = 1;
while (!httpUrlConnection.getHeaderFieldKey(i).equals("A")) {
i++;
}
// Check the ordering of the headers set by app code.
assertResponseHeaderAtIndex(httpUrlConnection, i++, "A", "c");
assertResponseHeaderAtIndex(httpUrlConnection, i++, "B", "d");
assertResponseHeaderAtIndex(httpUrlConnection, i++, "A", "e");
// There may be some additional headers provided by the implementation.
while (httpUrlConnection.getHeaderField(i) != null) {
assertNotNull(httpUrlConnection.getHeaderFieldKey(i));
i++;
}
// Confirm the correct behavior when the index is out-of-range.
assertNull(httpUrlConnection.getHeaderFieldKey(i));
}
use of okhttp3.ResponseBody in project okhttp by square.
the class JavaApiConverterTest method createResponseBody.
private static ResponseBody createResponseBody(String bodyText) {
final Buffer source = new Buffer().writeUtf8(bodyText);
final long contentLength = source.size();
return new ResponseBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain; charset=utf-8");
}
@Override
public long contentLength() {
return contentLength;
}
@Override
public BufferedSource source() {
return source;
}
};
}
Aggregations