Last active
August 29, 2015 14:07
-
-
Save davidbreyer/64b9fd078785f0cd7422 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class FakeResponseHandler : DelegatingHandler | |
| { | |
| private readonly Dictionary<Uri, HttpResponseMessage> _FakeResponses = new Dictionary<Uri, HttpResponseMessage>(); | |
| public void AddFakeResponse(Uri uri, HttpResponseMessage responseMessage) | |
| { | |
| _FakeResponses.Add(uri, responseMessage); | |
| } | |
| protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) | |
| { | |
| if (_FakeResponses.ContainsKey(request.RequestUri)) | |
| { | |
| return Task.FromResult(_FakeResponses[request.RequestUri]); | |
| } | |
| else | |
| { | |
| return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = request }); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| [TestClass] | |
| public class UnitTest1 | |
| { | |
| public UnityContainer Container { get; set; } | |
| [TestInitialize] | |
| public void Setup() | |
| { | |
| Container = new UnityContainer(); | |
| var fakeResponseHandler = new FakeResponseHandler(); | |
| fakeResponseHandler.AddFakeResponse( | |
| new Uri("http://example.org/api/restserviceexample/1"), | |
| new HttpResponseMessage(HttpStatusCode.OK) | |
| { | |
| Content = new ObjectContent<ExampleDto>(new ExampleDto | |
| { | |
| Id = 1, | |
| Name = "Printer" | |
| }, new JsonMediaTypeFormatter()) | |
| }); | |
| Container.RegisterType<Proxy, Proxy>(); | |
| Container.RegisterType<HttpClient>( | |
| new InjectionFactory(x => | |
| new HttpClient(fakeResponseHandler) { BaseAddress = new Uri("http://example.org/") } | |
| ) | |
| ); | |
| } | |
| [TestMethod] | |
| public void TestProxy1() | |
| { | |
| var proxy = Container.Resolve<Proxy>(); | |
| var response = proxy.GetExample(1).Result; | |
| Assert.IsNotNull(response); | |
| Assert.AreEqual("Printer", response.Name); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment