public void GivenAnInvalidSessionIDReturns400() { var request = new HttpRequestMessage(); request.Headers.Add("X-SessionID", "Not a GUID"); var authUtilMock = new Mock<IAuthenticationUtil>(); authUtilMock.Setup(a => a.ValidateSessionID(It.IsAny<string>())) .Returns(() => ApiResultCode.InvalidSessionID); var toolUtilMock = new Mock<IToolUtil>(); toolUtilMock.Setup(t => t.getEnumDescription(It.IsAny<ApiResultCode>())) .Returns((ApiResultCode theEnum) => theEnum.ToString()); var sessionHttpHandler = new SessionHttpHandler() { _authenticationUtil = authUtilMock.Object, _toolUtil = toolUtilMock.Object }; var response = new HttpMessageInvoker( sessionHttpHandler ).SendAsync(request, new CancellationToken()).Result; Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest); }
public void GivenExpireSessionIDReturns401() { var request = new HttpRequestMessage(); request.Headers.Add("X-SessionID", Guid.Empty.ToString()); var authUtilMock = new Mock<IAuthenticationUtil>(); authUtilMock.Setup(a => a.ValidateSessionID(It.IsAny<string>())) .Returns(() => ApiResultCode.ExpiredSessionID); var toolUtilMock = new Mock<IToolUtil>(); toolUtilMock.Setup(t => t.getEnumDescription(It.IsAny<ApiResultCode>())) .Returns((ApiResultCode theEnum) => theEnum.ToString()); var sessionHttpHandler = new SessionHttpHandler() { _authenticationUtil = authUtilMock.Object, _toolUtil = toolUtilMock.Object }; var error = new Error() { code = (int)ApiResultCode.ExpiredSessionID, message = ApiResultCode.ExpiredSessionID.ToString() }; var response = new HttpMessageInvoker( sessionHttpHandler ).SendAsync(request, new CancellationToken()).Result; Assert.IsTrue(response.StatusCode == HttpStatusCode.Unauthorized); Assert.IsTrue(response.Content.ReadAsStringAsync().Result == JsonConvert.SerializeObject(error, Formatting.Indented)); }
public void GivenInternalServerReturnsEmptyBody() { var innerHandlerMock = new Mock<DelegatingHandler>(); innerHandlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>() ).Returns(create500Response()); var authUtilMock = new Mock<IAuthenticationUtil>(); authUtilMock.Setup(a => a.ValidateSessionID(It.IsAny<string>())) .Returns(() => ApiResultCode.Success); var sessionHandler = new SessionHttpHandler(); sessionHandler._authenticationUtil = authUtilMock.Object; sessionHandler.InnerHandler = innerHandlerMock.Object; var invoker = new HttpMessageInvoker(sessionHandler); var request = new HttpRequestMessage(); request.Headers.Add("X-SessionID", Guid.Empty.ToString()); HttpResponseMessage response = invoker.SendAsync(request, new CancellationToken()).Result; Assert.IsTrue(response.StatusCode == HttpStatusCode.InternalServerError); Assert.IsTrue(response.Content.ReadAsStringAsync().Result == string.Empty); }
public void GivenNoSessionIDReturn400() { var toolUtilMock = new Mock<IToolUtil>(); toolUtilMock.Setup(t => t.getEnumDescription(It.IsAny<ApiResultCode>())) .Returns((ApiResultCode theEnum) => theEnum.ToString()); var sessionHttpHandler = new SessionHttpHandler() { _toolUtil = toolUtilMock.Object }; var request = new HttpRequestMessage(); var response = new HttpMessageInvoker( sessionHttpHandler ).SendAsync(request, new CancellationToken()).Result; Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest); }