Here it comes another recipe I’ve learnt during the
Today’s cooking is really short and easy:
how to test your
you use to add headers to your http requests.
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
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
In this case, I send the device language via
application/vnd.railsapibase.v1 as the
to tell the server which api version and language the client wants to use.
Then, we need to add this interceptor to the
In my case I use Retrofit so I need to define its
explicitly to add it.
I create it in a
Don’t worry if you don’t use Dagger, Retrofit or
the only important part here is to
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
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:
OkHttpClientwill execute the mock call to
MockWebServer. First we create the client, adding to it our test target
HttpInterceptor. Then execute a void call to
mockWebServer.url("/"), just to send a request.
Check the request: Take the
RecordedRequestfrom the server and test that its headers have the values that the
Shutdown the server: Don’t forget it!
That’s all folks!