Created
July 9, 2015 19:57
-
-
Save angelovstanton/973b9a12193113d88ff4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedirectService : IDisposable | |
{ | |
readonly IRedirectStrategy redirectEngine; | |
private Sites sites; | |
public RedirectService(IRedirectStrategy redirectEngine) | |
{ | |
this.redirectEngine = redirectEngine; | |
this.redirectEngine.Initialize(); | |
this.InitializeRedirectUrls(); | |
} | |
public void TestRedirects() | |
{ | |
bool shouldFail = false; | |
foreach (var currentSite in this.sites.Site) | |
{ | |
Uri baseUri = new Uri(currentSite.Url); | |
foreach (var currentRedirect in currentSite.Redirects.Redirect) | |
{ | |
Uri currentFromUrl = new Uri(baseUri, currentRedirect.From); | |
Uri currentToUrl = new Uri(baseUri, currentRedirect.To); | |
string currentSitesUrl = this.redirectEngine.NavigateToFromUrl(currentFromUrl.AbsoluteUri); | |
try | |
{ | |
Assert.AreEqual<string>(currentToUrl.AbsoluteUri, currentSitesUrl); | |
Console.WriteLine(string.Format("{0} \n OK", currentFromUrl)); | |
} | |
catch (Exception) | |
{ | |
shouldFail = true; | |
Console.WriteLine(string.Format("{0} \n was NOT redirected to \n {1}", currentFromUrl, currentToUrl)); | |
} | |
} | |
} | |
if (shouldFail) | |
{ | |
throw new Exception("There were incorrect redirects!"); | |
} | |
} | |
public void Dispose() | |
{ | |
redirectEngine.Dispose(); | |
} | |
private void InitializeRedirectUrls() | |
{ | |
XmlSerializer deserializer = new XmlSerializer(typeof(Sites)); | |
TextReader reader = new StreamReader(@"redirect-URLs.xml"); | |
this.sites = (Sites)deserializer.Deserialize(reader); | |
reader.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment