Skip to content

Commit

Permalink
Update Rule “write-integration-tests-to-validate-your-web-links/rule” (
Browse files Browse the repository at this point in the history
…#8593)

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

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

* Update Rule “write-integration-tests-to-validate-your-web-links/rule”
  • Loading branch information
babakamyljanovssw authored May 24, 2024
1 parent d6b125c commit ed3428e
Showing 1 changed file with 45 additions and 51 deletions.
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;
}
}
```

0 comments on commit ed3428e

Please sign in to comment.