Using
Fiddler Core
web proxy
5 minute presentation by Tim
Why?
avoid hitting live APIs
control API responses
rerunnable unit tests
test edge cases / unexpected responses
no need to change production code
no need to abstract an interface for dependency injection
Fiddler
http://www.fiddler2.com/fiddler2/
"Fiddler...":
is a Web Debugging Proxy which logs HTTP(S) traffic;
allows you to "fiddle" with incoming or outgoing data;
can be extended using any .NET language;
is freeware;
can debug traffic from IE, Chrome, Safari, Firefox, Opera, Windows Phone, iPod/iPad, ..., etc.
Fiddler Core dlls
http://www.fiddler2.com/fiddler/Core/
MockWebProxyHelper
https://gist.github.com/4079321
public static class MockWebProxyHelper
{
public static void SetUp() {...}
public static void TearDown() {...}
public class Response
{
public string Header { get; set; } // default "HTTP/1.1 200 OK"
public string Body { get; set; } // default ""
}
public static Func<HttpMethods, string, Response> GetMockResponse
= delegate { return new Response(); };
}
Set up & Tear down
[TestFixture]
public class StockCheckClientBackgroundWorkerTests
{
[TestFixtureSetUp]
public void StartFiddlerProxy()
{
MockWebProxyHelper.SetUp();
}
[TestFixtureTearDown]
public void StopFiddlerProxy()
{
MockWebProxyHelper.TearDown();
}Mock response
[Test]
public void TestMockClientCall()
{
MockWebProxyHelper.GetMockResponse = (httpMethod, url) =>
new MockWebProxyHelper.Response(body:
@"{'productCount':1,
'products':[
{'productId':'1012881',
'categoryId':
'CAT13617200330',
'availableStock':'23'}
]}");
// dummy key - should work anyways, cos it's a mock!
var client = new StockCheckClient {AuthKey = "dummy-auth-key"};
List<Product> inStockProducts =
client.GetCategoryItemsInStock("CAT13617200330");
Assert.AreEqual(1, inStockProducts.Count);
Assert.AreEqual(23, inStockProducts[0].AvailableStock);
}
Possible uses
- Dummy response files
- Return actual data
- Fake "chaos monkey"
- more?
Dummy response files
eg http://myapi.com/products?categoryId=CAT13617200330
filename "GET myapi_com_products_categoryId_CAT13617200330.txt":
HTTP/1.1 200 OK
{'productCount':1,'products':[{'productId':'1012881','categoryId':'CAT13617200330','availableStock':'23'}]}
[TestFixtureSetUp]
public void StartFiddlerProxy()
{
MockWebProxyHelper.SetUp();
MockWebProxyHelper.GetMockResponse = (httpMethod, url) =>
{
var file = url.Replace("http://", "")
.Replace('/', '_').Replace('?', '_').Replace('=', '_');
var lines = File.ReadAllLines(httpMethod + " " + file +".txt");
return new MockWebProxyHelper.Response(
header: lines.First(),
body: String.Join(Environment.NewLine, lines.Skip(1)));
}
}
Return actual data
MockWebProxyHelper.GetMockResponse =
(httpMethod, url) =>
{
// eg http://myapi.com/products?categoryId=CAT13617200330
var categoryId = url.Split('=').Last();
var products =
productsRepository.Where(x => x.CategoryId == categoryId);
var body = JsonConvert.SerializeObject(responseObject));
return new MockWebProxyHelper.Response(body: body);
};
Fake "chaos monkey"
Return whole gamut of HTTP status codes, to ensure your client code handles errors correctly
// TODO:
Record mode
If a dummy response doesn't exist, run a real request and record the response as a dummy for next time.
MOAR?
The End.
using fiddler core web proxy
By tim_iles
using fiddler core web proxy
- 2,470