private static async Task<ServerTransaction> Signin(TestServer server, string user, string password) { var request = new HttpRequestMessage(HttpMethod.Post, $"{TestServerHelper.BaseUrl}/Auth/Signin"); var dict = new Dictionary<string, string>(); dict["username"] = user; dict["password"] = password; request.Content = new FormUrlEncodedContent(dict.AsEnumerable()); var transaction = new ServerTransaction { Request = request, Response = await server.CreateClient().SendAsync(request), }; if (transaction.Response.Headers.Contains("Set-Cookie")) { transaction.Cookies = TestServerHelper.GenerateCookies(transaction.Response.Headers.GetValues("Set-Cookie")); } transaction.ResponseText = await transaction.Response.Content.ReadAsStringAsync(); return transaction; }
public static async Task<ServerTransaction> SendAsync(TestServer server, string uri, string cookieHeader = "", bool ajaxRequest = false, HttpMethod method = null, object body = null, bool tokens = false) { var request = new HttpRequestMessage(method == null? HttpMethod.Get : method, uri); if (body != null) { request.Content = new StringContent(JsonConvert.SerializeObject(body)); request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application / json"); } if (!string.IsNullOrEmpty(cookieHeader) || tokens) { if (tokens) { cookieHeader = !string.IsNullOrEmpty(cookieHeader) && !cookieHeader.EndsWith(";") ? $"{cookieHeader};" : cookieHeader; cookieHeader = $"{cookieHeader}{TestAntiforgeryOptionsSetup.CookieName}={TestAntiforgeryTokenGenerator.CookieString};"; } request.Headers.Add("Cookie", cookieHeader); } if (ajaxRequest) { request.Headers.Add("X-Requested-With", "XMLHttpRequest"); } if(tokens) { request.Headers.Add("RequestVerificationToken", TestAntiforgeryTokenGenerator.FieldString); } var transaction = new ServerTransaction { Request = request, Response = await server.CreateClient().SendAsync(request), }; if (transaction.Response.Headers.Contains("Set-Cookie")) { transaction.Cookies = GenerateCookies(transaction.Response.Headers.GetValues("Set-Cookie")); } transaction.StatusCode = transaction.Response.StatusCode; transaction.ResponseText = await transaction.Response.Content.ReadAsStringAsync(); return transaction; }