Forked from LindaLawton/gist:b576d21924139902b7163d0441636a20
Created
March 24, 2017 15:03
-
-
Save psxvoid/370a9da3cd105d88211dd886ad933dd4 to your computer and use it in GitHub Desktop.
Calling google api's in unit tests by providing fake responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment