Search code examples
androidretrofitretrofit2okhttpmockwebserver

OkHttp MockWebServer with dynamic URLs using Retrofit


My app uses dynamic URLs to make web-service calls (on Android). baseUrl is set as empty and we pass Retrofit2 @Url parameters in the service interface:

public interface UserService {  
    @GET
    public Call<ResponseBody> profilePicture(@Url String url);
}

We don't know the host/domain in advance, so MockWebServer is not able to intercept the requests. The call to fetch the initial list of dynamic URLs is made in different screens. One idea is to create a new flavor providing a local data source for URLs to be used, which is my fallback plan.

I am curious if MockWebServer has any other methods to help test such cases and can be limited to test code.


Solution

  • You could use an OkHttp interceptor to rewrite the hostname and port?