Skip to content

Instantly share code, notes, and snippets.

Avatar

Kieron McIntyre digbyswift

View GitHub Profile
@digbyswift
digbyswift / unit-tests-httpclient-mock.cs
Created Mar 29, 2018
Create a mock HttpMessageHandler
View unit-tests-httpclient-mock.cs
public class MockHttpMessageHandler : HttpMessageHandler
{
public int CalledTimes { get; private set; }
public HttpResponseMessage Response { get; set; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
CalledTimes++;
return Task.FromResult(Response);
@digbyswift
digbyswift / unit-tests-httpclient-moq.cs
Last active Mar 29, 2018
Configure a HttpClient request using Moq
View unit-tests-httpclient-moq.cs
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.Is<HttpRequestMessage>(x => x.RequestUri == testUri), ItExpr.IsAny<CancellationToken>())
.Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(testJson) }));
// Then ...
new HttpClient(mockHttpMessageHandler.Object)
@digbyswift
digbyswift / unit-test-httpclient-moq-verify.cs
Last active Mar 29, 2018
Verify a HttpClient request using Moq
View unit-test-httpclient-moq-verify.cs
mockHttpMessageHandler
.Protected()
.Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(x => x.RequestUri == testUri),
ItExpr.IsAny<CancellationToken>()
);
View elmah-custom-httpmodule
public class AirbrakeErrorLogModule : ErrorLogModule
{
protected override void LogException(Exception e, HttpContext context)
{
if (e == null)
throw new ArgumentNullException("e");
var args = new ExceptionFilterEventArgs(e, context);
OnFiltering(args);
View umbraco-renderactioninvoker
namespace Umbraco.Web.Mvc
{
public class RenderActionInvoker : AsyncControllerActionInvoker
{
private static readonly ConcurrentDictionary<Type, ReflectedActionDescriptor> IndexDescriptors = new ConcurrentDictionary<Type, ReflectedActionDescriptor>();
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
{
var ad = base.FindAction(controllerContext, controllerDescriptor, actionName);
View umbraco-registercustomercontrollerfactory
ControllerBuilder.Current.SetControllerFactory(typeof (CustomControllerFactory));
View umbraco-customactioninvoker
public class CustomActionInvoker : AsyncControllerActionInvoker
{
private static readonly ConcurrentDictionary<Type, ActionDescriptor> IndexDescriptors = new ConcurrentDictionary<Type, ActionDescriptor>();
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
{
var ad = base.FindAction(controllerContext, controllerDescriptor, actionName);
if (ad == null)
{
View umbraco-customcontrollerfactory
public class CustomControllerFactory : RenderControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
var controllerInstance = base.CreateController(requestContext, controllerName) as Controller;
if (controllerInstance != null)
{
controllerInstance.ActionInvoker = new CustomActionInvoker();
}
View umbraco-rendermvccontroller-failing-async-index-action
public async Task<ActionResult> Index(RenderModel model, FormCollection form)
{
await Task.Delay(1000);
return CurrentTemplate(model);
}
View umbraco-surfacecontroller-async-action
[HttpPost]
public async Task<ActionResult> SubmitForm(string postcode)
{
// Do something async with the postcode
return CurrentUmbracoPage();
}
You can’t perform that action at this time.