Search in sources :

Example 51 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project SpaceLaunchNow-Android by ItsCalebJones.

the class RetrofitBuilder method defaultClient.

private static OkHttpClient defaultClient() {
    OkHttpClient.Builder client = new OkHttpClient().newBuilder();
    client.connectTimeout(15, TimeUnit.SECONDS);
    client.readTimeout(15, TimeUnit.SECONDS);
    client.writeTimeout(15, TimeUnit.SECONDS);
    return client.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient)

Example 52 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project SpaceLaunchNow-Android by ItsCalebJones.

the class YouTubeAPIHelper method defaultClient.

private static OkHttpClient defaultClient(Context context) {
    File httpCacheDirectory = new File(context.getCacheDir(), "responses-youtube");
    // 1 MiB
    int cacheSize = 1024 * 1024;
    Cache cache = new Cache(httpCacheDirectory, cacheSize);
    OkHttpClient.Builder client = new OkHttpClient().newBuilder();
    client.cache(cache);
    client.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR);
    client.connectTimeout(15, TimeUnit.SECONDS);
    client.readTimeout(15, TimeUnit.SECONDS);
    client.writeTimeout(15, TimeUnit.SECONDS);
    return client.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) File(java.io.File) Cache(okhttp3.Cache)

Example 53 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project spring-framework-debug by Joker-5.

the class InvalidHttpMethodIntegrationTests method invalidHttpMethod.

@ParameterizedHttpServerTest
void invalidHttpMethod(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().method("BAZ", null).url("http://localhost:" + port + "/").build();
    try (Response response = client.newCall(request).execute()) {
        assertThat(response.body().string()).isEqualTo("BAR");
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request)

Example 54 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class OkHttpDataSourceTest method open_setsCorrectHeaders.

/**
 * This test will set HTTP default request parameters (1) in the OkHttpDataSource, (2) via
 * OkHttpDataSource.setRequestProperty() and (3) in the DataSpec instance according to the table
 * below. Values wrapped in '*' are the ones that should be set in the connection request.
 *
 * <pre>{@code
 * +---------------+-----+-----+-----+-----+-----+-----+-----+
 * |               |               Header Key                |
 * +---------------+-----+-----+-----+-----+-----+-----+-----+
 * |   Location    |  0  |  1  |  2  |  3  |  4  |  5  |  6  |
 * +---------------+-----+-----+-----+-----+-----+-----+-----+
 * | Constructor   | *Y* |  Y  |  Y  |     |  Y  |     |     |
 * | Setter        |     | *Y* |  Y  |  Y  |     | *Y* |     |
 * | DataSpec      |     |     | *Y* | *Y* | *Y* |     | *Y* |
 * +---------------+-----+-----+-----+-----+-----+-----+-----+
 * }</pre>
 */
@Test
public void open_setsCorrectHeaders() throws Exception {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse());
    String propertyFromFactory = "fromFactory";
    Map<String, String> defaultRequestProperties = new HashMap<>();
    defaultRequestProperties.put("0", propertyFromFactory);
    defaultRequestProperties.put("1", propertyFromFactory);
    defaultRequestProperties.put("2", propertyFromFactory);
    defaultRequestProperties.put("4", propertyFromFactory);
    HttpDataSource dataSource = new OkHttpDataSource.Factory(new OkHttpClient()).setDefaultRequestProperties(defaultRequestProperties).createDataSource();
    String propertyFromSetter = "fromSetter";
    dataSource.setRequestProperty("1", propertyFromSetter);
    dataSource.setRequestProperty("2", propertyFromSetter);
    dataSource.setRequestProperty("3", propertyFromSetter);
    dataSource.setRequestProperty("5", propertyFromSetter);
    String propertyFromDataSpec = "fromDataSpec";
    Map<String, String> dataSpecRequestProperties = new HashMap<>();
    dataSpecRequestProperties.put("2", propertyFromDataSpec);
    dataSpecRequestProperties.put("3", propertyFromDataSpec);
    dataSpecRequestProperties.put("4", propertyFromDataSpec);
    dataSpecRequestProperties.put("6", propertyFromDataSpec);
    DataSpec dataSpec = new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).setHttpRequestHeaders(dataSpecRequestProperties).build();
    dataSource.open(dataSpec);
    Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
    assertThat(headers.get("0")).isEqualTo(propertyFromFactory);
    assertThat(headers.get("1")).isEqualTo(propertyFromSetter);
    assertThat(headers.get("2")).isEqualTo(propertyFromDataSpec);
    assertThat(headers.get("3")).isEqualTo(propertyFromDataSpec);
    assertThat(headers.get("4")).isEqualTo(propertyFromDataSpec);
    assertThat(headers.get("5")).isEqualTo(propertyFromSetter);
    assertThat(headers.get("6")).isEqualTo(propertyFromDataSpec);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) HttpDataSource(androidx.media3.datasource.HttpDataSource) Test(org.junit.Test)

Example 55 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.

the class OkHttpDataSourceTest method open_invalidResponseCode.

@Test
public void open_invalidResponseCode() {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("failure msg"));
    HttpDataSource okHttpDataSource = new OkHttpDataSource.Factory(new OkHttpClient()).createDataSource();
    DataSpec dataSpec = new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build();
    HttpDataSource.InvalidResponseCodeException exception = assertThrows(HttpDataSource.InvalidResponseCodeException.class, () -> okHttpDataSource.open(dataSpec));
    assertThat(exception.responseCode).isEqualTo(404);
    assertThat(exception.responseBody).isEqualTo("failure msg".getBytes(Charsets.UTF_8));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) HttpDataSource(androidx.media3.datasource.HttpDataSource) Test(org.junit.Test)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)1944 Request (okhttp3.Request)1024 Response (okhttp3.Response)880 IOException (java.io.IOException)567 Test (org.junit.Test)365 Call (okhttp3.Call)290 RequestBody (okhttp3.RequestBody)222 Test (org.junit.jupiter.api.Test)145 Retrofit (retrofit2.Retrofit)138 File (java.io.File)132 HttpUrl (okhttp3.HttpUrl)131 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)128 Callback (okhttp3.Callback)117 JSONObject (org.json.JSONObject)110 ArrayList (java.util.ArrayList)106 ResponseBody (okhttp3.ResponseBody)105 Gson (com.google.gson.Gson)98 MediaType (okhttp3.MediaType)98 List (java.util.List)92 Map (java.util.Map)85