diff --git a/ddmock/src/main/java/com/dd/DDMock.kt b/ddmock/src/main/java/com/dd/DDMock.kt index c4cf099..7ed36c4 100644 --- a/ddmock/src/main/java/com/dd/DDMock.kt +++ b/ddmock/src/main/java/com/dd/DDMock.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import okhttp3.HttpUrl +import okhttp3.MediaType import okio.Buffer import java.io.IOException import java.util.regex.Pattern @@ -35,10 +36,11 @@ object DDMock { } else { val fullFilePath = "$path/$file" val key = path.replace(MOCK_FILE_DIRECTORY, "") + val extension = fullFilePath.split("/", ".").last() if (result.contains(key)) { result[key]?.files?.add(fullFilePath) } else { - result[key] = com.dd.MockEntry(key, arrayListOf(fullFilePath)) + result[key] = com.dd.MockEntry(key, arrayListOf(fullFilePath), mediaType = getMediaType(extension)) } } } @@ -63,6 +65,14 @@ object DDMock { return mockEntry ?: getRegexEntry(path) } + private fun getMediaType(extension: String): MediaType? { + when (extension) { + "json" -> return MediaType.parse(MockEntry.CONTENT_TYPE_APPLICATION_JSON) + "pdf" -> return MediaType.parse(MockEntry.CONTENT_TYPE_APPLICATION_PDF) + } + return MediaType.parse(MockEntry.CONTENT_TYPE_APPLICATION_JSON) + } + private fun getRegexEntry(path: String): MockEntry? { for (key in mockEntries.keys) { if (key.contains("{")) { diff --git a/ddmock/src/main/java/com/dd/MockEntry.kt b/ddmock/src/main/java/com/dd/MockEntry.kt index 32bf683..b1d1b3c 100644 --- a/ddmock/src/main/java/com/dd/MockEntry.kt +++ b/ddmock/src/main/java/com/dd/MockEntry.kt @@ -3,7 +3,7 @@ package com.dd import okhttp3.MediaType import java.net.HttpURLConnection -internal const val CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=utf-8" + private const val DEFAULT_MOCK_RESPONSE_DELAY_MS = 400L class MockEntry( @@ -11,7 +11,11 @@ class MockEntry( val files: ArrayList, var selectedFile: Int = 0, var statusCode: Int = HttpURLConnection.HTTP_OK, - var responseTime: Long = DEFAULT_MOCK_RESPONSE_DELAY_MS + var responseTime: Long = DEFAULT_MOCK_RESPONSE_DELAY_MS, + var mediaType: MediaType? ) { - val mediaType = MediaType.parse(CONTENT_TYPE_APPLICATION_JSON) + companion object { + const val CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=utf-8" + const val CONTENT_TYPE_APPLICATION_PDF = "application/pdf; charset=utf-8" + } } \ No newline at end of file diff --git a/ddmock/src/main/java/com/dd/MockInterceptor.kt b/ddmock/src/main/java/com/dd/MockInterceptor.kt index ab601dc..920c64d 100644 --- a/ddmock/src/main/java/com/dd/MockInterceptor.kt +++ b/ddmock/src/main/java/com/dd/MockInterceptor.kt @@ -36,7 +36,7 @@ class MockInterceptor : Interceptor { .message("MOCK") .header( "Content-Type", - if (mockEntry.mediaType != null) mockEntry.mediaType.toString() else CONTENT_TYPE_APPLICATION_JSON + if (mockEntry.mediaType != null) mockEntry.mediaType.toString() else MockEntry.CONTENT_TYPE_APPLICATION_JSON ) .request(request) .body(ResponseBody.create(mockEntry.mediaType, buffer.size(), buffer))