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 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 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 void Service_MatchesRequest_ReturnsFalseWhenMismatchedBodyFilter() { // Arrange var desc = new ServiceDesc() { // Note: case shouldn't matter. The ctor converts it to uppercase. Method = "GET", // Note: case shouldn't matter. When matching both the sides are converted to uppercase. Path = "/api/resource?x=1", // Case matters on the body filter. BodyContains = "This isn't in the body." }; var service = new Service(desc); var httpRequest = new TestHttpRequest() { Method = "GET", Path = "/api/resource", QueryString = new QueryString("?x=1") }; // Act var result = service.MatchesRequest( httpRequest, "Lorem ipsum dolor sit amet, consectetur adipiscing" ); // Assert Assert.IsFalse(result); }
public GravatarImageTests() { HttpRequest = new TestHttpRequest(); HttpContext = new TestHttpContext(HttpRequest); HtmlHelperExtensions.GetHttpContext = () => HttpContext; }
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 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 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 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); }
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 Service_MatchesRequest_ReturnsTrueWhenMatchesWithoutBodyFilter() { // Arrange var desc = new ServiceDesc() { // Note: case shouldn't matter. The ctor converts it to uppercase. Method = "get", // Note: case shouldn't matter. When matching both the sides are converted to uppercase. Path = "/API/Resource?X=1", }; var service = new Service(desc); var httpRequest = new TestHttpRequest() { Method = "GET", Path = "/api/resource", QueryString = new QueryString("?x=1") }; // Act var result = service.MatchesRequest(httpRequest, string.Empty); // Assert Assert.IsTrue(result); }
public async Task SetPayloadHandlerContentTypeOnPostAsync() { var mockRequestExecutor = Substitute.For <IRequestExecutor>(); mockRequestExecutor .PostAsync(Arg.Any <HttpRequest>(), Arg.Any <CancellationToken>()) .Returns(new HttpResponse <string>() { StatusCode = 200 }); var testPayloadHandler = new TestPayloadHandler(); PayloadHandler.TryRegister(testPayloadHandler); var request = new TestHttpRequest(); request.ContentType.Should().Be("application/json"); request.GetPayloadHandler().ContentType.Should().Be("application/json"); var dataStore = new DefaultDataStore(mockRequestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance); await dataStore.PostAsync <TestResource>(request, new RequestContext { ContentType = "foo" }, CancellationToken.None); request.ContentType.Should().Be("foo"); request.GetPayloadHandler().ContentType.Should().Be("foo"); }
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 void Init() { TestHttpRequest threq0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", new string[] { "text/xml" }, ConnectionType.KeepAlive, 4711, new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis")); threq0.Method = "GET"; threq0.HttpVersion = HttpHelper.HTTP10; TestHttpRequest threq1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", new string[] { "text/xml" }, ConnectionType.KeepAlive, 4711, new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2")); threq1.Method = "POST"; threq1.HttpVersion = HttpHelper.HTTP11; threq1.Headers["x-wuff"] = "wuffwuff"; threq1.Headers["www-authenticate"] = "go away"; req0 = new OSHttpRequest(new TestHttpClientContext(false), threq0); req1 = new OSHttpRequest(new TestHttpClientContext(false), threq1); rsp0 = new OSHttpResponse(new TestHttpResponse()); ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711); }
// 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 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 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 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 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 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 GetEvents_NoData_ReturnsEmptyEnumerable() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(0, events.Count()); }
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"]); } }
public object Clone() { TestHttpRequest clone = new TestHttpRequest(); clone._acceptTypes = _acceptTypes; clone._connection = _connection; clone._contentLength = _contentLength; clone._uri = _uri; clone._headers = new NameValueCollection(_headers); return(clone); }
public void SetPayloadHandlerWhenContentTypeIsSet() { // must register PkixCertPayloadHandler for it to be available to the HttpRequest // the OktaClient will register the PkixCertPayloadHandler on instantiation. PayloadHandler.TryRegister <PkixCertPayloadHandler>(); var request = new TestHttpRequest(); request.ContentType.Should().Be("application/json"); request.GetPayloadHandler().GetType().Should().Be(typeof(JsonPayloadHandler)); request.ContentType = "application/pkix-cert"; request.GetPayloadHandler().GetType().Should().Be(typeof(PkixCertPayloadHandler)); }
public async Task GetEvents_InvalidRequest_ReplyTokenReturnsNull() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(InvalidJson); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.IsNull(lineEvent.ReplyToken); }
public async Task <TestHttpResponse> SendAsync(TestHttpRequest request) { if (WebServerInstance is TestWebServer testWebServer) { return(await testWebServer.GetClient().SendAsync(request)); } using (var client = new HttpClient()) { var response = await client.SendAsync(request.ToHttpRequestMessage()); return(response.ToTestHttpResponse()); } }
// 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 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 Init() { TestHttpRequest threq0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", new string[] {"text/xml"}, ConnectionType.KeepAlive, 4711, new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis")); threq0.Method = "GET"; threq0.HttpVersion = HttpHelper.HTTP10; TestHttpRequest threq1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", new string[] {"text/xml"}, ConnectionType.KeepAlive, 4711, new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2")); threq1.Method = "POST"; threq1.HttpVersion = HttpHelper.HTTP11; threq1.Headers["x-wuff"] = "wuffwuff"; threq1.Headers["www-authenticate"] = "go away"; req0 = new OSHttpRequest(new TestHttpClientContext(false), threq0); req1 = new OSHttpRequest(new TestHttpClientContext(false), threq1); rsp0 = new OSHttpResponse(new TestHttpResponse()); ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711); }
public object Clone() { TestHttpRequest clone = new TestHttpRequest(); clone._acceptTypes = _acceptTypes; clone._connection = _connection; clone._contentLength = _contentLength; clone._uri = _uri; clone._headers = new NameValueCollection(_headers); return clone; }