Created
October 28, 2016 20:27
-
-
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).
This file contains 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 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?