Search in sources :

Example 31 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 32 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 33 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 34 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 35 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)178 Request (okhttp3.Request)141 IOException (java.io.IOException)75 Response (okhttp3.Response)74 Test (org.junit.Test)53 ResponseBody (okhttp3.ResponseBody)32 Call (okhttp3.Call)31 MultipartBody (okhttp3.MultipartBody)28 MediaType (okhttp3.MediaType)27 FormBody (okhttp3.FormBody)25 Callback (okhttp3.Callback)23 Buffer (okio.Buffer)23 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)15 Headers (okhttp3.Headers)12 HttpUrl (okhttp3.HttpUrl)11 JSONObject (org.json.JSONObject)11 Body (retrofit2.http.Body)11