public void RedirectLocalWithDifferentHostGoesToRootTest() { RedirectData data = new RedirectData() { RequestUrl = "http://foo" }; var context = GetContextForRedirectLocal(data); context.RedirectLocal("http://bar"); Assert.Equal("~/", data.RedirectUrl); }
public void Vortal_rewrites_alone_should_go_to_correct_domain(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl) { //Arrange var samples = new RandomSampleValues(); var factory = new RewriteFactory(samples); string[] lines = { @"RewriteCond %{HTTP:Host} ^(movers|vehicles|boats|motorcycles|freight)\.uship\.com$", @"RewriteRule ^/(.+)$ http://www.uship.com/%1/$1 [CL,NC,R=301]", @"", @"RewriteCond %{HTTP:Host} ^pets-livestock\.uship\.com$", @"RewriteRule ^/(.+)$ http://www.uship.com/pet-shipping/$1 [CL,NC,R=301]", @"", @"RewriteCond %{HTTP:Host} ^household-goods\.uship\.com$", @"RewriteRule ^/(.+)$ http://www.uship.com/furniture/$1 [CL,NC,R=301]", @"", @"# 301 these lortals to their vortals, these lortals will not exist", @"RewriteCond %{HTTP:Host} ^(craigslist|special-care|junk|food)(?:-agriculture)?\.uship\.com$", @"RewriteRule ^/(.+)$ http://www.uship.com/%1/ [CL,NC,R=301]" }; var lineNum = 1; var redirects = lines.Select(l => factory.Build(lineNum++, l)); var ruleSet = RewriteRuleSet.BuildRuleSets(redirects); var redirectData = new RedirectData(originalUrl); TestConditions(matchesCond, matchesRule, 1, expectedUrl, ruleSet, redirectData); }
public void RedirectLocalWithEmptyStringGoesToRootTest() { RedirectData data = new RedirectData() { RequestUrl = "http://foo" }; var context = GetContextForRedirectLocal(data); context.RedirectLocal(""); Assert.Equal("~/", data.RedirectUrl); }
public void Pricing_index_page_urls(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl) { //Arrange var samples = new RandomSampleValues(); var factory = new RewriteFactory(samples); string[] lines = { @"RewriteCond %{HTTP:Host} ^directory[2]?\.uship\.com$", @"RewriteRule ^/tips/showtip.aspx(.*)$ http://www.uship.com/tips/showtip.aspx$1 [NC,R=301]", @"", @"# ...", @"", @"# Listing Index", @"RewriteRule ^/pricing/((?:[a-z]|[-])+)/((?:[a-z]|[-])+)(?:/page/([0-9]+))? /listingindex/PricingCommodity.aspx?c=$1&c2=$2&page=$3 [NC,L]", @"RewriteRule ^/pricing/((?:[a-z]|[-])+)(?:/page/([0-9]+))? /listingindex/PricingCommodity.aspx?c=$1&page=$2 [NC,L]", @"RewriteRule ^/pricing/?$ /listingindex/?c=4&c2=79 [NC,L]" }; var lineNum = 1; var redirects = lines.Select(l => factory.Build(lineNum++, l)); var ruleSet = RewriteRuleSet.BuildRuleSets(redirects); var redirectData = new RedirectData(originalUrl); TestConditions(matchesCond, matchesRule, 3, expectedUrl, ruleSet, redirectData); }
private static HttpContextBase GetContextForRedirectLocal(RedirectData data) { Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>(); contextMock.Setup(context => context.Request.Url).Returns(new Uri(data.RequestUrl)); contextMock.Setup(context => context.Response.Redirect(It.IsAny<string>())).Callback((string url) => data.RedirectUrl = url); return contextMock.Object; }
public void RedirectLocalRelativeTest() { RedirectData data = new RedirectData() { RequestUrl = "http://foo" }; var context = GetContextForRedirectLocal(data); context.RedirectLocal("bar"); Assert.AreEqual("bar", data.RedirectUrl); context.RedirectLocal("bar/hey.you"); Assert.AreEqual("bar/hey.you", data.RedirectUrl); }
private static HttpContextBase GetContextForRedirectLocal(RedirectData data) { Mock <HttpContextBase> contextMock = new Mock <HttpContextBase>(); contextMock.Setup(context => context.Request.Url).Returns(new Uri(data.RequestUrl)); contextMock.Setup(context => context.Response.Redirect(It.IsAny <string>())).Callback((string url) => data.RedirectUrl = url); return(contextMock.Object); }
public void RedirectLocalOnSameHostTest() { RedirectData data = new RedirectData() { RequestUrl = "http://foo" }; var context = GetContextForRedirectLocal(data); context.RedirectLocal("http://foo/bar/baz"); Assert.AreEqual("http://foo/bar/baz", data.RedirectUrl); context.RedirectLocal("http://foo/bar/baz/woot.htm"); Assert.AreEqual("http://foo/bar/baz/woot.htm", data.RedirectUrl); }
//TODO all these tests could be moved into this... public static void BuildAndTestConditions(IEnumerable <string> lines, string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl, int?expectedRuleCount = null) { var samples = new RandomSampleValues(); var factory = new RewriteFactory(samples); var lineNum = 1; var redirects = lines.Select(l => factory.Build(lineNum++, l)); var ruleSet = RewriteRuleSet.BuildRuleSets(redirects); var redirectData = new RedirectData(originalUrl); TestConditions(matchesCond, matchesRule, null, expectedUrl, ruleSet, redirectData); }
public void RedirectLocalOnSameHostTest() { RedirectData data = new RedirectData() { RequestUrl = "http://foo" }; var context = GetContextForRedirectLocal(data); context.RedirectLocal("http://foo/bar/baz"); Assert.Equal("~/", data.RedirectUrl); context.RedirectLocal("http://foo/bar/baz/woot.htm"); Assert.Equal("~/", data.RedirectUrl); }
public void RedirectLocalRelativeTest() { RedirectData data = new RedirectData() { RequestUrl = "http://foo" }; var context = GetContextForRedirectLocal(data); context.RedirectLocal("/bar"); Assert.Equal("/bar", data.RedirectUrl); context.RedirectLocal("/bar/hey.you"); Assert.Equal("/bar/hey.you", data.RedirectUrl); }
public void TesRedirectData() { var redirectData = new RedirectData { RETURNMAC = "hello", RedirectURL = "world" }; string json = DefaultMarshaller.Instance.Marshal(redirectData); Assert.AreEqual("{\"RETURNMAC\":\"hello\",\"redirectURL\":\"world\"}", json); var unmarshalledRedirectData = DefaultMarshaller.Instance.Unmarshal <RedirectData>(json); Assert.AreEqual(redirectData.RETURNMAC, unmarshalledRedirectData.RETURNMAC); Assert.AreEqual(redirectData.RedirectURL, unmarshalledRedirectData.RedirectURL); }
public void Single_ruleset_should_object_work_as_expected(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl) { //Arrange var samples = new RandomSampleValues(); var factory = new RewriteFactory(samples); string[] lines = { @"RewriteCond %{HTTP:Host} ^(movers|household-goods|vehicles|boats|motorcycles|special-care|freight|pets-livestock|food-agriculture|junk|craigslist)[2]?\.uship\.com$", @"RewriteRule ^(?!.+\.axd|.+\.ashx|public/images|sticky/images)/([^?]*\u.*)/?(?:[^?]*\u.*)?$ /$1 [CL,R=301]" }; var lineNum = 1; var redirects = lines.Select(l => factory.Build(lineNum++, l)); var ruleSet = RewriteRuleSet.BuildRuleSets(redirects); var redirectData = new RedirectData(originalUrl); TestConditions(matchesCond, matchesRule, 1, expectedUrl, ruleSet, redirectData); }
public void Single_ruleset_should_manually_work_as_expected(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl) { //Arrange //TODO use structure map for getting the instance???: var samples = new RandomSampleValues(); var factory = new RewriteFactory(samples); string[] lines = { @"RewriteCond %{HTTP:Host} ^(movers|household-goods|vehicles|boats|motorcycles|special-care|freight|pets-livestock|food-agriculture|junk|craigslist)[2]?\.uship\.com$", @"RewriteRule ^(?!.+\.axd|.+\.ashx|public/images|sticky/images)/([^?]*\u.*)/?(?:[^?]*\u.*)?$ /$1 [CL,R=301]" }; var redirectData = new RedirectData(originalUrl); //Act var lineNum = 1; var redirects = lines.Select(l => factory.Build(lineNum++, l)); //Assert var redirectLines = redirects as IList <IRedirectLine> ?? redirects.ToList(); redirectLines.Count().Should().Be(2); var cond = (RewriteCondition)redirectLines.Single(x => x.LineType == RedirectLineType.Condition); cond.Variable.Should().Be("HTTP:Host"); cond.MatchesCondition(ref redirectData).Should().Be(matchesCond); if (matchesCond) { var rule = (RewriteRule)redirectLines.Single(x => x.LineType == RedirectLineType.Rule); redirectData = rule.ProcessRule(redirectData); if (matchesRule) { redirectData.Status.Should().NotBe(RedirectStatus.NotProcessed); redirectData.ProcessedUrl.Should().Be(expectedUrl); } else { redirectData.Status.Should().Be(RedirectStatus.NotProcessed); } } }
public void Https_test_cases(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl) { //Arrange var samples = new RandomSampleValues(); var factory = new RewriteFactory(samples); string[] lines = { @"# Force SSL for pages that request personally-identifiable information", @"RewriteCond %{HTTPS} ^(?!on).*$", @"RewriteCond %{SERVER_PORT} ^80$", @"RewriteCond %{HTTP:Host} (.*)", @"RewriteRule ^(/ltl-freight)(.*)$ https\://%1$1 [NC,R=301]", }; var lineNum = 1; var redirects = lines.Select(l => factory.Build(lineNum++, l)); var ruleSet = RewriteRuleSet.BuildRuleSets(redirects); var redirectData = new RedirectData(originalUrl); TestConditions(matchesCond, matchesRule, 1, expectedUrl, ruleSet, redirectData); }
public RedirectData TestUrl(string url) { if (RulesSets == null) { throw new Exception("Did you forget to call LoadConfig?"); } var data = new RedirectData(url); var matchesRuleSets = RulesSets.Where(r => r.ProcessConditions(ref data)); foreach (var rule in matchesRuleSets) { data = rule.ProcessRules(data); if (data.Status == RedirectStatus.Redirected) { break; } } return(data); }
public RedirectDataCollection TestUrlGetAllMatches(string url) { if (RulesSets == null) { throw new Exception("Did you forget to call LoadConfig?"); } var dataCollection = new RedirectDataCollection(url); RedirectData data = dataCollection; var matchesRuleSets = RulesSets.Where(r => r.ProcessConditions(ref data)); foreach (var rule in matchesRuleSets) { data = rule.ProcessRules(data); if (data.Status == RedirectStatus.Redirected || data.Status == RedirectStatus.Modified) { dataCollection.AddRuleSet(rule); } } return(dataCollection); }
//TODO would want this for unit tests to be easily loaded from a collection... public virtual string GetSampleValue(string variable, RedirectData data) { if (LookupDictionary != null && LookupDictionary.ContainsKey(variable)) { return(LookupDictionary[variable](data)); } switch (variable) { case "HTTP_TRUE_CLIENT_IP": return(GetRandom(IpExamples)); case "HTTP_HOST": case "HTTP:Host": return(data.OriginalUrl.Host); case "HTTP:User-Agent": return(GetRandom(UserAgentExamples)); case "REQUEST_METHOD": return("GET"); case "HTTPS": return(data.OriginalUrl.OriginalString.StartsWith("https") ? "ON" : "OFF"); case "SERVER_PORT": return(data.OriginalUrl.Port.ToString()); case "QUERY_STRING": return(data.OriginalUrl.Query); case "HTTP:Referer": return(GetRandom(ReferrerExamples)); case "REQUEST_URI": return(data.OriginalUrl.OriginalString); } throw new Exception("Unknown variable: " + variable); }
private static void TestConditions(bool matchesCond, bool matchesRule, int?expectedRuleCount, string expectedUrl, IEnumerable <RewriteRuleSet> ruleSet, RedirectData redirectData) { //Act var matchingSets = ruleSet.Where(r => r.ProcessConditions(ref redirectData)); var rewriteRuleSets = matchingSets as IList <RewriteRuleSet> ?? matchingSets.ToList(); //Assert if (matchesCond) { if (expectedRuleCount != null) { rewriteRuleSets.Count().Should().Be(expectedRuleCount); } //TODO this should be a method on RewriteRuleSetCollection... foreach (var rs in rewriteRuleSets) { redirectData = rs.ProcessRules(redirectData); if (redirectData.Status == RedirectStatus.Redirected) { break; } } if (matchesRule) { //TODO add an option for LAST RULE too? //redirectData.Status.Should().NotBe(RedirectStatus.NotProcessed); redirectData.WasRedirected.Should().BeTrue("Was NOT processed when it should have been"); redirectData.ProcessedUrl.Should().Be(expectedUrl); } else { //redirectData.Status.Should().Be(RedirectStatus.NotProcessed); redirectData.WasRedirected.Should().BeFalse("Was processed when it should NOT have been"); if (expectedUrl != null) { redirectData.ProcessedUrl.Should().Be(expectedUrl); } } } else { rewriteRuleSets.Count().Should().Be(0); } }
/// <summary> /// Loads the controls data from a previous request. /// </summary> /// <param name="savedState">The previous data object.</param> protected override void LoadControlState(object savedState) { using (BinaryReader reader = new BinaryReader(new MemoryStream((byte[])savedState))) { _data = new RedirectData(reader); LoadData(); } }
/// <summary> /// Initialise the controls. /// </summary> /// <param name="e"></param> protected override void OnInit(EventArgs e) { base.OnInit(e); if (DataSet != null) { Page.MaintainScrollPositionOnPostBack = true; _container.Controls.Add(_panelMessages); _panelMessages.Controls.Add(_literalMessage); _panelMessages.Controls.Add(_validationSummary); _container.Controls.Add(_panelBasic); _panelBasic.Controls.Add(_literalBasic); _panelBasic.Controls.Add(_tableBasic); AddTableRow(_panelEnabled, _checkBoxEnabled); AddTableRow(_panelFirstRequestOnly, _checkBoxFirstRequestOnly); AddTableRow(_panelOriginalUrlAsQueryString, _checkBoxOriginalUrlAsQueryString); AddTableRow(_panelTimeout, _textBoxTimeout); AddTableRowSingle(_panelMobileHomePageUrl, _textBoxMobileHomePageUrl); AddTableRowSingle(_panelMobilePagesRegex, _textBoxMobilePagesRegex); AddTableRowSingle(_panelDevicesFile, _textBoxDevicesFile); _container.Controls.Add(_panelLocations); _panelLocations.Controls.Add(_literalLocations); _container.Controls.Add(_panelButtons); _panelButtons.Controls.Add(_buttonUpdate); _panelButtons.Controls.Add(_buttonReset); _panelButtons.Controls.Add(_buttonAdd); _container.Controls.Add(_regularExpressionValidatorDevicesFile); _container.Controls.Add(_regularExpressionValidatorTimeout); _container.Controls.Add(_regularExpressionValidatorUrl); _container.Controls.Add(_customValidatorRegex); Page.RegisterRequiresControlState(this); if (!Page.IsPostBack) { _data = new RedirectData(FiftyOne.Foundation.Mobile.Configuration.Manager.Redirect); LoadData(); } _textBoxDevicesFile.TextChanged += new EventHandler(_textBoxDevicesFile_TextChanged); _checkBoxEnabled.CheckedChanged += new EventHandler(_checkBoxEnabled_CheckedChanged); _checkBoxFirstRequestOnly.CheckedChanged += new EventHandler(_checkBoxFirstRequestOnly_CheckedChanged); _textBoxMobileHomePageUrl.TextChanged += new EventHandler(_textBoxMobileHomePageUrl_TextChanged); _textBoxMobilePagesRegex.TextChanged += new EventHandler(_textBoxMobilePagesRegex_TextChanged); _checkBoxOriginalUrlAsQueryString.CheckedChanged += new EventHandler(_checkBoxOriginalUrlAsQueryString_CheckedChanged); _textBoxTimeout.TextChanged += new EventHandler(_textBoxTimeout_TextChanged); _buttonAdd.Click += new EventHandler(_buttonAdd_Click); _buttonReset.Click += new EventHandler(_buttonReset_Click); _buttonUpdate.Click += new EventHandler(_buttonUpdate_Click); _customValidatorRegex.ServerValidate += new ServerValidateEventHandler(_customValidatorRegex_ServerValidate); } }
public IRedirectData GetRedirectData(int campaignId) { var result = new RedirectData(); var campaign = _oenRepository.Retrieve<Campaign>().Where(c => c.campaign_id == campaignId).Select(c => new { CampaignId = c.campaign_id, ClientId = c.key_id, DeliveryGroupId = c.deliverygroup_id, TemplateId = c.template_id }).FirstOrDefault(); if (campaign == null) { throw new InvalidOperationException(string.Format("Campaign {0} doesn't exist", campaignId)); } if (!campaign.TemplateId.HasValue) { throw new InvalidOperationException(string.Format("Campaign {0} has no Template assigned", campaignId)); } var template = _oenRepository.Retrieve<Template>().Select(t => new { t.template_id, t.fromDomain }).FirstOrDefault(t => t.template_id == campaign.TemplateId); if (template == null) { throw new InvalidOperationException(string.Format("Template {0} assigned to Campaign {1} doesn't exist", campaign.TemplateId, campaignId)); } result.FromDomain = template.fromDomain; var deliveryGroupId = -1; if (campaign.DeliveryGroupId.HasValue) { deliveryGroupId = campaign.DeliveryGroupId.Value; } else { var clientDvg = _oenRepository.Retrieve<ClientDeliveryGroup>().Select(c => new { c.clientKey_id, c.deliveryGroup_id }).FirstOrDefault(c => c.clientKey_id == campaign.ClientId); if (clientDvg == null) { throw new InvalidOperationException(string.Format("Campaign {0} nor Client {1} have a DeliveryGroup Assigned", campaignId, campaign.ClientId)); } deliveryGroupId = clientDvg.deliveryGroup_id; } result.Prefix = (from dvg in _oenRepository.Retrieve<DeliveryGroup>() join ent in _oenRepository.Retrieve<Entity>() on dvg.entity_id equals ent.entity_id where dvg.deliveryGroup_id == deliveryGroupId select ent.tracking_prefix).FirstOrDefault(); if (string.IsNullOrWhiteSpace(result.Prefix)) { throw new InvalidOperationException(string.Format("Entity of DeliveryGroup {0} has no Tracking Prefix", deliveryGroupId)); } return result; }
private void _buttonReset_Click(object sender, EventArgs e) { _data = new RedirectData(FiftyOne.Foundation.Mobile.Configuration.Manager.Redirect); LoadData(); }
internal LocationsControl (RedirectData data, Redirect parent) { _data = data; _parent = parent; _panelName = new Panel(); _panelUrl = new Panel(); _panelMatchExpression = new Panel(); _panelFilters = new Panel(); _customValidatorUniqueName = new CustomValidator(); }