Search in sources :

Example 51 with RequestBody

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();
}
Also used : OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 52 with RequestBody

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();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 53 with RequestBody

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);
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 54 with RequestBody

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();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 55 with RequestBody

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!");
    }
}
Also used : Header(retrofit2.http.Header) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

RequestBody (okhttp3.RequestBody)159 Request (okhttp3.Request)129 Response (okhttp3.Response)68 IOException (java.io.IOException)63 Test (org.junit.Test)53 Call (okhttp3.Call)30 ResponseBody (okhttp3.ResponseBody)30 MultipartBody (okhttp3.MultipartBody)27 FormBody (okhttp3.FormBody)25 MediaType (okhttp3.MediaType)24 Callback (okhttp3.Callback)22 Buffer (okio.Buffer)20 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)14 HttpUrl (okhttp3.HttpUrl)11 Body (retrofit2.http.Body)11 Headers (okhttp3.Headers)10 List (java.util.List)9