Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Created March 1, 2017 08:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PureKrome/55698e875e6c9028a09096c04691186f to your computer and use it in GitHub Desktop.
Save PureKrome/55698e875e6c9028a09096c04691186f to your computer and use it in GitHub Desktop.
Calling google api's in unit tests by providing fake responses
/*
This is a quick example of how to fake out the response for calling a Google API endpoint.
I'm using the QPX Express api as an example, but I'm _assuming_ this could apply accross the board
for other Google API's.
The reason you would want to do this, is so you don't need to hit the internet to get your results
(think, coding on a plane, no internet coverage, no hurting your API allowance, etc).
The main trick here is that we need to do two things:
1. Create a fake HttpMessageHandler
2. Provide this fake HttpMessageHandler in part of the BaseClientService.Initializer code when you
are setting up your stuff.
PRO TIP: I would personally inject your fake HttpMessageHandler into your main Service code. If null, then
the Google code will use the real internets to hit their api. Otherwise, it will use your
fake HttpMessageHandler instead. *Magic!*
*/
// Create a class that inherits from Google's HttpClientFactory.
// This is because we need to override the virtual method "CreateHandler" which is where all the magic happens.
// Here, we define our own faked RESPONSE object.
// NOTE: for simplicity, I'm using the library HttpClient.Helpers to simply this process/easier to read/maintain.
// ref: https://github.com/PureKrome/HttpClient.Helpers
// Disclosure: yes, that's my repo too.
public class FakeHttpClientFactory : HttpClientFactory
{
private readonly string _json;
public FakeHttpClientFactory(string json)
{
_json = json;
}
protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
{
var messageOptions = new HttpMessageOptions
{
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonResponse)
}
};
var fakeHttpMessageHandler = new FakeHttpMessageHandler(messageOptions);
return fakeHttpMessageHandler;
}
}
// Now create this faked up class and pass it to your code.
public async Task WhateverAsync()
{
// Sample response: https://developers.google.com/qpx-express/v1/json.samples/SFOLAX.out.json
var jsonResponse = File.ReadAllText("Fake Data\\SFOLAX.out.json");
// The fake response...
var fakeHttpClientFactory = new FakeHttpClientFactory(jsonResponse);
// Now lets create this QPX Express thingy...
var initializer = new BaseClientService.Initializer
{
ApiKey = "SOME API KEY",
ApplicationName = "I Love Turtles",
HttpClientFactory = fakeHttpClientFactory
};
var qpxExpressService = new QPXExpressService(initializer);
// Finally, lets do something with this service.
var options = new TripsSearchRequest
{
// snip snip snip...
}
var response = await _qpxExpressService.Trips.Search(requestOptions)
.ExecuteAsync()
.ConfigureAwait(false);
// ASSERT: the response object should be filled with the data from that json file.
}
@LindaLawton
Copy link

I forked this I will bring it up with the other guys working on the client library. I question whether its the developers responsibility to ensure that the library is parsing data correctly or if it should be the client library that contains tests like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment