diff --git a/native/kotlin/api/kotlin/src/integrationTest/kotlin/MediaEndpointTest.kt b/native/kotlin/api/kotlin/src/integrationTest/kotlin/MediaEndpointTest.kt index 5ac672aa..8b77edf6 100644 --- a/native/kotlin/api/kotlin/src/integrationTest/kotlin/MediaEndpointTest.kt +++ b/native/kotlin/api/kotlin/src/integrationTest/kotlin/MediaEndpointTest.kt @@ -6,13 +6,15 @@ import org.junit.jupiter.api.Test import uniffi.wp_api.MediaCreateParams import uniffi.wp_api.MediaListParams import uniffi.wp_api.SparseMediaFieldWithEditContext +import uniffi.wp_api.WpAuthenticationProvider +import java.io.File import kotlin.test.assertEquals import kotlin.test.assertNotNull private const val MEDIA_ID_611: Long = 611 class MediaEndpointTest { - private val client = defaultApiClient() + private val client = mediaApiClient() @Test fun testMediaListRequest() = runTest { @@ -74,4 +76,29 @@ class MediaEndpointTest { assertEquals(title, response.title.rendered) restoreTestServer() } + + fun mediaApiClient(): WpApiClient { + val testCredentials = TestCredentials.INSTANCE + val authProvider = WpAuthenticationProvider.staticWithUsernameAndPassword( + username = testCredentials.adminUsername, password = testCredentials.adminPassword + ) + val requestExecutor = WpRequestExecutor( + fileResolver = FileResolverMock() + ) + return WpApiClient( + wpOrgSiteApiRootUrl = testCredentials.apiRootUrl, + authProvider = authProvider, + requestExecutor = requestExecutor + ) + } + + class FileResolverMock: FileResolver { + // in order to properly resolve the file from the test assets, we need to do it in the following way + override fun getFile(path: String): File? = + WpAuthenticationProvider::class.java.classLoader?.getResource(path)?.file?.let { + File( + it + ) + } + } } diff --git a/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/DefaultFileResolver.kt b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/DefaultFileResolver.kt new file mode 100644 index 00000000..7f9a072c --- /dev/null +++ b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/DefaultFileResolver.kt @@ -0,0 +1,7 @@ +package rs.wordpress.api.kotlin + +import java.io.File + +class DefaultFileResolver : FileResolver { + override fun getFile(path: String): File? = File(path) +} diff --git a/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/FileResolver.kt b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/FileResolver.kt new file mode 100644 index 00000000..9569d3ee --- /dev/null +++ b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/FileResolver.kt @@ -0,0 +1,7 @@ +package rs.wordpress.api.kotlin + +import java.io.File + +interface FileResolver { + fun getFile(path: String): File? +} diff --git a/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt index dcc6462b..474fc2ed 100644 --- a/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt +++ b/native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt @@ -32,7 +32,8 @@ const val USER_AGENT_HEADER_NAME = "User-Agent" class WpRequestExecutor( private val httpClient: WpHttpClient = WpHttpClient.DefaultHttpClient(), - private val dispatcher: CoroutineDispatcher = Dispatchers.IO + private val dispatcher: CoroutineDispatcher = Dispatchers.IO, + private val fileResolver: FileResolver = DefaultFileResolver() ) : RequestExecutor { override suspend fun execute(request: WpNetworkRequest): WpNetworkResponse = withContext(dispatcher) { @@ -88,13 +89,10 @@ class WpRequestExecutor( mediaUploadRequest.mediaParams().forEach { (k, v) -> multipartBodyBuilder.addFormDataPart(k, v) } - val filePath = mediaUploadRequest.filePath() - val file = - WpRequestExecutor::class.java.classLoader?.getResource(filePath)?.file?.let { - File( - it - ) - } ?: throw MediaUploadRequestExecutionException.MediaFileNotFound(filePath) + val file = fileResolver.getFile(mediaUploadRequest.filePath()) + if (file == null || !file.canBeUploaded()) { + throw MediaUploadRequestExecutionException.MediaFileNotFound(mediaUploadRequest.filePath()) + } multipartBodyBuilder.addFormDataPart( name = "file", filename = file.name, @@ -124,6 +122,8 @@ class WpRequestExecutor( override suspend fun sleep(millis: ULong) { delay(millis.toLong()) } + + private fun File.canBeUploaded() = exists() && isFile && canRead() } private fun RequestExecutionErrorReason.Companion.unknownHost(e: UnknownHostException) =