Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rule “write-integration-tests-to-validate-your-web-links/rule” #8593

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 45 additions & 51 deletions rules/write-integration-tests-to-validate-your-web-links/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,64 @@ created: 2020-03-11T22:50:56.000Z
archivedreason: null
guid: dfa6be65-2251-442d-8a9e-ead15d505dce
---

If you store your URL references in the application settings, you can create integration tests to validate them.

<!--endintro-->

![Figure: URL for link stored in application settings](testURLSettings.gif)
![Figure: URL for link stored in application settings](testURLSettings.gif)

**Sample Code: How to test the URL**



```cs
[Test]
public void urlRulesToBetterInterfaces()
{
HttpStatusCode result = WebAccessTester.GetWebPageStatusCode(Settings.Default.urlRulesToBetterInterfaces);
Assert.IsTrue(result == HttpStatusCode.OK, result.ToString());
}
public void urlRulesToBetterInterfaces()
{
HttpStatusCode result = WebAccessTester.GetWebPageStatusCode(Settings.Default.urlRulesToBetterInterfaces);
Assert.IsTrue(result == HttpStatusCode.OK, result.ToString());
}
```



**Sample Code: Method used to verify the Page**



```cs
public class WebAccessTester
{

public static HttpStatusCode GetWebPageStatusCode(string url)
{
HttpWebRequest req = ((HttpWebRequest)(WebRequest.Create(url)));
req.Proxy = new WebProxy();
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse resp = null;
try
{
resp = ((HttpWebResponse)(req.GetResponse()));
if (resp.StatusCode == HttpStatusCode.OK)
{
if (url.ToLower().IndexOf("redirect") == -1 && url.ToLower().IndexOf(resp.ResponseUri.AbsolutePath.ToLower()) == -1)
{
return HttpStatusCode.NotFound;
}
}
}
catch (System.Exception ex)
{JavaScript
while (!(ex == null))
{
Console.WriteLine(ex.ToString());
Console.WriteLine("INNER EXCEPTION");
ex = ex.InnerException;
}
}
finally
{
if (!(resp == null))
{
resp.Close();
}
}
return resp.StatusCode;
}
}
{
public static HttpStatusCode GetWebPageStatusCode(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = new WebProxy();
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse resp = null;

try
{
resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
if (url.ToLower().IndexOf("redirect") == -1 && url.ToLower().IndexOf(resp.ResponseUri.AbsolutePath.ToLower()) == -1)
{
return HttpStatusCode.NotFound;
}
}
}
catch (System.Exception ex)
{
while (!(ex == null))
{
Console.WriteLine(ex.ToString());
Console.WriteLine("INNER EXCEPTION");
ex = ex.InnerException;
}
}
finally
{
if (!(resp == null))
{
resp.Close();
}
}

return resp.StatusCode;
}
}
```
Loading