public IHttpActionResult Post(UpdateConfigurationRequest updateConfiguration) { try { if (!remoteSettings.AllowRemoteDynamicConfigurationUpdate) { return new ChaosWebApiActionResult<string>(Request, HttpStatusCode.Forbidden, "Dynamic configuration update is disabled"); } var apiConfiguration = UpdateRequestToConfigurationConverter.ToApiConfiguration(updateConfiguration); apiConfiguration.Validate(); var existingHandler = configuration.MessageHandlers.FirstOrDefault(f => f.GetType() == typeof(ChaoticDelegatingHandler)); configuration.MessageHandlers.Remove(existingHandler); if (existingHandler != null) { existingHandler.Dispose(); } ChaosInstaller.Register(configuration, apiConfiguration); return Ok(); } catch (InvalidOperationException validationException) { return BadRequest(validationException.Message); } catch (Exception) { return InternalServerError(); } }
public void Adds_Handler_When_Valid_Settings_Are_Provided() { remoteSettings.SetupGet(f => f.AllowRemoteDynamicConfigurationUpdate).Returns(true); var request = new UpdateConfigurationRequest(){Enabled = true}; var result = chaosConfigurationChangedHookController.Post(request); result.ShouldBeType(typeof(OkResult)); configuration.MessageHandlers.Count.ShouldEqual(1); }
public void Returns_BadRequest_Result_When_Settings_Are_Invalid() { remoteSettings.SetupGet(f => f.AllowRemoteDynamicConfigurationUpdate).Returns(true); var request = new UpdateConfigurationRequest() { Enabled = true, ChaosSettings = new Collection<UpdateChaosSettings>() { new UpdateChaosSettings() { MaxResponseDelayTime = 1, MinResponseDelayTime = 2 } } }; var result = chaosConfigurationChangedHookController.Post(request); result.ShouldBeType(typeof(BadRequestErrorMessageResult)); }
public static ApiConfiguration ToApiConfiguration(UpdateConfigurationRequest updateConfigurationRequest) { var config = new ApiConfiguration { ChaosInterval = new TimeSpan(0, 0, updateConfigurationRequest.ChaosInterval), ConfigurationRotationInterval = new TimeSpan(0, 0, updateConfigurationRequest.ConfigurationRotationInterval), Enabled = updateConfigurationRequest.Enabled }; foreach (var updateChaosSettings in updateConfigurationRequest.ChaosSettings) { var chaosConfiguration = new ChaosSettings { Name = updateChaosSettings.Name, MaxResponseDelayTime = updateChaosSettings.MaxResponseDelayTime, MinResponseDelayTime = updateChaosSettings.MinResponseDelayTime, PercentageOfChaos = updateChaosSettings.PercentageOfChaos, PercentageOfSlowResponses = updateChaosSettings.PercentageOfSlowResponses, ResponseTypeMediaType = updateChaosSettings.ResponseTypeMediaType }; if (updateChaosSettings.IgnoreUrls != null) { foreach (var url in updateChaosSettings.IgnoreUrls) { chaosConfiguration.IgnoreUrlPattern.Add(url.Pattern); } } foreach (var updateResponse in updateChaosSettings.HttpResponses) { var httpResponse = new ResponseDetails { StatusCode = updateResponse.StatusCode }; foreach (var payload in updateResponse.Payloads.Select(chaosResponsePayload => new ChaosResponsePayload { Code = chaosResponsePayload.Code, Content = chaosResponsePayload.Content })) { httpResponse.Payloads.Add(payload); } chaosConfiguration.HttpResponses.Add(httpResponse); } config.ChaosSettings.Add(chaosConfiguration); } return config; }