Instantly share code, notes, and snippets.

Embed
What would you like to do?
NancyFx Browser shim for acceptance tests to HttpResponseMessage (specifically for asp.net core migration).
public class BrowserShim
{
private readonly HttpClient _client;
public BrowserShim(HttpClient client)
{
_client = client;
}
private static RequestModifier ApplyModifier(Action<RequestModifier> action, HttpRequestMessage req, string url)
{
var modifier = new RequestModifier();
action(modifier);
modifier.Apply(req, url);
return modifier;
}
public async Task<BrowserResponse> Put(string url, Action<RequestModifier> action = null)
{
return await HttpRequest(url, HttpMethod.Put, action);
}
public async Task<BrowserResponse> Get(string url, Action<RequestModifier> action = null)
{
return await HttpRequest(url, HttpMethod.Get, action);
}
private async Task<BrowserResponse> HttpRequest(string url, HttpMethod httpMethod, Action<RequestModifier> action)
{
var req = new HttpRequestMessage(httpMethod, url);
if (action != null)
{
ApplyModifier(action, req, url);
}
var result = await _client.SendAsync(req);
return new BrowserResponse(result);
}
public async Task<BrowserResponse> Patch(string url, Action<RequestModifier> action)
{
return await HttpRequest(url, new HttpMethod("PATCH"), action);
}
public async Task<BrowserResponse> Delete(string url, Action<RequestModifier> action)
{
return await HttpRequest(url, HttpMethod.Delete, action);
}
public async Task<BrowserResponse> Post(string url, Action<RequestModifier> action)
{
return await HttpRequest(url, HttpMethod.Post, action);
}
}
public class RequestModifier
{
private readonly List<Action<HttpRequestMessage>> _modifiers = new List<Action<HttpRequestMessage>>();
private string url;
public void User(User user)
{
_modifiers.Add(m =>
{
var qChar = url.Contains("?") ? "&" : "?";
m.RequestUri = new Uri(url + $"{qChar}$id$=id-{user.Id}", UriKind.Relative);
});
}
public void JsonBody(object request)
{
_modifiers.Add(m => m.Content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"));
}
public void Apply(HttpRequestMessage request, string uri)
{
url = uri;
foreach (var modifier in _modifiers)
{
modifier(request);
}
}
}
public class BrowserResponse
{
public BrowserResponse(HttpResponseMessage message)
{
StatusCode = message.StatusCode;
ContentType = message.Content.Headers.ContentType?.MediaType;
Body = new ResponseBody(message.Content);
}
public HttpStatusCode StatusCode { get; private set; }
public ResponseBody Body { get; private set; }
public string ContentType { get; private set; }
public class ResponseBody
{
public HttpContent Content { get; }
public ResponseBody(HttpContent content)
{
Content = content;
}
public string AsString()
{
return Content.ReadAsStringAsync().Result;
}
}
}
@Moses-Bassey

This comment has been minimized.

Moses-Bassey commented Apr 20, 2017

Hi,

Am really stock. looking for a solution so i can migrate from nancyfx user identity to asp.net core identity framework, Please any help?

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