Skip to content

Instantly share code, notes, and snippets.

@sjdweb
Created October 28, 2016 20:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjdweb/459052d5a6948e2d6aaea1cbc6217a1e to your computer and use it in GitHub Desktop.
Save sjdweb/459052d5a6948e2d6aaea1cbc6217a1e to your computer and use it in GitHub Desktop.
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
Copy link

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