Closes #6873: Support many more Content-Type in MockWebServer (UI tests) (#7001)

Ensure our MockResponse can handle different Content-Type e.g, images, to help support other tests
staging
Aaron Train 5 years ago committed by GitHub
parent 5f393bd5d4
commit e1fd53c718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,8 +12,11 @@ import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest import okhttp3.mockwebserver.RecordedRequest
import okio.Buffer
import okio.source
import org.mozilla.fenix.helpers.ext.toUri import org.mozilla.fenix.helpers.ext.toUri
import java.io.IOException import java.io.IOException
import java.io.InputStream
object MockWebServerHelper { object MockWebServerHelper {
@ -37,7 +40,7 @@ object MockWebServerHelper {
* If the dispatcher is unable to read a requested asset, it will fail the test by throwing an * If the dispatcher is unable to read a requested asset, it will fail the test by throwing an
* Exception on the main thread. * Exception on the main thread.
* *
* @sample [org.mozilla.tv.firefox.ui.BasicNavigationTest.basicNavigationTest] * @sample [org.mozilla.fenix.ui.NavigationToolbarTest.visitURLTest]
*/ */
const val HTTP_OK = 200 const val HTTP_OK = 200
const val HTTP_NOT_FOUND = 404 const val HTTP_NOT_FOUND = 404
@ -47,16 +50,43 @@ class AndroidAssetDispatcher : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse { override fun dispatch(request: RecordedRequest): MockResponse {
val assetManager = InstrumentationRegistry.getInstrumentation().context.assets val assetManager = InstrumentationRegistry.getInstrumentation().context.assets
val assetContents = try { try {
val pathNoLeadingSlash = request.path.drop(1) val pathNoLeadingSlash = request.path.drop(1)
assetManager.open(pathNoLeadingSlash).use { inputStream -> assetManager.open(pathNoLeadingSlash).use { inputStream ->
inputStream.bufferedReader().use { it.readText() } return fileToResponse(pathNoLeadingSlash, inputStream)
} }
} catch (e: IOException) { // e.g. file not found. } catch (e: IOException) { // e.g. file not found.
// We're on a background thread so we need to forward the exception to the main thread. // We're on a background thread so we need to forward the exception to the main thread.
mainThreadHandler.postAtFrontOfQueue { throw e } mainThreadHandler.postAtFrontOfQueue { throw e }
return MockResponse().setResponseCode(HTTP_NOT_FOUND) return MockResponse().setResponseCode(HTTP_NOT_FOUND)
} }
return MockResponse().setResponseCode(HTTP_OK).setBody(assetContents) }
}
@Throws(IOException::class)
private fun fileToResponse(path: String, file: InputStream): MockResponse {
return MockResponse()
.setResponseCode(HTTP_OK)
.setBody(fileToBytes(file))
.addHeader("content-type: " + contentType(path))
}
@Throws(IOException::class)
private fun fileToBytes(file: InputStream): Buffer? {
val result = Buffer()
result.writeAll(file.source())
return result
}
private fun contentType(path: String): String? {
return when {
path.endsWith(".png") -> "image/png"
path.endsWith(".jpg") -> "image/jpeg"
path.endsWith(".jpeg") -> "image/jpeg"
path.endsWith(".gif") -> "image/gif"
path.endsWith(".svg") -> "image/svg+xml"
path.endsWith(".html") -> "text/html; charset=utf-8"
path.endsWith(".txt") -> "text/plain; charset=utf-8"
else -> "application/octet-stream"
} }
} }

Loading…
Cancel
Save