public async Task <string> GetAsync(string url) { Request = new TestHttpRequest(url); Response = new TestHttpResponse(); if (!(WebServer is TestWebServer testServer)) { throw new InvalidOperationException(); } testServer._entryQueue.Enqueue(this); try { while (Response.OutputStream.Position == 0) { await Task.Delay(1); } } catch { // ignore } return(Encoding.UTF8.GetString((Response.OutputStream as MemoryStream)?.ToArray())); }
public void Service_WriteResponseTo_WritesResponse() { // Arrange var desc = new ServiceDesc() { Method = "GET", Path = "/api/resource?x=1", ContentType = "application/json", StatusCode = "200", Body = "Lorem ipsum dolor sit amet, consectetur adipiscing" }; var service = new Service(desc); var httpResponse = new TestHttpResponse(); // Act service.WriteResponseTo(httpResponse).Wait(); // Assert Assert.AreEqual(200, httpResponse.StatusCode); Assert.AreEqual("application/json", httpResponse.ContentType); Assert.AreEqual( "Lorem ipsum dolor sit amet, consectetur adipiscing", httpResponse.ReadBody() ); }
public void BadEggValidationInvalidRouteProvider() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService; IPluginHelperService pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginBadEgg.GetRequiredService <IPluginTypesService>(); INotificationService notificationService = _testPluginBadEgg.GetRequiredService <INotificationService>(); IIpValidation iPValidation = new TestIPValidation(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); RequestDelegate requestDelegate = async(context) => { await Task.Delay(0); }; RouteDataServices routeDataServices = new RouteDataServices(); BadEggMiddleware badEgg = new BadEggMiddleware(requestDelegate, null, routeDataServices, pluginHelperServices, pluginTypesService, iPValidation, settingsProvider, notificationService); }
public void ServiceFunctions_DeleteAllServices_ReturnsNotFoundAfterDeleting() { // Arrange var service1Id = AddService("GET", "/api/resource/5be5223c-70d4-4a61-aa5c-be5d60d90001"); var service2Id = AddService("GET", "/api/resource/5be5223c-70d4-4a61-aa5c-be5d60d90002"); var service3Id = AddService("GET", "/api/resource/5be5223c-70d4-4a61-aa5c-be5d60d90003"); var request = new TestHttpRequest() { Method = "DELETE", Path = "/__vs/services", BodyString = string.Empty }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.DeleteAllServices(context, next).Wait(); // Assert Assert.AreEqual(200, response.StatusCode); Assert.AreEqual(404, InvokeService("GET", "/api/resource/5be5223c-70d4-4a61-aa5c-be5d60d90001"), "Invoking service1 should result in a 404."); Assert.AreEqual(404, InvokeService("GET", "/api/resource/5be5223c-70d4-4a61-aa5c-be5d60d90002"), "Invoking service2 should result in a 404."); Assert.AreEqual(404, InvokeService("GET", "/api/resource/5be5223c-70d4-4a61-aa5c-be5d60d90003"), "Invoking service3 should result in a 404."); }
public async Task Validate_TestEndCalled_ISmokeTestProviderNotRegistered_Void() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/end/"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); IServiceCollection serviceCollection = new ServiceCollection() as IServiceCollection; TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse, serviceCollection.BuildServiceProvider()); ILogger logger = new Logger(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); await sut.Invoke(httpContext); Assert.IsFalse(nextDelegateCalled); Assert.AreEqual(200, httpResponse.StatusCode); Assert.IsNull(httpResponse.ContentType); } }
public void ServiceFunctions_AddService_InvokesNextFunctionWhenMethodNotPOST() { // Arrange #region BodyString var bodyString = @"# BodyString # This ""GET"" is irrelevant Method: GET Path: /api/things/53"; #endregion var request = new TestHttpRequest() { // This "GET" causes the request not to match the required pattern. Method = "GET", Path = "/__vs/services", BodyString = bodyString }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); var numCallsToNext = 0; Task next() { numCallsToNext++; return(Task.CompletedTask); } // Act ServiceFunctions.AddService(context, next).Wait(); // Assert Assert.AreEqual(1, numCallsToNext); }
public async Task BadEggValidationSuccess() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService; IPluginHelperService pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginBadEgg.GetRequiredService <IPluginTypesService>(); INotificationService notificationService = _testPluginBadEgg.GetRequiredService <INotificationService>(); IIpValidation iPValidation = new TestIPValidation(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; ActionDescriptorCollection actionDescriptorCollection = new ActionDescriptorCollection(new List <ActionDescriptor>(), 1); TestActionDescriptorCollectionProvider testActionDescriptorCollectionProvider = new TestActionDescriptorCollectionProvider(actionDescriptorCollection); RouteDataServices routeDataServices = new RouteDataServices(); BadEggMiddleware badEgg = new BadEggMiddleware(requestDelegate, testActionDescriptorCollectionProvider, routeDataServices, pluginHelperServices, pluginTypesService, iPValidation, settingsProvider, notificationService); await badEgg.Invoke(httpContext); Assert.IsTrue(nextDelegateCalled); }
public async Task RetrieveUniqueId_InvalidRequest_SiteId_Returns404() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/SiteId"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); ILogger logger = new Logger(); using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(null, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); await sut.Invoke(httpContext); Assert.AreEqual(404, httpResponse.StatusCode); } }
public void BadEggValidationInvalidRequestDelegate() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService; IPluginHelperService pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginBadEgg.GetRequiredService <IPluginTypesService>(); INotificationService notificationService = _testPluginBadEgg.GetRequiredService <INotificationService>(); IIpValidation iPValidation = new TestIPValidation(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); ActionDescriptorCollection actionDescriptorCollection = new ActionDescriptorCollection(new List <ActionDescriptor>(), 1); TestActionDescriptorCollectionProvider testActionDescriptorCollectionProvider = new TestActionDescriptorCollectionProvider(actionDescriptorCollection); RouteDataServices routeDataServices = new RouteDataServices(); BadEggMiddleware badEgg = new BadEggMiddleware(null, testActionDescriptorCollectionProvider, routeDataServices, pluginHelperServices, pluginTypesService, iPValidation, settingsProvider, notificationService); }
public async Task LoginFromCookieValueCookieValidLoginUserFound() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); LoginControllerSettings loginControllerSettings = settingsProvider.GetSettings <LoginControllerSettings>(nameof(LoginPlugin)); TestRequestCookieCollection cookies = new TestRequestCookieCollection(); cookies.AddCookie("RememberMe", Shared.Utilities.Encrypt("123", loginControllerSettings.EncryptionKey)); TestHttpRequest httpRequest = new TestHttpRequest(cookies); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginLogin) as IPluginClassesService; TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); RequestDelegate requestDelegate = async(context) => { await Task.Delay(0); }; LoginMiddleware login = new LoginMiddleware(requestDelegate, loginProvider, settingsProvider, claimsProvider); await login.Invoke(httpContext, authenticationService); TestResponseCookies responseCookies = httpResponse.Cookies as TestResponseCookies; Assert.IsNotNull(responseCookies); Assert.IsTrue(authenticationService.SignInAsyncCalled); }
// Adds a service and returns it's ID. // // Do not use this to test the ServiceFunctions.AddService method. // This was designed to help other tests and assumes that the // ServiceFunctions.AddService has already been tested. private static string AddService(string method, string path) { var bodyString = @"# BodyString # Request Method: {0} Path: {1} # Response ContentType: application/json StatusCode: 200 # Body {""color"":""Red""}"; var request = new TestHttpRequest() { Method = "POST", Path = "/__vs/services", BodyString = bodyString .Replace("{0}", method) .Replace("{1}", path) }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; ServiceFunctions.AddService(context, next).Wait(); return(response.ReadBody()); }
public async Task LoginAutoLoginFromHeadersInvalidUsernameAndPassword() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); LoginControllerSettings loginControllerSettings = settingsProvider.GetSettings <LoginControllerSettings>(nameof(LoginPlugin)); TestHttpRequest httpRequest = new TestHttpRequest(); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginLogin) as IPluginClassesService; TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); RequestDelegate requestDelegate = async(context) => { await Task.Delay(0); }; string encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes("Miley:Cyrus")); httpRequest.Headers.Add(SharedPluginFeatures.Constants.HeaderAuthorizationName, "Basic " + encoded); LoginMiddleware login = new LoginMiddleware(requestDelegate, loginProvider, settingsProvider, claimsProvider); await login.Invoke(httpContext, authenticationService); Assert.IsFalse(authenticationService.SignInAsyncCalled); }
public void ServiceFunctions_QueryService_ReturnsServiceStats() { // Arrange var serviceId = AddService("GET", "/api/serviceToBeQueried/1"); InvokeService("GET", "/api/serviceToBeQueried/1", "Lorem ipsum dolor"); InvokeService("GET", "/api/serviceToBeQueried/1", "sit amet, consectetur adipiscing"); var request = new TestHttpRequest() { Method = "GET", Path = "/__vs/services/" + serviceId }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.QueryService(context, next).Wait(); // Assert Assert.AreEqual(200, response.StatusCode); Assert.AreEqual("text/plain", response.ContentType); var expectedResponseBody = new StringBuilder() .AppendLine($"CallCount: 2") .AppendLine() .AppendLine("LastRequestBody:") .Append("sit amet, consectetur adipiscing") .ToString(); Assert.AreEqual(expectedResponseBody, response.ReadBody()); }
public async Task LoginAutoLoginFromHeadersInvalidEncoding() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); LoginControllerSettings loginControllerSettings = settingsProvider.GetSettings <LoginControllerSettings>(nameof(LoginPlugin)); TestHttpRequest httpRequest = new TestHttpRequest(); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginLogin) as IPluginClassesService; TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); RequestDelegate requestDelegate = async(context) => { await Task.Delay(0); }; httpRequest.Headers.Add(SharedPluginFeatures.Constants.HeaderAuthorizationName, "Basic blahblahblah"); LoginMiddleware login = new LoginMiddleware(requestDelegate, loginProvider, settingsProvider, claimsProvider); await login.Invoke(httpContext, authenticationService); Assert.AreEqual(400, httpContext.Response.StatusCode); Assert.IsFalse(authenticationService.SignInAsyncCalled); }
public void CustomerController_GetCustomer_Success(string Country) { string getUri = $"{baseUri}/{Country}"; TestHttpResponse testHttpResponse = WebTestManager.HttpClient.GET(getUri).Result; Assert.AreEqual(HttpStatusCode.OK, testHttpResponse.StatusCode); }
public void ServiceFunctions_DeleteService_InvokesNextFunctionWhenPathNotCorrect() { // Arrange var request = new TestHttpRequest() { Method = "GET", // This path causes the request not to match the required pattern. Path = "/__vs/badResource/ab61870e-8427-ae57effd5c6a", BodyString = string.Empty }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); var numCallsToNext = 0; Task next() { numCallsToNext++; return(Task.CompletedTask); } // Act ServiceFunctions.DeleteService(context, next).Wait(); // Assert Assert.AreEqual(1, numCallsToNext); }
public void ServiceFunctions_AddService_ReturnsOkAndServiceId() { // Arrange #region BodyString var bodyString = @"# BodyString Method: GET Path: /api/things/53"; #endregion var request = new TestHttpRequest() { Method = "POST", Path = "/__vs/services", BodyString = bodyString }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.AddService(context, next).Wait(); // Assert Assert.AreEqual(200, response.StatusCode); Assert.AreEqual("text/plain", response.ContentType); Assert.IsTrue(Guid.TryParse(response.ReadBody(), out Guid serviceId)); Assert.AreNotEqual(Guid.Empty, serviceId); }
public TestHttpContext(string url) { testRequest = new TestHttpRequest() { _AppRelativeCurrentExecutionFilePath = url }; testResponse = new TestHttpResponse(); }
public TestHttpContext(string url, string httpVerb = "GET") { testRequest = new TestHttpRequest() { _AppRelativeCurrentExecutionFilePath = url, _HttpMethod = httpVerb }; testResponse = new TestHttpResponse(); }
public async Task Validate_TestStartCalled_ISmokeTestProviderRegistered_Returns_NvpCodecValues() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/Start/"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); IServiceCollection serviceCollection = new ServiceCollection() as IServiceCollection; NVPCodec codecValues = new NVPCodec(); codecValues.Add("username", "admin"); MockSmokeTestProvider smokeTestProvider = new MockSmokeTestProvider(codecValues); serviceCollection.AddSingleton <ISmokeTestProvider>(smokeTestProvider); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse, serviceCollection.BuildServiceProvider()); ILogger logger = new Logger(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); await sut.Invoke(httpContext); Assert.IsFalse(nextDelegateCalled); Assert.AreEqual(200, httpResponse.StatusCode); Assert.IsNull(httpResponse.ContentType); Assert.IsTrue(smokeTestProvider.StartCalled); byte[] data = new byte[httpResponse.Body.Length]; httpResponse.Body.Position = 0; httpResponse.Body.Read(data, 0, data.Length); string test = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey); Assert.IsFalse(String.IsNullOrEmpty(test)); NVPCodec codec = new NVPCodec(); codec.Decode(test); Assert.AreEqual(1, codec.AllKeys.Length); Assert.IsTrue(codec.AllKeys.Contains("username")); Assert.AreEqual("admin", codec["username"]); } }
// Invokes a service and returns the HTTP status code result. // // Do not use this to test the ServiceFunctions.InvokeService method. // This was designed to help other tests and assumes that the // ServiceFunctions.InvokeService has already been tested. private static int InvokeService(string method, string path, string bodyString = "") { var request = new TestHttpRequest() { Method = method, Path = path, BodyString = bodyString }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; ServiceFunctions.InvokeService(context, next).Wait(); return(response.StatusCode); }
public void ServiceFunctions_QueryService_Returns404WhenServiceDoesNotExist() { // Arrange var request = new TestHttpRequest() { Method = "GET", Path = "/__vs/services/" + Guid.NewGuid().ToString() }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.QueryService(context, next).Wait(); // Assert Assert.AreEqual(404, response.StatusCode); }
public async Task BadEggValidationIgnoreValidation() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); BadEggSettings badEggSettings = settingsProvider.GetSettings <BadEggSettings>(SharedPluginFeatures.Constants.BadEggSettingsName); TestHttpRequest httpRequest = new TestHttpRequest(); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginBadEgg) as IPluginClassesService; IPluginHelperService pluginHelperServices = _testPluginBadEgg.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginBadEgg.GetRequiredService <IPluginTypesService>(); INotificationService notificationService = _testPluginBadEgg.GetRequiredService <INotificationService>(); IIpValidation iPValidation = new TestIPValidation(); IIpManagement ipManagement = _testPluginBadEgg.GetRequiredService <IIpManagement>(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); httpRequest.SetContext(httpContext); MockLoginProvider loginProvider = new MockLoginProvider(); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); TestAuthenticationService authenticationService = new TestAuthenticationService(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; ActionDescriptorCollection actionDescriptorCollection = new ActionDescriptorCollection(new List <ActionDescriptor>(), 1); TestActionDescriptorCollectionProvider testActionDescriptorCollectionProvider = new TestActionDescriptorCollectionProvider(actionDescriptorCollection); RouteDataServices routeDataServices = new RouteDataServices(); httpRequest.Headers.Add(SharedPluginFeatures.Constants.BadEggValidationIgnoreHeaderName, badEggSettings.IgnoreValidationHeaderCode); BadEggMiddleware badEgg = new BadEggMiddleware(requestDelegate, testActionDescriptorCollectionProvider, routeDataServices, pluginHelperServices, pluginTypesService, iPValidation, settingsProvider, notificationService); await badEgg.Invoke(httpContext); Assert.IsTrue(httpContext.Response.Headers.ContainsKey(Constants.BadEggValidationIgnoreHeaderName)); Assert.AreEqual(httpContext.Response.Headers[Constants.BadEggValidationIgnoreHeaderName], Boolean.TrueString); Assert.IsTrue(nextDelegateCalled); Assert.AreEqual(200, httpContext.Response.StatusCode); }
public async Task RetrieveUniqueId_ValidRequest_Count_Enabled_Returns200() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/Count/"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); ILogger logger = new Logger(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); Assert.IsFalse(nextDelegateCalled); await sut.Invoke(httpContext); Assert.AreEqual(200, httpResponse.StatusCode); byte[] data = new byte[httpResponse.Body.Length]; httpResponse.Body.Position = 0; httpResponse.Body.Read(data, 0, data.Length); string count = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey); if (Int32.TryParse(count, out int actualCount)) { Assert.IsTrue(actualCount > 1); } else { throw new InvalidCastException("Failed to convert returned count"); } } }
public void ServiceFunctions_DeleteAllServices_ReturnsOk() { // Arrange var request = new TestHttpRequest() { Method = "DELETE", Path = "/__vs/services", BodyString = string.Empty }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.DeleteAllServices(context, next).Wait(); // Assert Assert.AreEqual(200, response.StatusCode); }
public void ServiceFunctions_InvokeService_ReturnsNotFound() { // Arrange var request = new TestHttpRequest() { Method = "PUT", Path = "/api/things/fc845a74-973e-4db9-8951-5c99e7cf009c", BodyString = string.Empty }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.InvokeService(context, next).Wait(); // Assert Assert.AreEqual(404, response.StatusCode); }
public async void LoginNullAuthenticationValueOnInvoke() { TestRequestCookieCollection cookies = new TestRequestCookieCollection(); cookies.AddCookie("RememberMe", "1"); TestHttpRequest httpRequest = new TestHttpRequest(cookies); TestHttpResponse httpResponse = new TestHttpResponse(); IPluginClassesService pluginServices = new pm.PluginServices(_testPluginLogin) as IPluginClassesService; TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); MockLoginProvider loginProvider = new MockLoginProvider(); ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); MockClaimsProvider claimsProvider = new MockClaimsProvider(pluginServices); RequestDelegate requestDelegate = async(context) => { await Task.Delay(0); }; LoginMiddleware login = new LoginMiddleware(requestDelegate, loginProvider, settingsProvider, claimsProvider); await login.Invoke(httpContext, null); }
public async Task Validate_TestStartCalled_ISmokeTestProviderNotRegistered_Returns_EmptyString() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/Start/"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse, _testPluginSmokeTest.GetServiceProvider()); ILogger logger = new Logger(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); await sut.Invoke(httpContext); Assert.IsFalse(nextDelegateCalled); Assert.AreEqual(200, httpResponse.StatusCode); Assert.IsNull(httpResponse.ContentType); byte[] data = new byte[httpResponse.Body.Length]; httpResponse.Body.Position = 0; httpResponse.Body.Read(data, 0, data.Length); string test = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey); Assert.IsTrue(String.IsNullOrEmpty(test)); } }
public async Task RetrieveUniqueId_ValidRequest_RetrieveTest_Enabled_Returns200() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/Test/1"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); ILogger logger = new Logger(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); await sut.Invoke(httpContext); Assert.IsFalse(nextDelegateCalled); Assert.AreEqual(200, httpResponse.StatusCode); Assert.AreEqual("application/json", httpResponse.ContentType); byte[] data = new byte[httpResponse.Body.Length]; httpResponse.Body.Position = 0; httpResponse.Body.Read(data, 0, data.Length); string test = Decrypt(Encoding.UTF8.GetString(data), EncryptionKey); Assert.IsTrue(test.Contains("Please try again")); Assert.IsTrue(test.Contains("Method\":\"POST") || test.Contains("Method\":\"GET")); } }
public async Task RetrieveUniqueId_ValidRequest_SiteId_Disabled_Returns200() { ISettingsProvider settingsProvider = new pm.DefaultSettingProvider(Directory.GetCurrentDirectory()); TestHttpRequest httpRequest = new TestHttpRequest(); httpRequest.Path = "/smokeTest/SiteId/"; TestHttpResponse httpResponse = new TestHttpResponse(); IPluginHelperService pluginHelperServices = _testPluginSmokeTest.GetRequiredService <IPluginHelperService>(); IPluginTypesService pluginTypesService = _testPluginSmokeTest.GetRequiredService <IPluginTypesService>(); TestHttpContext httpContext = new TestHttpContext(httpRequest, httpResponse); ILogger logger = new Logger(); bool nextDelegateCalled = false; RequestDelegate requestDelegate = async(context) => { nextDelegateCalled = true; await Task.Delay(0); }; using (WebSmokeTestMiddleware sut = new WebSmokeTestMiddleware(requestDelegate, pluginHelperServices, pluginTypesService, settingsProvider, logger)) { sut.Enabled = false; List <WebSmokeTestItem> smokeTests = sut.SmokeTests; Assert.IsTrue(smokeTests.Count > 1); await sut.Invoke(httpContext); Assert.IsTrue(nextDelegateCalled); Assert.AreEqual(200, httpResponse.StatusCode); byte[] data = new byte[httpResponse.Body.Length]; httpResponse.Body.Position = 0; httpResponse.Body.Read(data, 0, data.Length); string id = Encoding.UTF8.GetString(data); Assert.AreEqual("", id); } }
public void ServiceFunctions_DeleteServices_ReturnsNotFoundAfterDeleting() { // Arrange var serviceId = AddService("GET", "/api/resource/5be5223c-70d4-4a61-af5c-be5d60d9eb65"); var request = new TestHttpRequest() { Method = "DELETE", Path = "/__vs/services/" + serviceId, BodyString = string.Empty }; var response = new TestHttpResponse(); var context = new TestHttpContext(request, response); Task next() => Task.CompletedTask; // Act ServiceFunctions.DeleteService(context, next).Wait(); // Assert Assert.AreEqual(200, response.StatusCode); Assert.AreEqual(404, InvokeService("GET", "/api/resource/5be5223c-70d4-4a61-af5c-be5d60d9eb65")); }
private void RunTestHttp( string httpMethod, string origin, string configXml, string json, string requestId, string userAgent, string userHostAddress, DateTime serverSideTimeUtc, string url, int expectedResponseCode, NameValueCollection expectedResponseHeaders, List<LogEntry> expectedLogEntries) { // Arrange TestHttpResponse response = new TestHttpResponse(); TestLogger logger = new TestLogger(); XmlElement xe = Utils.ConfigToXe(configXml); // Act LoggerProcessor.ProcessLogRequest(json, userAgent, userHostAddress, serverSideTimeUtc, url, requestId, httpMethod, origin, response, logger, xe); // Assert Assert.AreEqual(expectedResponseCode, response.StatusCode); TestLogEntries(expectedLogEntries, logger.LogEntries); TestResponseHeaders(expectedResponseHeaders, response.Headers); }