Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Last active July 11, 2020 16:51
Show Gist options
  • Select an option

  • Save felipecsl/de84eae52172a4fdccef403f6810366c to your computer and use it in GitHub Desktop.

Select an option

Save felipecsl/de84eae52172a4fdccef403f6810366c to your computer and use it in GitHub Desktop.

Revisions

  1. felipecsl revised this gist Jul 11, 2020. 2 changed files with 9 additions and 13 deletions.
    12 changes: 6 additions & 6 deletions HttpUrlStreamHandler.kt
    Original file line number Diff line number Diff line change
    @@ -31,13 +31,13 @@ class HttpUrlStreamHandler private constructor() : URLStreamHandler() {
    }

    companion object {
    fun init(): HttpUrlStreamHandler {
    val urlStreamHandlerFactory = Mockito.mock(URLStreamHandlerFactory::class.java)
    private val urlStreamHandlerFactory = Mockito.mock(URLStreamHandlerFactory::class.java)
    val INSTANCE = HttpUrlStreamHandler()

    init {
    URL.setURLStreamHandlerFactory(urlStreamHandlerFactory)
    val httpUrlStreamHandler = HttpUrlStreamHandler()
    given(urlStreamHandlerFactory.createURLStreamHandler("https")).willReturn(httpUrlStreamHandler)
    given(urlStreamHandlerFactory.createURLStreamHandler("http")).willReturn(httpUrlStreamHandler)
    return httpUrlStreamHandler
    given(urlStreamHandlerFactory.createURLStreamHandler("https")).willReturn(INSTANCE)
    given(urlStreamHandlerFactory.createURLStreamHandler("http")).willReturn(INSTANCE)
    }
    }
    }
    10 changes: 3 additions & 7 deletions SampleTest.kt
    Original file line number Diff line number Diff line change
    @@ -7,23 +7,19 @@ import java.net.URL

    class SampleTest {
    @Before fun setUp() {
    httpUrlStreamHandler?.resetConnections()
    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")
    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 var httpUrlStreamHandler: HttpUrlStreamHandler? = null

    @BeforeClass @JvmStatic fun setupURLStreamHandlerFactory() {
    httpUrlStreamHandler = HttpUrlStreamHandler.init()
    }
    private val httpUrlStreamHandler: HttpUrlStreamHandler = HttpUrlStreamHandler.INSTANCE
    }
    }
  2. felipecsl revised this gist Jul 4, 2020. 2 changed files with 6 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions CannedResponseFromResource.kt
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    // Based on https://stackoverflow.com/questions/565535/mocking-a-url-in-java

    package com.example

    import java.io.File

    /** Returns resource file contents instead of hittin the network for the provided URL */
    inline fun <reified T> T.resourceToFile(resourceName: String) =
    File(T::class.java.classLoader.getResource(resourceName)!!.toURI())
    6 changes: 6 additions & 0 deletions FakeResourceHttpURLConnection.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Based on https://stackoverflow.com/questions/565535/mocking-a-url-in-java

    package com.example

    import java.io.InputStream
    @@ -21,4 +23,8 @@ class FakeResourceHttpURLConnection(
    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())
    }
  3. felipecsl created this gist Jul 4, 2020.
    9 changes: 9 additions & 0 deletions CannedResponseFromResource.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // Based on https://stackoverflow.com/questions/565535/mocking-a-url-in-java

    package com.example

    import java.io.File

    /** Returns resource file contents instead of hittin the network for the provided URL */
    inline fun <reified T> T.resourceToFile(resourceName: String) =
    File(T::class.java.classLoader.getResource(resourceName)!!.toURI())
    24 changes: 24 additions & 0 deletions FakeResourceHttpURLConnection.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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()
    }
    }
    43 changes: 43 additions & 0 deletions HttpUrlStreamHandler.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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 {
    fun init(): HttpUrlStreamHandler {
    val urlStreamHandlerFactory = Mockito.mock(URLStreamHandlerFactory::class.java)
    URL.setURLStreamHandlerFactory(urlStreamHandlerFactory)
    val httpUrlStreamHandler = HttpUrlStreamHandler()
    given(urlStreamHandlerFactory.createURLStreamHandler("https")).willReturn(httpUrlStreamHandler)
    given(urlStreamHandlerFactory.createURLStreamHandler("http")).willReturn(httpUrlStreamHandler)
    return httpUrlStreamHandler
    }
    }
    }
    29 changes: 29 additions & 0 deletions SampleTest.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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 var httpUrlStreamHandler: HttpUrlStreamHandler? = null

    @BeforeClass @JvmStatic fun setupURLStreamHandlerFactory() {
    httpUrlStreamHandler = HttpUrlStreamHandler.init()
    }
    }
    }