-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8261d46
commit b252da8
Showing
16 changed files
with
2,999 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.saiful.core.di | ||
|
||
import com.saiful.core.mock.* | ||
import com.saiful.core.utils.DispatcherProvider | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.* | ||
import timber.log.Timber | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object MockApiModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideCoroutineScope(dispatcherProvider: DispatcherProvider): CoroutineScope { | ||
val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
Timber.e("CoreModule -> CoroutineExceptionHandler -> ${throwable.localizedMessage}") | ||
} | ||
return CoroutineScope(SupervisorJob() + dispatcherProvider.io() + exceptionHandler) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideMockMaker(): MockMaker { | ||
return MockMaker().apply { | ||
putMap(PhotoMockApi.getMap()) | ||
} | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideMockDispatcher(mockMaker: MockMaker): MockDispatcher { | ||
return MockDispatcher(mockMaker) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideMockManager( | ||
coroutineScope: CoroutineScope, | ||
mockDispatcher: MockDispatcher | ||
): MockServerManager { | ||
return MockServerManager(coroutineScope, mockDispatcher) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.saiful.core.mock | ||
|
||
import okhttp3.mockwebserver.RecordedRequest | ||
|
||
data class MockApi( | ||
val path: String, | ||
val shouldMock: Boolean = true, | ||
val responseCode: Int = 200, | ||
val delay: Long = 500L, | ||
val findPath: (RecordedRequest) -> String = { path } | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.saiful.core.mock | ||
|
||
import okhttp3.mockwebserver.* | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
|
||
class MockDispatcher @Inject constructor( | ||
private val mockMaker: MockMaker | ||
) : Dispatcher() { | ||
override fun dispatch(request: RecordedRequest): MockResponse { | ||
return try { | ||
val apiEndPoint = request.path?.removePrefix("/") ?: "" | ||
mockMaker.findMockApi(apiEndPoint)?.let { mockApi -> | ||
MockResponse().setResponseCode(mockApi.responseCode).setBody( | ||
ResourceUtils.getJsonString("${mockApi.findPath(request)}/${mockApi.responseCode}.json") | ||
).apply { | ||
if (mockApi.delay > 0) this.setBodyDelay(mockApi.delay, TimeUnit.MILLISECONDS) | ||
} | ||
} ?: throw NullPointerException("API mocker not found!") | ||
|
||
|
||
} catch (ex: Exception) { | ||
MockResponse().setResponseCode(404) | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core/src/main/java/com/saiful/core/mock/MockInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.saiful.core.mock | ||
|
||
import com.saiful.core.components.logger.logDebug | ||
import okhttp3.* | ||
|
||
class MockInterceptor( | ||
private val baseUrl: String, | ||
private val mockMaker: MockMaker, | ||
) : Interceptor { | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
var request: Request = chain.request() | ||
val apiEndPoint = request.url.toString().replace(baseUrl, "") | ||
if (mockMaker.findMockApi(apiEndPoint)?.shouldMock == true) { | ||
val newBaseUrl = "http://localhost:8080/" | ||
val newEndPointUrl = newBaseUrl + apiEndPoint | ||
request = request.newBuilder() | ||
.url(newEndPointUrl) | ||
.build() | ||
} | ||
logDebug("MOCK", "Mocker: request -> $request") | ||
return chain.proceed(request) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.saiful.core.mock | ||
|
||
class MockMaker { | ||
|
||
private val mockApis = mutableMapOf<Regex, MockApi>() | ||
|
||
private fun getMapper() = buildMap { | ||
putAll(mockApis) | ||
} | ||
|
||
fun putMap(map: Map<Regex, MockApi>) { | ||
mockApis.putAll(map) | ||
} | ||
|
||
fun findMockApi(api: String): MockApi? { | ||
for ((regex, mockApi) in getMapper()) { | ||
if (regex.containsMatchIn(api)) { | ||
return mockApi | ||
} | ||
} | ||
return null | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core/src/main/java/com/saiful/core/mock/MockServerManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.saiful.core.mock | ||
|
||
import com.saiful.core.components.logger.logDebug | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import okhttp3.mockwebserver.MockWebServer | ||
|
||
class MockServerManager( | ||
private val coroutineScope: CoroutineScope, | ||
private val mockDispatcher: MockDispatcher | ||
) { | ||
private var mockServer: MockWebServer? = null | ||
|
||
fun startServer() { | ||
coroutineScope.launch { | ||
mockServer = MockWebServer() | ||
mockServer?.apply { | ||
dispatcher = mockDispatcher | ||
}?.start(8080) | ||
}.invokeOnCompletion { | ||
logDebug(msg = "Mock server started") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.saiful.core.mock | ||
|
||
object PhotoMockApi { | ||
|
||
private val PHOTOS = Regex("^photos\\?page=\\d+&per_page=\\d+\$") | ||
|
||
fun getMap(): Map<Regex, MockApi> = mapOf( | ||
PHOTOS to MockApi( | ||
path = "photos", | ||
findPath = { recordedRequest -> | ||
val regex = """page=(\d+)""".toRegex() | ||
when (regex.find(recordedRequest.path.toString())?.groupValues?.get(1) ?: "") { | ||
"1" -> "photos/one" | ||
"2" -> "photos/two" | ||
else -> "photos" | ||
} | ||
} | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.saiful.core.mock | ||
|
||
import okio.buffer | ||
import okio.source | ||
import java.io.IOException | ||
import java.nio.charset.StandardCharsets | ||
|
||
internal object ResourceUtils { | ||
@Throws(exceptionClasses = [IOException::class]) | ||
fun getJsonString(path : String) : String { | ||
// Load the JSON response | ||
return try { | ||
this.javaClass | ||
.classLoader | ||
?.getResourceAsStream(path) | ||
?.source() | ||
?.buffer() | ||
?.readString(StandardCharsets.UTF_8) | ||
.orEmpty() | ||
} catch (exception: IOException) { | ||
throw exception | ||
} | ||
} | ||
} |
Oops, something went wrong.