use of okhttp3.RequestBody in project okhttp by square.
the class DisconnectTest method interruptWritingRequestBody.
@Test
public void interruptWritingRequestBody() throws Exception {
// 2 MiB
int requestBodySize = 2 * 1024 * 1024;
server.enqueue(new MockResponse().throttleBody(64 * 1024, 125, // 500 Kbps
TimeUnit.MILLISECONDS));
server.start();
HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
disconnectLater(connection, 500);
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(requestBodySize);
OutputStream requestBody = connection.getOutputStream();
byte[] buffer = new byte[1024];
try {
for (int i = 0; i < requestBodySize; i += buffer.length) {
requestBody.write(buffer);
requestBody.flush();
}
fail("Expected connection to be closed");
} catch (IOException expected) {
}
connection.disconnect();
}
use of okhttp3.RequestBody in project retrofit by square.
the class RetrofitTest method requestConverterFactoryNoMatchThrows.
@Test
public void requestConverterFactoryNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingConverterFactory nonMatchingFactory = new NonMatchingConverterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory).build();
try {
retrofit.requestBodyConverter(type, annotations, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate RequestBody converter for class java.lang.String.\n" + " Tried:\n" + " * retrofit2.BuiltInConverters\n" + " * retrofit2.helpers.NonMatchingConverterFactory");
}
assertThat(nonMatchingFactory.called).isTrue();
}
use of okhttp3.RequestBody in project retrofit by square.
the class RetrofitTest method requestConverterFactoryQueried.
@Test
public void requestConverterFactoryQueried() {
Type type = String.class;
Annotation[] parameterAnnotations = new Annotation[0];
Annotation[] methodAnnotations = new Annotation[1];
Converter<?, RequestBody> expectedAdapter = mock(Converter.class);
Converter.Factory factory = mock(Converter.Factory.class);
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(factory).build();
doReturn(expectedAdapter).when(factory).requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
Converter<?, RequestBody> actualAdapter = retrofit.requestBodyConverter(type, parameterAnnotations, methodAnnotations);
assertThat(actualAdapter).isSameAs(expectedAdapter);
verify(factory).requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
verifyNoMoreInteractions(factory);
}
use of okhttp3.RequestBody in project retrofit by square.
the class RetrofitTest method requestConverterFactorySkippedNoMatchThrows.
@Test
public void requestConverterFactorySkippedNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingConverterFactory nonMatchingFactory1 = new NonMatchingConverterFactory();
NonMatchingConverterFactory nonMatchingFactory2 = new NonMatchingConverterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory1).addConverterFactory(nonMatchingFactory2).build();
try {
retrofit.nextRequestBodyConverter(nonMatchingFactory1, type, annotations, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate RequestBody converter for class java.lang.String.\n" + " Skipped:\n" + " * retrofit2.BuiltInConverters\n" + " * retrofit2.helpers.NonMatchingConverterFactory\n" + " Tried:\n" + " * retrofit2.helpers.NonMatchingConverterFactory");
}
assertThat(nonMatchingFactory1.called).isFalse();
assertThat(nonMatchingFactory2.called).isTrue();
}
use of okhttp3.RequestBody in project retrofit by square.
the class RequestBuilderTest method malformedContentTypeParameterThrows.
@Test
public void malformedContentTypeParameterThrows() {
class Example {
//
@POST("/")
Call<ResponseBody> method(@Header("Content-Type") String contentType, @Body RequestBody body) {
return null;
}
}
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
try {
buildRequest(Example.class, "hello, world!", body);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Malformed content type: hello, world!");
}
}
Aggregations