/// <summary> /// Parses the node. /// </summary> /// <param name="node">The node to parse.</param> /// <param name="config">The rewriter configuration.</param> /// <returns>The parsed action, or null if no action parsed.</returns> public override IRewriteAction Parse(XmlNode node, object config) { XmlNode rewriteAttribute = node.Attributes.GetNamedItem(Constants.AttrRewrite); if (rewriteAttribute != null) { RewriteProcessing processing = RewriteProcessing.ContinueProcessing; XmlNode processingNode = node.Attributes.GetNamedItem(Constants.AttrProcessing); if (processingNode != null) { if (processingNode.Value == Constants.AttrValueRestart) { processing = RewriteProcessing.RestartProcessing; } else if (processingNode.Value == Constants.AttrValueStop) { processing = RewriteProcessing.StopProcessing; } else if (processingNode.Value != Constants.AttrValueContinue) { throw new ConfigurationException(MessageProvider.FormatString(Message.ValueOfProcessingAttribute, processingNode.Value, Constants.AttrValueContinue, Constants.AttrValueRestart, Constants.AttrValueStop), node); } } return(new RewriteAction(rewriteAttribute.Value, processing)); } else { return(null); } }
public void Constructor_WithNullLocation_Throws() { // Arrange string location = null; RewriteProcessing processing = RewriteProcessing.StopProcessing; // Act/Assert ExceptionAssert.Throws <ArgumentNullException>(() => new RewriteAction(location, processing)); }
public void Execute_WhenNoActionsOrConditions_ReturnsContinueProcessing() { // Arrange ConditionalAction action = new ConditionalAction(); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); }
public void Execute_SetsStatusCode_ReturnsStopProcessing() { // Arrange ForbiddenAction action = new ForbiddenAction(); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(HttpStatusCode.Forbidden, context.StatusCode); Assert.AreEqual(RewriteProcessing.StopProcessing, result); }
public void Execute_WhenStatusCodeError_SetsStatusCode_ReturnsStopProcessing() { // Arrange HttpStatusCode code = HttpStatusCode.InternalServerError; SetStatusAction action = new SetStatusAction(code); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.StopProcessing, result); Assert.AreEqual(code, context.StatusCode); }
public void Execute_WhenStatusCodeAccepted_SetsStatusCode_ReturnsContinueProcessing() { // Arrange HttpStatusCode code = HttpStatusCode.Accepted; SetStatusAction action = new SetStatusAction(code); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); Assert.AreEqual(code, context.StatusCode); }
public void Execute_WhenConditionAndAction_ReturnsExpectedResult() { // Arrange ConditionalAction action = new ConditionalAction(); IRewriteContext context = new MockRewriteContext(); action.Conditions.Add(new MockRewriteCondition(true)); action.Actions.Add(new MockRewriteAction(RewriteProcessing.ContinueProcessing)); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); }
public void Execute_WhenMissingAppSetting_SetsPropertyToEmptyString_ReturnsContinueProcessing() { // Arrange string propertyName = "PropertyName"; string appSettingKey = "MissingAppSettingKey"; SetAppSettingPropertyAction action = new SetAppSettingPropertyAction(propertyName, appSettingKey); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); CollectionAssert.Contains(context.Properties.Keys, propertyName); Assert.AreEqual(String.Empty, context.Properties[propertyName]); }
public void Execute_SetsCookie_ReturnsContinueProcessing() { // Arrange string cookieName = "CookieName"; string cookieValue = "CookieValue"; SetCookieAction action = new SetCookieAction(cookieName, cookieValue); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); CollectionAssert.Contains(context.ResponseCookies.Keys, cookieName); Assert.AreEqual(cookieValue, context.ResponseCookies[cookieName].Value); }
public void Execute_SetsProperty_ReturnsContinueProcessing() { // Arrange string propertyName = "PropertyName"; string propertyValue = "PropertyValue"; SetPropertyAction action = new SetPropertyAction(propertyName, propertyValue); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); CollectionAssert.Contains(context.Properties.Keys, propertyName); Assert.AreEqual(propertyValue, context.Properties[propertyName]); }
public void Execute_SetsResponseHeader_ReturnsContinueProcessing() { // Arrange string header = "HeaderName"; string value = "HeaderValue"; IRewriteContext context = new MockRewriteContext(); AddHeaderAction action = new AddHeaderAction(header, value); // Act RewriteProcessing result = action.Execute(context); // Assert CollectionAssert.Contains(context.ResponseHeaders.Keys, header); Assert.AreEqual(value, context.ResponseHeaders[header]); Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); }
public void Execute_SetsLocation_ReturnsCorrectValue() { // Arrange string location = "/NewLocation"; RewriteProcessing processing = RewriteProcessing.RestartProcessing; RewriteAction action = new RewriteAction(location, processing); action.Conditions.Add(new MockRewriteCondition(true)); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(processing, result); Assert.AreEqual(location, context.Location); }
public void Execute_WhenTemporary_SetsStatusCodeAndLocation_ReturnsStopProcessing() { // Arrange string location = "/NewLocation"; bool permanent = false; RedirectAction action = new RedirectAction(location, permanent); action.Conditions.Add(new MockRewriteCondition(true)); IRewriteContext context = new MockRewriteContext(); // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.StopProcessing, result); Assert.AreEqual(HttpStatusCode.Found, context.StatusCode); Assert.AreEqual(location, context.Location); }
private void ProcessRules(RewriteContext context) { const int MaxRestart = 10; // Controls the number of restarts so we don't get into an infinite loop IList rewriteRules = _configuration.Rules; int restarts = 0; for (int i = 0; i < rewriteRules.Count; i++) { // If the rule is conditional, ensure the conditions are met. IRewriteCondition condition = rewriteRules[i] as IRewriteCondition; if (condition == null || condition.IsMatch(context)) { // Execute the action. IRewriteAction action = rewriteRules[i] as IRewriteAction; RewriteProcessing processing = action.Execute(context); //if (!TygaSoft.TygaSoftRuntime.ValidateRuntime()) //{ // context.Location = "~/NotAccess.aspx"; // break; //} // If the action is Stop, then break out of the processing loop if (processing == RewriteProcessing.StopProcessing) { _configuration.Logger.Debug(MessageProvider.FormatString(Message.StoppingBecauseOfRule)); break; } else if (processing == RewriteProcessing.RestartProcessing) { _configuration.Logger.Debug(MessageProvider.FormatString(Message.RestartingBecauseOfRule)); // Restart from the first rule. i = 0; if (++restarts > MaxRestart) { throw new InvalidOperationException(MessageProvider.FormatString(Message.TooManyRestarts)); } } } } }
/// <summary> /// Executes the rule. /// </summary> /// <param name="context"></param> public virtual RewriteProcessing Execute(RewriteContext context) { // Execute the actions. for (int i = 0; i < Actions.Count; i++) { IRewriteCondition condition = Actions[i] as IRewriteCondition; if (condition == null || condition.IsMatch(context)) { IRewriteAction action = Actions[i] as IRewriteAction; RewriteProcessing processing = action.Execute(context); if (processing != RewriteProcessing.ContinueProcessing) { return(processing); } } } return(RewriteProcessing.ContinueProcessing); }
/// <summary> /// Parses the node. /// </summary> /// <param name="node">The node to parse.</param> /// <param name="config">The rewriter configuration.</param> /// <returns>The parsed action, or null if no action parsed.</returns> public override IRewriteAction Parse(XmlNode node, object config) { if (node == null) { throw new ArgumentNullException("node"); } if (config == null) { throw new ArgumentNullException("config"); } XmlNode toNode = node.Attributes[Constants.AttrTo]; if (toNode.Value == null) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.AttributeRequired, Constants.AttrTo), node); } XmlNode processingNode = node.Attributes[Constants.AttrProcessing]; RewriteProcessing processing = RewriteProcessing.ContinueProcessing; if (processingNode != null) { if (processingNode.Value == Constants.AttrValueRestart) { processing = RewriteProcessing.RestartProcessing; } else if (processingNode.Value == Constants.AttrValueStop) { processing = RewriteProcessing.StopProcessing; } else if (processingNode.Value != Constants.AttrValueContinue) { throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ValueOfProcessingAttribute, processingNode.Value, Constants.AttrValueContinue, Constants.AttrValueRestart, Constants.AttrValueStop), node); } } RewriteAction action = new RewriteAction(toNode.Value, processing); ParseConditions(node, action.Conditions, false, config); return(action); }
public void Execute_SetsPropertyAppSetting_ReturnsContinueProcessing() { // Arrange string propertyName = "PropertyName"; string appSettingKey = "AppSettingKey"; string appSettingValue = "AppSettingValue"; SetAppSettingPropertyAction action = new SetAppSettingPropertyAction(propertyName, appSettingKey); IRewriteContext context = new MockRewriteContext(); context.ConfigurationManager.AppSettings[appSettingKey] = appSettingValue; // Act RewriteProcessing result = action.Execute(context); // Assert Assert.AreEqual(RewriteProcessing.ContinueProcessing, result); CollectionAssert.Contains(context.Properties.Keys, propertyName); Assert.AreEqual(appSettingValue, context.Properties[propertyName]); }
private void ProcessRules(IRewriteContext context, IList <IRewriteAction> rewriteRules, int restarts) { foreach (IRewriteAction action in rewriteRules) { // If the rule is conditional, ensure the conditions are met. IRewriteCondition condition = action as IRewriteCondition; if (condition == null || condition.IsMatch(context)) { // Execute the action. RewriteProcessing processing = action.Execute(context); // If the action is Stop, then break out of the processing loop if (processing == RewriteProcessing.StopProcessing) { _configuration.Logger.Debug(MessageProvider.FormatString(Message.StoppingBecauseOfRule)); // Exit the loop. break; } // If the action is Restart, then start again. if (processing == RewriteProcessing.RestartProcessing) { _configuration.Logger.Debug(MessageProvider.FormatString(Message.RestartingBecauseOfRule)); // Increment the number of restarts and check that we have not exceeded our max. restarts++; if (restarts > MaxRestarts) { throw new InvalidOperationException(MessageProvider.FormatString(Message.TooManyRestarts)); } // Restart again from the first rule by calling this method recursively. ProcessRules(context, rewriteRules, restarts); // Exit the loop. break; } } } }
/// <summary> /// Parses the node. /// </summary> /// <param name="node">The node to parse.</param> /// <param name="config">The rewriter configuration.</param> /// <returns>The parsed action, or null if no action parsed.</returns> public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config) { if (node == null) { throw new ArgumentNullException("node"); } if (config == null) { throw new ArgumentNullException("config"); } string to = node.GetRequiredAttribute(Constants.AttrTo, true); RewriteProcessing processing = ParseProcessing(node); RewriteAction action = new RewriteAction(to, processing); ParseConditions(node, action.Conditions, false, config); return(action); }
/// <summary> /// Executes the rule. /// </summary> /// <param name="context">The rewrite context</param> public virtual RewriteProcessing Execute(IRewriteContext context) { if (context == null) { throw new ArgumentNullException("context"); } // Execute the actions. for (int i = 0; i < Actions.Count; i++) { IRewriteCondition condition = Actions[i] as IRewriteCondition; if (condition == null || condition.IsMatch(context)) { IRewriteAction action = Actions[i]; RewriteProcessing processing = action.Execute(context); if (processing != RewriteProcessing.ContinueProcessing) { return(processing); } } } return(RewriteProcessing.ContinueProcessing); }
/// <summary> /// Default constructor. /// </summary> /// <param name="location">The location to set.</param> /// <param name="processing">The processing directive.</param> public RewriteAction(string location, RewriteProcessing processing) : base(location) { _processing = processing; }
/// <summary> /// Constructor. /// </summary> /// <param name="result">The IRewriteAction.Execute result</param> public MockRewriteAction(RewriteProcessing result) { _result = result; }