Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Last active July 11, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipecsl/de84eae52172a4fdccef403f6810366c to your computer and use it in GitHub Desktop.
Save felipecsl/de84eae52172a4fdccef403f6810366c to your computer and use it in GitHub Desktop.
Kotlin snippets for return a canned response from a resource file whenever a URL is request is fired
// Based on https://stackoverflow.com/questions/565535/mocking-a-url-in-java
package com.example
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
class FakeResourceHttpURLConnection(
url: URL,
private val resourceFileName: String
) : HttpURLConnection(url) {
override fun usingProxy(): Boolean {
return false
}
override fun connect() {
}
override fun disconnect() {
}
override fun getInputStream(): InputStream {
return resourceToFile(resourceFileName).inputStream()
}
/** Returns resource file contents instead of hittin the network for the provided URL */
private inline fun <reified T> T.resourceToFile(resourceName: String) =
File(T::class.java.classLoader.getResource(resourceName)!!.toURI())
}
package com.example
import org.mockito.BDDMockito.given
import org.mockito.Mockito
import java.io.IOException
import java.net.URL
import java.net.URLConnection
import java.net.URLStreamHandler
import java.net.URLStreamHandlerFactory
/**
* [URLStreamHandler] that allows us to control the [URLConnections][URLConnection] that are returned
* by [URLs][URL] in the code under test.
* Call [addFakeResponse] to return a canned responce from the provided resource file for the
* provided [URL].
*/
class HttpUrlStreamHandler private constructor() : URLStreamHandler() {
private var connections: MutableMap<URL, URLConnection> = mutableMapOf()
@Throws(IOException::class) override fun openConnection(url: URL): URLConnection? {
return connections[url]
}
fun resetConnections() {
connections.clear()
}
fun addFakeResponse(url: URL, resource: String): HttpUrlStreamHandler {
connections[url] = FakeResourceHttpURLConnection(url, resource)
return this
}
companion object {
private val urlStreamHandlerFactory = Mockito.mock(URLStreamHandlerFactory::class.java)
val INSTANCE = HttpUrlStreamHandler()
init {
URL.setURLStreamHandlerFactory(urlStreamHandlerFactory)
given(urlStreamHandlerFactory.createURLStreamHandler("https")).willReturn(INSTANCE)
given(urlStreamHandlerFactory.createURLStreamHandler("http")).willReturn(INSTANCE)
}
}
}
package com.example
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import java.net.URL
class SampleTest {
@Before fun setUp() {
httpUrlStreamHandler.resetConnections()
}
@Test fun `return canned URL response from resource`() {
val url = URL("https://some.url.that.com/want/to/mock.html")
httpUrlStreamHandler.addFakeResponse(url, "my_resource_file.json")
val stream = url.openConnection() // returns contents of my_resource_file.json
stream.use {
// use the stream :)
}
}
companion object {
private val httpUrlStreamHandler: HttpUrlStreamHandler = HttpUrlStreamHandler.INSTANCE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment