Last active
July 11, 2020 16:51
-
-
Save felipecsl/de84eae52172a4fdccef403f6810366c to your computer and use it in GitHub Desktop.
Revisions
-
felipecsl revised this gist
Jul 11, 2020 . 2 changed files with 9 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,13 +31,13 @@ class HttpUrlStreamHandler private constructor() : URLStreamHandler() { } 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) } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,23 +7,19 @@ 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 } } -
felipecsl revised this gist
Jul 4, 2020 . 2 changed files with 6 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()) } -
felipecsl created this gist
Jul 4, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() } } }