Android Http Interceptor testing recipe
Here it comes another recipe I’ve learnt during the
android-base development!
Today’s cooking is really short and easy:
how to test your okhttp3.Interceptor
you use to add headers to your http requests.
Basic Ingredients
Method
1. Create your Android client app
The basic ingredient is an Android client application that retrieves data from an api server. Usually, we modify the headers of the http requests made to that api. It’s a really common scenario, as common as not testing the headers sent. Let’s fix that!
In this particular case I use a Square’s client http library: Retrofit. But it’s also ok if you just use OkHttp, because it’s the really required library for this recipe (Retrofit depends on it), or even another http library you prefer (you’ll just need to make some adaptations).
2. Add an HttpInterceptor
The main ingredient of this recipe is the Interceptor
.
This class is added to the OkHttpClient
and it…
…observes, modifies, and potentially short-circuits requests going out and the corresponding requests coming back in. Typically interceptors will be used to add, remove, or transform headers on the request or response.
I use it in android-base to add two headers to all my api calls. Setup the interception system is very easy.
You must already have the api client system developed.
Here just take a look at the HttpInterceptor.java
example:
In this case, I send the device language via Accept-Language
header
and also application/vnd.railsapibase.v1
as the Accept
header
to tell the server which api version and language the client wants to use.
Then, we need to add this interceptor to the OkHttpClient
.
In my case I use Retrofit so I need to define its OkHttpClient
explicitly to add it.
I create it in a
Dagger 2
module:
Don’t worry if you don’t use Dagger, Retrofit or
RxJava,
the only important part here is to .addInterceptor(new HttpInterceptor())
to your OkHttpClient
.
Ok! By this time, you must be able to receive the headers on your server requests. Test it manually a little bit to be sure you have included correctly the interceptor and let’s keep cooking…
3. Test it
As I’ve said, this recipe is really short. We only need to add our key ingredient, the test, and that’s it!
Note: Remember to add
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
to your build.gradle
.
The test is composed by 4 parts:
-
Create the server: MockWebServer is an awesome library that let’s you mock a server and test your requests, responses, reproduce delays, errors… Here we will use a tiny part of its potential, but it’s enough for us. Just create the
MockWebServer
, start it and enqueue a void response. -
Create the client:
OkHttpClient
will execute the mock call toMockWebServer
. First we create the client, adding to it our test targetHttpInterceptor
. Then execute a void call tomockWebServer.url("/")
, just to send a request. -
Check the request: Take the
RecordedRequest
from the server and test that its headers have the values that theInterceptor
put there. -
Shutdown the server: Don’t forget it!
That’s all folks!