Search in sources :

Example 31 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project Just-Another-Android-App by athkalia.

the class NetworkModule method provideOkHttpClient.

@Provides
@Singleton
public static OkHttpClient provideOkHttpClient(PropertiesManager propertiesManager, HttpLoggingInterceptor httpLoggingInterceptor, List<Interceptor> networkInterceptors, BaseUrlInterceptor baseUrlInterceptor) {
    final OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
    // Logs network calls for debug builds
    okHttpBuilder.addInterceptor(httpLoggingInterceptor);
    // Adds authentication headers when required in network calls
    okHttpBuilder.addInterceptor(new AuthenticationInterceptor(propertiesManager));
    // Helps with changing base url of network calls in espresso tests to the MockWebServer base url.
    okHttpBuilder.addInterceptor(baseUrlInterceptor);
    // For release builds nothing is added, the list is empty. For debug builds Stetho interceptor is added.
    for (Interceptor networkInterceptor : networkInterceptors) {
        okHttpBuilder.addNetworkInterceptor(networkInterceptor);
    }
    return okHttpBuilder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AuthenticationInterceptor(com.example.networking.AuthenticationInterceptor) Interceptor(okhttp3.Interceptor) AuthenticationInterceptor(com.example.networking.AuthenticationInterceptor) BaseUrlInterceptor(com.example.networking.BaseUrlInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 32 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project Just-Another-Android-App by athkalia.

the class MockWebServerHelper method initMockWebServer.

public MockWebServer initMockWebServer() throws IOException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.start();
    setBaseUrlToMockWebServer(mockWebServer);
    return mockWebServer;
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer)

Example 33 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project qualitymatters by artem-zinnatullin.

the class MockWebServerRule method apply.

@Override
@NonNull
public Statement apply(@NonNull Statement base, @NonNull Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final NeedsMockWebServer needsMockWebServer = description.getAnnotation(NeedsMockWebServer.class);
            if (needsMockWebServer != null) {
                final MockWebServer mockWebServer = new MockWebServer();
                mockWebServer.start();
                TestUtils.app().applicationComponent().changeableBaseUrl().setBaseUrl(mockWebServer.url("").toString());
                if (!needsMockWebServer.setupMethod().isEmpty()) {
                    final Method setupMethod = testClassInstance.getClass().getDeclaredMethod(needsMockWebServer.setupMethod(), MockWebServer.class);
                    setupMethod.invoke(testClassInstance, mockWebServer);
                }
                // Try to evaluate the test and anyway shutdown the MockWebServer.
                try {
                    base.evaluate();
                } finally {
                    mockWebServer.shutdown();
                }
            } else {
                // No need to setup a MockWebServer, just evaluate the test.
                base.evaluate();
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) MockWebServer(okhttp3.mockwebserver.MockWebServer) Method(java.lang.reflect.Method) NonNull(android.support.annotation.NonNull)

Example 34 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project qualitymatters by artem-zinnatullin.

the class QualityMattersRestApiIntegrationTest method beforeEachTest.

@Before
public void beforeEachTest() throws IOException {
    mockWebServer = new MockWebServer();
    mockWebServer.start();
    // Change base url to the mocked
    QualityMattersIntegrationRobolectricTestRunner.qualityMattersApp().applicationComponent().changeableBaseUrl().setBaseUrl(mockWebServer.url("").toString());
    qualityMattersRestApi = QualityMattersIntegrationRobolectricTestRunner.qualityMattersApp().applicationComponent().qualityMattersApi();
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer) Before(org.junit.Before)

Example 35 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project glide by bumptech.

the class HttpUrlFetcherServerTest method setUp.

@Before
public void setUp() throws IOException {
    MockitoAnnotations.initMocks(this);
    defaultFollowRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    mockWebServer = new MockWebServer();
    mockWebServer.start();
    streamCaptor = ArgumentCaptor.forClass(InputStream.class);
}
Also used : InputStream(java.io.InputStream) MockWebServer(okhttp3.mockwebserver.MockWebServer) Before(org.junit.Before)

Aggregations

MockWebServer (okhttp3.mockwebserver.MockWebServer)48 MockResponse (okhttp3.mockwebserver.MockResponse)31 Test (org.junit.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 Before (org.junit.Before)14 IOException (java.io.IOException)9 CookieManager (java.net.CookieManager)7 HttpCookie (java.net.HttpCookie)5 Retrofit (retrofit2.Retrofit)5 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 Request (okhttp3.Request)4 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)3 Response (com.github.scribejava.core.model.Response)3 InputStream (java.io.InputStream)3 InterruptedIOException (java.io.InterruptedIOException)3 HttpURLConnection (java.net.HttpURLConnection)3 URI (java.net.URI)3 URL (java.net.URL)3 List (java.util.List)3