public void GetUserDomain_VariousCases_Correct(string username, string expectedDomain) { // arrange var fakeIdentity = new Mock <IIdentity>(); fakeIdentity.Setup(x => x.IsAuthenticated) .Returns(false); var fakePrincipal = new ClaimsPrincipal(fakeIdentity.Object); var fakeRequest = new Mock <HttpRequest>(); fakeRequest.Setup(x => x.Headers) .Returns(new HeaderDictionary() { { CompanyHttpHeaders.UserId, username } }); var fakeHttpContext = new Mock <HttpContext>(); fakeHttpContext.Setup(x => x.Request) .Returns(fakeRequest.Object); fakeHttpContext.Setup(x => x.User) .Returns(fakePrincipal); // act var context = HttpCallContext.Create(fakeHttpContext.Object); // assert Assert.That(context.UserDomain, Is.EqualTo(expectedDomain)); }
public void Create_RequestWithoutHeaders_ContextWithDefaultValues() { // arrange var fakeIdentity = new Mock <IIdentity>(); fakeIdentity.Setup(x => x.IsAuthenticated) .Returns(false); var fakePrincipal = new ClaimsPrincipal(fakeIdentity.Object); var fakeRequest = new Mock <HttpRequest>(); fakeRequest.Setup(x => x.Headers) .Returns(new HeaderDictionary()); var fakeHttpContext = new Mock <HttpContext>(); fakeHttpContext.Setup(x => x.Request) .Returns(fakeRequest.Object); fakeHttpContext.Setup(x => x.User) .Returns(fakePrincipal); // act var context = HttpCallContext.Create(fakeHttpContext.Object); // assert Assert.Multiple(() => { Assert.That(context.UserId, Is.EqualTo(string.Empty)); Assert.That(context.UserDomain, Is.EqualTo(string.Empty)); Assert.That(context.RequestCallerIp, Is.EqualTo(string.Empty)); Assert.That(context.AuthorizeInfo, Is.EqualTo(string.Empty)); Assert.That(context.RequestId, Is.Not.Null); }); }
public void TestInitialize() { CallContextManagerInstance = CreateCallContextManager(); CallContextManagerInstance.CallContextOverride = new HttpCallContext(useLogicalCallContext: true); MachineInformation = new UnitTestMachineInformation(); HttpCallContext callContext = CallContextManagerInstance.CallContextHandler(MachineInformation) as HttpCallContext; callContext.ExistingCallContext(); callContext.StartCallContext(); }
public async Task InvokeAsync(HttpContext context, RequestDelegate next) { if (!context.Request.Headers.ContainsKey(CompanyHttpHeaders.RequestId)) { context.Request.Headers.Add(CompanyHttpHeaders.RequestId, Guid.NewGuid() .ToString("N")); } try { _httpCallContextAccessor.SetContext(HttpCallContext.Create(context)); await next(context); } finally { _httpCallContextAccessor.CleanContext(); } }
public void Create_AllFieldsInHeaders_CopiedToContext() { // arrange var fakeIdentity = new Mock <IIdentity>(); fakeIdentity.Setup(x => x.IsAuthenticated) .Returns(false); var fakePrincipal = new ClaimsPrincipal(fakeIdentity.Object); var fakeRequest = new Mock <HttpRequest>(); fakeRequest.Setup(x => x.Headers) .Returns(new HeaderDictionary() { { CompanyHttpHeaders.RequestId, "123" }, { HeaderNames.Authorization, "token" }, { CompanyHttpHeaders.RequestHeaderCallerIp, "ip" }, { CompanyHttpHeaders.UserId, "user" }, { CompanyHttpHeaders.UserDomain, "domain" }, }); var fakeHttpContext = new Mock <HttpContext>(); fakeHttpContext.Setup(x => x.Request) .Returns(fakeRequest.Object); fakeHttpContext.Setup(x => x.User) .Returns(fakePrincipal); // act var context = HttpCallContext.Create(fakeHttpContext.Object); // assert Assert.Multiple(() => { Assert.That(context.RequestId, Is.EqualTo("123")); Assert.That(context.AuthorizeInfo, Is.EqualTo("token")); Assert.That(context.RequestCallerIp, Is.EqualTo("ip")); Assert.That(context.UserId, Is.EqualTo("user")); Assert.That(context.UserDomain, Is.EqualTo("domain")); }); }
private async Task <HttpResponseMessage> ExecuteAuthenticatedAsync(Func <HttpClient, HttpCallContext, Task <HttpResponseMessage> > func, HttpCallContext context) { if (HttpClient.DefaultRequestHeaders.Authorization == null) { await AddAuthHeaderAsync(context.CancellationToken); } var response = await func(HttpClient, context); if (response.StatusCode == HttpStatusCode.Unauthorized) { _logger.Info($"The call to '{context.RequestUrl}' was unauthorized. Adding Auth Headers."); await AddAuthHeaderAsync(context.CancellationToken); response = await func(HttpClient, context); } if (!response.IsSuccessStatusCode) { _logger.Warn($"The request to '{context.RequestUrl}' was unsuccuessful. Status code: {response.StatusCode}. Response body: '{await response.Content.ReadAsStringAsync()}'."); } return(response); }