Created
June 15, 2023 20:57
-
-
Save tugceaktepe/c744e3314ec2b7d46fb3721423efddac to your computer and use it in GitHub Desktop.
MockWebServerDispatcher to match path while making HTTP call
This file contains 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 characters
import okhttp3.mockwebserver.Dispatcher | |
import okhttp3.mockwebserver.MockResponse | |
import okhttp3.mockwebserver.RecordedRequest | |
class MockWebServerDispatcher { | |
internal inner class RequestDispatcher : Dispatcher() { | |
override fun dispatch(request: RecordedRequest): MockResponse { | |
return when (request.path) { | |
"/movie/top_rated?api_key=${API_KEY}&language=en-US&page=1" -> | |
MockResponse().setResponseCode(200) | |
.setBody(FileReader.readStringFromFile("success_response.json")) | |
else -> MockResponse().setResponseCode(400) | |
} | |
} | |
} | |
internal inner class ErrorDispatcher : Dispatcher() { | |
override fun dispatch(request: RecordedRequest): MockResponse { | |
return MockResponse().setResponseCode(400) | |
.setBody(Constants.ERROR_MESSAGE) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment