Retrofit2 can't test with localhost:8080 - android

I am writing a Unit Test to test my API calls, using Retrofit2.
I have a mock of the server that I can launch locally (using localhost:8080)
I always receive a 403 error - Forbidden but it's working great with Postman
#Config(sdk = [28])
#RunWith(RobolectricTestRunner::class)
class MockServerTest {
private lateinit var result: Response<CacheResult>
private lateinit var api: FakeAPI
#Before
fun setUp() {
api = CompanySingleton.retrofit.create(FakeAPI::class.java)
}
#Test
fun getResult() {
runBlocking {
result = api.cache()
assertThat(result.isSuccessful, equalTo(true))
}
}
}
object CompanySingleton {
private val okHttpClient: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(Interceptor { chain ->
val original = chain.request()
val requestBuilder = original
.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "OAuth test")
.addHeader("x-device-id", "test")
val request = requestBuilder.build()
chain.proceed(request)
}
).build()
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("http://localhost:8080/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

in my case testing with emulators I put http://10.0.2.2:8000 to access my local host

I assume that the api is being served on your laptop/pc (and postman definitely running on it can reach it using localhost). I think you should use the local IP of your laptop (e.g. 192.168.1.10) on android. Also don't forget to open 8080 port on your firewall (or simply turn it off)

When I have to test some local service, I usually use ngrok. It's simple to run, just in terminal you type:
./ngrok http 3000

Related

How to test Retrofit API call using MockWebServer

I have a class that creates the RetrofitInstance in a very basic way, and I want to test that it is working correctly by running a dummy api against a mockedWebServer but for some reason Instead of getting a succesfull 200 response I get a 0.
fun createRetrofitInstance(baseUrl: String, client: OkHttpClient): Retrofit {
return Retrofit.Builder().baseUrl(baseUrl)
.addCallAdapterFactory(callAdapterFactory)
.addConverterFactory(converterFactory)
.client(client)
.build()
}
and I want to test it using a DummyApi
#Test
fun `should return successful response`() {
val mockedWebServer = MockWebServer()
val mockedResponse = MockResponse().setResponseCode(200)
mockedWebServer.enqueue(mockedResponse)
mockedWebServer.start()
mockedWebServer.url("/")
val retrofit = tested.createRetrofitInstance(mockedWebServer.url("/").toString(), client)
val testApi = retrofit.create(TestApi::class.java)
val actualResponseCall: Call<Any> = testApi.getTestApi()
assertEquals(200, actualResponseCall.execute().code())
mockedWebServer.shutdown()
}
DummyApi
interface TestApi {
#GET("/")
fun getTestApi() : Call<Any>
}
You should read through one of the excellent tutorials on MockWebServer out there. Too much information for just this answer. I think in this case you are just missing the setBody call.
https://medium.com/android-news/unit-test-api-calls-with-mockwebserver-d4fab11de847
val mockedResponse = MockResponse()
mockedResponse.setResponseCode(200)
mockedResponse.setBody("{}") // sample JSON

How to check if a server is reachable with retrofit in kotlin?

I have a basic retrofit setup in kotlin.
val BASE_URL: String = "http://10.0.2.2:5000/"
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
this.level = HttpLoggingInterceptor.Level.BODY
}
private val client: OkHttpClient = OkHttpClient.Builder().apply {
this.addInterceptor(interceptor)
}.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.client(client)
.build()
val service: Api by lazy {
retrofit.create(Api::class.java)
}
I want to check if the server I'm fetching my data from is running - if its not I want to fall back on the local DB for basic functionality. I tried something similar at first but there's a couple of things that are wrong with this approach. First of all the request timeout period is 10 seconds long, which is a little bit more than you'd want it to be for an app. Second, well, it doesn't really work, it'll still throw an exception if the server is offline.
fun serverReachable(): Boolean {
return try {
GlobalScope.async {
// call whatever api function here
}
true
} catch (e: Exception) {
false
}
}
Is there are quick and dirty version of checking if the server is up?

Kotlin : Okhttpclient correct way of creating single instance? OkHttpClient doesn't go through server ip check?

I'm new at android kotlin development and currently trying to solve how to correctly create a single instance of OkHttpClient for app-wide usage. I've currently sort-of* created a single instance of client and using it to communicate with the server, however currently the back-end server is not using token/userid for validation but IP check. I can log in the user no problem, but after going to another activity trying to call api, I'm being blocked access by server because apparently IP is not the same. I've used POSTMAN as well as already created a same functioning iOS app that is working with no issue. So my question is am i creating the single instance of OkHttpClient wrong? Or is OkHttpClient not suitable for this kind of ipcheck system? Should i use other library, and if yes, any suggestion and examples?
Thanks in advance
Currently i tried creating it like this :
class MyApplication: Application(){
companion object{
lateinit var client: OkHttpClient
}
override fun onCreate(){
super.onCreate()
client = OkHttpClient()
}
}
Then i created a helper class for it :
class OkHttpRequest {
private var client : OkHttpClient = MyApplication.client
fun POST(url: String, parameters: HashMap<String, String>, callback: Callback): Call {
val builder = FormBody.Builder()
val it = parameters.entries.iterator()
while (it.hasNext()) {
val pair = it.next() as Map.Entry<*, *>
builder.add(pair.key.toString(), pair.value.toString())
}
val formBody = builder.build()
val request = Request.Builder()
.url(url)
.post(formBody)
.build()
val call = client.newCall(request)
call.enqueue(callback)
return call
}
fun GET(url: String, callback: Callback): Call {
val request = Request.Builder()
.url(url)
.build()
val call = client.newCall(request)
call.enqueue(callback)
return call
}
}
Finally I'm using it like this :
val loginUrl = MyApplication.postLoginUrl
var userIdValue = user_id_textfield.text.toString()
var passwordValue = password_textfield.text.toString()
val map: HashMap<String, String> = hashMapOf("email" to userIdValue, "password" to passwordValue)
var request = OkHttpRequest()
request.POST(loginUrl, map, object : Callback {
val responseData = response.body?.string()
// do something with response Data
}
And on another activity after user log in :
val getPaidTo = MyApplication.getPaidTo
var request = OkHttpRequest()
request.GET(getPaidTo, object: Callback{
//do something with data
}
First, don't use your OkHttpClient directly in every Activity or Fragment, use DI and move all of your business logic into Repository or some source of data.
Here I will share some easy way to make REST request with Retrofit, OkHttpClient and Koin, if you want use the same:
WebServiceModule:
val webServiceModule = module {
//Create HttpLoggingInterceptor
single { createLoggingInterceptor() }
//Create OkHttpClient
single { createOkHttpClient(get()) }
//Create WebServiceApi
single { createWebServiceApi(get()) }
}
/**
* Setup a Retrofit.Builder and create a WebServiceApi instance which will hold all HTTP requests
*
* #okHttpClient Factory for HTTP calls
*/
private fun createWebServiceApi(okHttpClient: OkHttpClient): WebServiceApi {
val retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.REST_SERVICE_BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
return retrofit.create(WebServiceApi::class.java)
}
/**
* Create a OkHttpClient which is used to send HTTP requests and read their responses.
*
* #loggingInterceptor logging interceptor
*/
private fun createOkHttpClient(
loggingInterceptor: HttpLoggingInterceptor
): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.readTimeout(defaultTimeout, TimeUnit.SECONDS)
.connectTimeout(defaultTimeout, TimeUnit.SECONDS)
.build()
}
And now you can inject your WebServiceApi everywhere, but better inject it in your Repository and then use it from some ViewModel
ViewModelModule:
val viewModelModule = module {
//Create an instance of MyRepository
single { MyRepository(webServiceApi = get()) }
}
Hope this help somehow
Okay, after i check with the back-end developer, i figured out the problem wasn't the ip address(it stays the same) but that the cookie was not saved by okhttp, both POSTMan and xcode automatically save the token returned into cookie so i never noticed that was the problem. So after googling a-bit, the solution can be as easy as this:
class MyApplication : Application(){
override fun onCreate(){
val cookieJar = PersistentCookieJar(SetCookieCache(),SharedPrefsCookiePersistor(this))
client = OkHttpClient.Builder()
.cookieJar(cookieJar)
.build()
}
}
With adding persistentCookieJar to gradle.

Retrofit request throwing UnknownHostException behind VPN

So I'm having an issue that looks that it came straight out from the Twilight Zone.
Problem
I have to hit a REST API endpoint from a backend, the thing is that in order to hit that endpoint I need to go through a VPN. Otherwise the host is not reachable. On desktop everything works fine, I open Postman, hit the GET endpoint and get the response. However when I try to hit the same endpoint through my Android device Retrofit throws an UnknownHostException.
Context
The endpoint url is something like https://api.something.something.net/. I'm using dependency injection with Dagger, so I've a NetworkModule that looks like:
...
NetworkModule("https://api.something.something.net/")
...
#Module
class NetworkModule(
private val baseHost: String
) {
...
#Provides
#Named("authInterceptor")
fun providesAuthInterceptor(
#Named("authToken") authToken: String
): Interceptor {
return Interceptor { chain ->
var request = chain.request()
request = request.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer $authToken")
.build()
val response = chain.proceed(request)
}
}
...
#Provides
#Singleton
fun provideOkHttpClient(
curlInterceptor: CurlInterceptor,
#Named("authInterceptor") authInterceptor: Interceptor
): OkHttpClient {
val builder = OkHttpClient.Builder()
builder.addInterceptor(authInterceptor)
builder.addInterceptor(curlInterceptor)
return builder.build()
}
...
#Provides
#Singleton
fun provideRetrofit(okHttpClient: OkHttpClient, gson: Gson): Retrofit {
return Retrofit.Builder()
.baseUrl(baseHost)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
}
}
Then I've a bunch of Repositories which are the ones doing the request through Retrofit:
class MyApiRepositoryImpl(
val myRetrofitApi: MyRetrofitApi,
val uiScheduler: Scheduler,
val backgroundScheduler: Scheduler
) : MyApiRepository {
override fun getSomethingFromTheApi(): Observable<DataResource<List<ApiSomethingResponse>>> {
return myRetrofitApi.getResponseFromEndpoint()
.map {
if (it.isSuccessful) {
DataResource(it.body()?.list!!, ResourceType.NETWORK_SUCCESS)
} else {
throw RuntimeException("Network request failed code ${it.code()}")
}
}
.subscribeOn(backgroundScheduler)
.observeOn(uiScheduler)
.toObservable()
}
}
And this is the Retrofit's API interface:
interface MyRetrofitApi {
#GET("/v1/something/")
fun getResponseFromEndpoint(): Single<Response<ApiSomethingResponse>>
}
So, when I call this Repository method from my Interactor/UseCases it jumps straight through the onError and shows the UnknownHostException.
What I tried so far
I switched Retrofit by Volley and later by Ion, just to be sure that wasn't something related to the rest client. I got the same exception in all cases:
java.net.UnknownHostException: Unable to resolve host "api.something.something.net": No address associated with hostname
com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "api.something.something.net": No address associated with hostname
I tried every configuration possible with Retrofit and the OkHttpClient:
On OkHttpClient I tried setting the followSslRedirects to true and false. followRedirects to true and false. Set hostnameVerifier to allow any hostname to pass through. Set a SSLSocketFactory to allow any unsigned certificates to pass through.
On my Manifest I set my android:networkSecurityConfig to:
https://github.com/commonsguy/cwac-netsecurity/issues/5
I tested the App on my Android Device (Android Nougat), on Emulators with Nougat, Marshmellow and Oreo, and a Genymotion emulator with Nougat.
I tried hitting a random public endpoint (https://jsonplaceholder.typicode.com/todos/1) and It worked perfectly. So this isn't an issue with the internet connection.
I've these three permissions set on my Manifest:
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
It's super weird because I've an Interceptor set to convert all the requests into cURL requests, I copied and pasted the same request that is failing into Postman and works perfectly.
On my laptop I'm using Cisco AnyConnect, on my Android device I'm using the Cisco AnyConnect App and AFAIK on the emulators and Genymotion it should use the same network than the hosting machine.
There's a couple of websites that are only visible through the VPN and I can see them on the devices and on the emulators. But the endpoint URL is still unreachable from the App.
Couple of weird things
Yes, this gets weirder. If I hit the endpoint through the Chrome browser in my device or in the emulator I got redirected into a login page, that's because I'm not sending the authorization token. Now If I check the network responses through chrome://inspect I can get the IP of the host. Now if I change the base url of the NetworkModule by the IP and I add this line to the authorization Interceptor:
#Provides
#Named("authInterceptor")
fun providesAuthInterceptor(
#Named("authToken") authToken: String
): Interceptor {
return Interceptor { chain ->
var request = chain.request()
request = request.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer $authToken")
.build()
val response = chain.proceed(request)
response.newBuilder().code(HttpURLConnection.HTTP_SEE_OTHER).build() // <--- This one
}
}
Then I start getting:
11-13 13:56:01.084 4867-4867/com.something.myapp D/GetSomethingUseCase: javax.net.ssl.SSLPeerUnverifiedException: Hostname <BASE IP> not verified:
certificate: sha256/someShaString
DN: CN=something.server.net,OU=Title,O=Company Something\, LLC,L=Some City,ST=SomeState,C=SomeCountryCode
subjectAltNames: [someServer1.com, someServer2.com, someServer3.com, someServer4.com, andSoOn.com]
I'm not sure if this is unrelated or if it's actually one step forward to fix it.
Any tip or advice is appreciated.
Thanks,
A coworker found the issue. Basically the VPN is blocking any App except com.android.chrome.
https://www.cisco.com/c/en/us/td/docs/security/vpn_client/anyconnect/anyconnect40/administration/guide/Cisco_AnyConnect_Mobile_Administrator_Guide_4-0/mobile-anyconnect-mobile-devices.html#task_F445916BDC9649D49A98F98224D2EA7D
I think this issue may be related to ssl certificate validity. for dealing with that issue you should to set setSSLSocketFactory in HttpClient.
private SSLConnectionSocketFactory getSSLSocketFactory() {
KeyStore trustStore;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
TrustStrategy trustStrategy = new TrustStrategy() {
#Override
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
};
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy);
sslContextBuilder.useTLS();
SSLContext sslContext = sslContextBuilder.build();
SSLConnectionSocketFactory sslSocketFactory = new
SSLConnectionSocketFactory(sslContext);
return sslSocketFactory;
} catch (GeneralSecurityException | IOException e) {
System.err.println("SSL Error : " + e.getMessage());
}
return null;
}
HttpClientBuilder.create().setSSLSocketFactory(getSSLSocketFactory())
.build();

