Skip to content

Commit

Permalink
Merge pull request #5 from sawicky/master
Browse files Browse the repository at this point in the history
Added ability to create mock PDF files (or any other files)
  • Loading branch information
nattb8 authored Jul 25, 2020
2 parents f35b489 + 2b9bfac commit c886662
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
12 changes: 11 additions & 1 deletion ddmock/src/main/java/com/dd/DDMock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
}
Expand All @@ -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("{")) {
Expand Down
10 changes: 7 additions & 3 deletions ddmock/src/main/java/com/dd/MockEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ 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(
val path: String,
val files: ArrayList<String>,
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"
}
}
2 changes: 1 addition & 1 deletion ddmock/src/main/java/com/dd/MockInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit c886662

Please sign in to comment.