Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network mocking improvements #507

Merged
merged 1 commit into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import com.jraska.github.client.core.android.BaseApp
import com.jraska.github.client.core.android.ServiceModel
import com.jraska.github.client.http.ReplayHttpModule
import com.jraska.github.client.http.FakeHttpModule
import com.jraska.github.client.users.test.DeepLinkRecordingComponent
import com.jraska.github.client.users.test.FakeDeepLinkRecordingModule
import dagger.BindsInstance
Expand Down Expand Up @@ -36,7 +36,7 @@ class TestUITestApp : BaseApp() {
modules = [
SharedModules::class,
FakeCoreModule::class,
ReplayHttpModule::class,
FakeHttpModule::class,
FakeDeepLinkRecordingModule::class
]
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jraska.github.client.http

import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.OkHttpClient
Expand All @@ -17,20 +16,22 @@ import java.io.File
object HttpTest {
private val DEFAULT_BASE_URL = "https://api.github.com".toHttpUrl()

fun retrofit(baseUrl: HttpUrl = DEFAULT_BASE_URL): Retrofit {
fun retrofit(mockWebServer: MockWebServer? = null): Retrofit {
return Retrofit.Builder()
.baseUrl(baseUrl)
.client(
OkHttpClient.Builder()
.also { if (baseUrl == DEFAULT_BASE_URL) it.addInterceptor(MockWebServerInterceptor) } // TODO: 30/4/21 Hack - unification of network mocking needed
.addInterceptor(HttpLoggingInterceptor { println(it) }.apply {
level = HttpLoggingInterceptor.Level.BASIC
}).build()
)
.baseUrl(mockWebServer?.url("/") ?: DEFAULT_BASE_URL)
.client(client(mockWebServer))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.createSynchronous())
.build()
}

private fun client(mockWebServer: MockWebServer?): OkHttpClient {
return OkHttpClient.Builder()
.also { if (mockWebServer == null) it.addInterceptor(MockWebServerInterceptor) }
.addInterceptor(HttpLoggingInterceptor { println(it) }.apply {
level = HttpLoggingInterceptor.Level.BASIC
}).build()
}
}

fun MockWebServer.enqueue(path: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.io.File
import javax.inject.Singleton

@Module
class HttpModule {
object HttpModule {
@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class GitHubApiRepoRepositoryTest {
assertThat(repoDetail).usingRecursiveComparison().isEqualTo(expectedRepoDetail())
}

private fun repoGitHubApi() = HttpTest.retrofit(mockWebServer.url("/")).create(RepoGitHubApi::class.java)
private fun repoGitHubApi() = HttpTest.retrofit(mockWebServer).create(RepoGitHubApi::class.java)

companion object {
fun expectedRepoDetail(): RepoDetail {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.jraska.github.client.FakeWebLinkModule
import com.jraska.github.client.android.test.FakeAndroidCoreModule
import com.jraska.github.client.core.android.AppBaseComponent
import com.jraska.github.client.core.android.CoreAndroidModule
import com.jraska.github.client.http.FakeHttpModule
import com.jraska.github.client.http.HttpTest
import com.jraska.github.client.users.UsersModule
import dagger.BindsInstance
Expand All @@ -32,11 +33,3 @@ interface TestUsersComponent : AppBaseComponent, DeepLinkRecordingComponent {
fun create(@BindsInstance context: Context): TestUsersComponent
}
}

// TODO: 21/04/2021 Unify the network mocking with app to have only one fake http module
@Module
class FakeHttpModule {
@Provides
@Singleton
fun provideRetrofit() = HttpTest.retrofit()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GitHubApiUsersRepositoryTest {

@Before
fun setUp() {
repository = GitHubApiUsersRepository(HttpTest.retrofit(mockWebServer.url("/")).create(GitHubUsersApi::class.java), Fakes.trampoline())
repository = GitHubApiUsersRepository(HttpTest.retrofit(mockWebServer).create(GitHubUsersApi::class.java), Fakes.trampoline())
}

@Test
Expand Down