From db1d98817a64d5131d0027682dc42b31faf1ef1b Mon Sep 17 00:00:00 2001 From: David Sawick Date: Sat, 25 Jul 2020 02:13:53 +1000 Subject: [PATCH 1/2] Added PDF content type --- ddmock/src/main/java/com/dd/DDMock.kt | 13 ++++++++++++- ddmock/src/main/java/com/dd/MockEntry.kt | 9 +++++++-- ddmock/src/main/java/com/dd/MockInterceptor.kt | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ddmock/src/main/java/com/dd/DDMock.kt b/ddmock/src/main/java/com/dd/DDMock.kt index c4cf099..61e043e 100644 --- a/ddmock/src/main/java/com/dd/DDMock.kt +++ b/ddmock/src/main/java/com/dd/DDMock.kt @@ -2,10 +2,12 @@ package com.dd import android.app.Application import android.content.res.AssetManager +import android.util.Log 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 +37,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), mediaType2 = getMediaType(extension)) } } } @@ -63,6 +66,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..3802bbe 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,12 @@ 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 mediaType2: MediaType? ) { + companion object { + const val CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=utf-8" + const val CONTENT_TYPE_APPLICATION_PDF = "application/pdf; charset=utf-8" + } val mediaType = MediaType.parse(CONTENT_TYPE_APPLICATION_JSON) } \ 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..970f03f 100644 --- a/ddmock/src/main/java/com/dd/MockInterceptor.kt +++ b/ddmock/src/main/java/com/dd/MockInterceptor.kt @@ -36,10 +36,10 @@ class MockInterceptor : Interceptor { .message("MOCK") .header( "Content-Type", - if (mockEntry.mediaType != null) mockEntry.mediaType.toString() else CONTENT_TYPE_APPLICATION_JSON + if (mockEntry.mediaType2 != null) mockEntry.mediaType2.toString() else MockEntry.CONTENT_TYPE_APPLICATION_JSON ) .request(request) - .body(ResponseBody.create(mockEntry.mediaType, buffer.size(), buffer)) + .body(ResponseBody.create(mockEntry.mediaType2, buffer.size(), buffer)) .build() } } \ No newline at end of file From 2b9bfac915f1c360015b528141fe919734345774 Mon Sep 17 00:00:00 2001 From: David Sawick Date: Sat, 25 Jul 2020 03:59:49 +1000 Subject: [PATCH 2/2] [FEAT] DDMock now allows returning of any content type by filename extension (if you add the content type key to MockEntry INIT ) --- ddmock/src/main/java/com/dd/DDMock.kt | 3 +-- ddmock/src/main/java/com/dd/MockEntry.kt | 3 +-- ddmock/src/main/java/com/dd/MockInterceptor.kt | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/ddmock/src/main/java/com/dd/DDMock.kt b/ddmock/src/main/java/com/dd/DDMock.kt index 61e043e..7ed36c4 100644 --- a/ddmock/src/main/java/com/dd/DDMock.kt +++ b/ddmock/src/main/java/com/dd/DDMock.kt @@ -2,7 +2,6 @@ package com.dd import android.app.Application import android.content.res.AssetManager -import android.util.Log import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -41,7 +40,7 @@ object DDMock { if (result.contains(key)) { result[key]?.files?.add(fullFilePath) } else { - result[key] = com.dd.MockEntry(key, arrayListOf(fullFilePath), mediaType2 = getMediaType(extension)) + result[key] = com.dd.MockEntry(key, arrayListOf(fullFilePath), mediaType = getMediaType(extension)) } } } diff --git a/ddmock/src/main/java/com/dd/MockEntry.kt b/ddmock/src/main/java/com/dd/MockEntry.kt index 3802bbe..b1d1b3c 100644 --- a/ddmock/src/main/java/com/dd/MockEntry.kt +++ b/ddmock/src/main/java/com/dd/MockEntry.kt @@ -12,11 +12,10 @@ class MockEntry( var selectedFile: Int = 0, var statusCode: Int = HttpURLConnection.HTTP_OK, var responseTime: Long = DEFAULT_MOCK_RESPONSE_DELAY_MS, - var mediaType2: MediaType? + var mediaType: MediaType? ) { companion object { const val CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=utf-8" const val CONTENT_TYPE_APPLICATION_PDF = "application/pdf; charset=utf-8" } - val mediaType = MediaType.parse(CONTENT_TYPE_APPLICATION_JSON) } \ 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 970f03f..920c64d 100644 --- a/ddmock/src/main/java/com/dd/MockInterceptor.kt +++ b/ddmock/src/main/java/com/dd/MockInterceptor.kt @@ -36,10 +36,10 @@ class MockInterceptor : Interceptor { .message("MOCK") .header( "Content-Type", - if (mockEntry.mediaType2 != null) mockEntry.mediaType2.toString() else MockEntry.CONTENT_TYPE_APPLICATION_JSON + if (mockEntry.mediaType != null) mockEntry.mediaType.toString() else MockEntry.CONTENT_TYPE_APPLICATION_JSON ) .request(request) - .body(ResponseBody.create(mockEntry.mediaType2, buffer.size(), buffer)) + .body(ResponseBody.create(mockEntry.mediaType, buffer.size(), buffer)) .build() } } \ No newline at end of file