How to make MockWebServer work?

I am developing an app using the MVP architecture. I am trying to test the Interactors of my app using MockWebServer. Well, I have this test:
#RunWith(RobolectricTestRunner::class)
#Config(constants = BuildConfig::class, manifest = "src/main/AndroidManifest.xml", packageName = "br.com.simplepass.simplepassnew", sdk = intArrayOf(23))
class LoginInteractorImplTest {
lateinit var mLoginInteractor : LoginInteractor
lateinit var mServer: MockWebServer
#Before
fun setUp(){
mLoginInteractor = LoginInteractorImpl()
mServer = MockWebServer()
mServer.start()
}
#Test
fun loginTest(){
mServer.url("http://192.168.0.10:8080/login")
val testSubscriber = TestSubscriber.create<User>()
mLoginInteractor.login("31991889992", "lala").subscribe(testSubscriber)
testSubscriber.assertNoErrors()
// testSubscriber.assertCompleted()
}
#After
fun tearDown(){
mServer.shutdown()
}
}
But, when I uncomment the assertCompleted on the TestSubscriber, I always get assertionError... I know the TestSubscriber works, because I use it in other tests.
Here is my ApiCall:
#GET("login")
fun login() : Observable<User>
My NetModule:
#Module
class NetModule(val mBaseUrl: String) {
#Provides
#Singleton
fun provideHttpCache(application: Application): Cache {
val cacheSize = 10 * 1024 * 1024
return Cache(application.cacheDir, cacheSize.toLong())
}
#Provides
#Singleton
fun provideOkhttpClient(cache: Cache) : OkHttpClient {
val client = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
client.addInterceptor(interceptor)
return client.cache(cache).build()
}
#Provides
#Singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build()
}
}
And my base URL (There's no backend server... could be anything):
<string name="api_base_url">http://192.168.0.12:8080</string>
So, What am I missing? This code should be working...
Any help is welcome!
EDIT:
So, I changed the code to this:
mLoginInteractor = LoginInteractorImpl()
mServer = MockWebServer()
mServer.enqueue(MockResponse()
.setResponseCode(200)
.setBody(Gson().toJson(User(1, "991889992", "Leandro", "123"))))
mServer.start()
val client = OkHttpClient.Builder()
val cacheSize = 10 * 1024 * 1024
client.cache(Cache(application.cacheDir, cacheSize.toLong())).build()
mLoginInteractor.setRetrofit(Retrofit.Builder()
.baseUrl(mServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client.cache(Cache(application.cacheDir, cacheSize.toLong())).build())
.build())
And this:
val testSubscriber = TestSubscriber.create<User>()
mLoginInteractor.login("31991889992", "lala").subscribe(testSubscriber)
testSubscriber.assertNoErrors()
testSubscriber.assertReceivedOnNext(listOf(User(1, "991889992", "Leandro", "123")))
testSubscriber.assertCompleted()
But I still get this error:
Number of items does not match. Provided: 1 Actual: 0.
Provided values: [User(id=1, phoneNumber=991889992, name=Leandro, password=123)]
Actual values: []
There are a couple of things going on here. First, MockWebServer.url() resolves the given url against the mock server's base url, it does not set the url. If you want to set the url, you'll need to pass it to the start() method. Generally, you configure your retrofit to call the server's endpoint --
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
// Other builder methods.
.build();
Second, to get responses from the mock web server, you need to enqueue the expected responses as MockResponses. Otherwise it doesn't know what to send back. Do something like the following before making your request --
server.enqueue(new MockResponse().setBody("Success!"));
You'll need to build your response to mirror the expected response.
See the README for some more examples.

Categories

Resources