Skip to content

Commit

Permalink
Merge pull request hmol#2 from ed-graham/feature/specify-HTTP-codes-t…
Browse files Browse the repository at this point in the history
…o-log

Added InterestingHttpStatusCodes key to app.config
  • Loading branch information
ed-graham authored Sep 13, 2018
2 parents 5ca2f87 + f2213a5 commit b2cda45
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HttpStatusCodeExtensionsTests
[TestCase(HttpStatusCode.OK ,"2xx,xX0", true, Description = "Multiple wildcard codes allowed")]
public void Will_match_a_single_code_exactly(HttpStatusCode givenCode, string givenConfig, bool expectedOutcome)
{
var result = givenCode.IsSuccess(givenConfig);
var result = givenCode.IsMatch(givenConfig);

Assert.That(result, Is.EqualTo(expectedOutcome));
}
Expand Down
3 changes: 2 additions & 1 deletion LinkCrawler/LinkCrawler/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<add key="CheckImages" value="true"/>
<add key="BaseUrl" value="https://github.com"/>
<add key="SuccessHttpStatusCodes" value="1xx,2xx,3xx"/>
<add key="InterestingHttpStatusCodes" value="4xx,9xx"/>
<!--explanation of regex below: http://regexr.com/3cqt9 -->
<add key="ValidUrlRegex" value="(^http[s]?:\/{2})|(^www)|(^\/{1,2})"/>
<add key="PrintSummary" value="false"/>
<add key="PrintSummary" value="true"/>
<add key="Csv.FilePath" value="C:\tmp\output.csv"/>
<add key="Csv.Overwrite" value="true"/>
<add key="Csv.Delimiter" value=";"/>
Expand Down
19 changes: 11 additions & 8 deletions LinkCrawler/LinkCrawler/LinkCrawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ public void CrawlForLinksInResponse(IResponseModel responseModel)

public void WriteOutput(IResponseModel responseModel)
{
if (!responseModel.IsSuccess)
if (responseModel.IsInteresting)
{
foreach (var output in Outputs)
if (!responseModel.IsSuccess)
{
output.WriteError(responseModel);
foreach (var output in Outputs)
{
output.WriteError(responseModel);
}
}
}
else if (!OnlyReportBrokenLinksToOutput)
{
foreach (var output in Outputs)
else if (!OnlyReportBrokenLinksToOutput)
{
output.WriteInfo(responseModel);
foreach (var output in Outputs)
{
output.WriteInfo(responseModel);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions LinkCrawler/LinkCrawler/Models/IResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IResponseModel
HttpStatusCode StatusCode { get; }
int StatusCodeNumber { get; }
bool IsSuccess { get; }
bool IsInteresting { get; }
bool ShouldCrawl { get; }
string ToString();
}
Expand Down
4 changes: 3 additions & 1 deletion LinkCrawler/LinkCrawler/Models/ResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ResponseModel : IResponseModel
public HttpStatusCode StatusCode { get; }
public int StatusCodeNumber { get { return (int)StatusCode; } }
public bool IsSuccess { get; }
public bool IsInteresting { get; }
public bool ShouldCrawl { get; }
public string ErrorMessage { get; }

Expand All @@ -26,6 +27,7 @@ public ResponseModel(IRestResponse restResponse, RequestModel requestModel, ISet
RequestedUrl = requestModel.Url;
Location = restResponse.GetHeaderByName("Location"); // returns null if no Location header present in the response
ErrorMessage = restResponse.ErrorMessage;
IsInteresting = settings.IsInteresting(StatusCode);

IsSuccess = settings.IsSuccess(StatusCode);
if (!IsSuccess)
Expand All @@ -49,7 +51,7 @@ public override string ToString()
}
else
{
if (StatusCodeNumber == 301 || StatusCodeNumber == 302)
if (!String.IsNullOrEmpty(Location))
{
return $"{StatusCodeNumber}\t{StatusCode}\t{RequestedUrl}{Environment.NewLine}\t->\t{Location}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ namespace LinkCrawler.Utils.Extensions
public static class HttpStatusCodeExtensions
{
/// <summary>
/// This method will determine if an HttpStatusCode represents a "success" or not
/// based on the configuration string you pass in.
/// This method will determine if an HttpStatusCode matches the expression in
/// the configuration string you pass in.
/// You can pass literal codes like 100, 200, 404
/// Or you can pass in simple patterns using "x"s as wildcards like: 1xx, xx4
/// </summary>
/// <param name="this">The HttpStatus code to use</param>
/// <param name="configuredCodes">CSV of HttpStatus codes</param>
/// <returns></returns>
public static bool IsSuccess(this HttpStatusCode @this, string configuredCodes)
public static bool IsMatch(this HttpStatusCode @this, string configuredCodes)
{
// if * is passed in, return true (helpful if app.config setting is missing)
if (configuredCodes.Equals("*")) return true;

var codeCollection = configuredCodes
.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries) // split into array
.Select(c => c.Trim().ToLower()) // trim off any spaces, and make lowercase (this allows for "100,20X" and "100, 20x)"
Expand Down
1 change: 1 addition & 0 deletions LinkCrawler/LinkCrawler/Utils/Settings/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class AppSettings
public const string CsvOverwrite = "Csv.Overwrite";
public const string CsvDelimiter = "Csv.Delimiter";
public const string SuccessHttpStatusCodes = "SuccessHttpStatusCodes";
public const string InterestingHttpStatusCodes = "InterestingHttpStatusCodes";
public const string OutputProviders = "outputProviders";
public const string PrintSummary = "PrintSummary";
}
Expand Down
2 changes: 2 additions & 0 deletions LinkCrawler/LinkCrawler/Utils/Settings/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface ISettings

bool IsSuccess(HttpStatusCode statusCode);

bool IsInteresting(HttpStatusCode statusCode);

bool PrintSummary { get; }
}
}
7 changes: 6 additions & 1 deletion LinkCrawler/LinkCrawler/Utils/Settings/MockSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public string SlackWebHookUrl
public string ValidUrlRegex => @"(^http[s]?:\/{2})|(^www)|(^\/{1,2})";

public bool IsSuccess(HttpStatusCode statusCode) {
return statusCode.IsSuccess("1xx,2xx,3xx");
return statusCode.IsMatch("1xx,2xx,3xx");
}

public bool IsInteresting(HttpStatusCode statusCode)
{
return statusCode.IsMatch("3xx");
}

public MockSettings(bool includeWebHookUrl) {
Expand Down
8 changes: 7 additions & 1 deletion LinkCrawler/LinkCrawler/Utils/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public class Settings : ISettings
public bool IsSuccess(HttpStatusCode statusCode)
{
var configuredCodes = ConfigurationManager.AppSettings[Constants.AppSettings.SuccessHttpStatusCodes] ?? "";
return statusCode.IsSuccess(configuredCodes);
return statusCode.IsMatch(configuredCodes);
}

public bool IsInteresting(HttpStatusCode statusCode)
{
var configuredCodes = ConfigurationManager.AppSettings[Constants.AppSettings.InterestingHttpStatusCodes] ?? "*";
return statusCode.IsMatch(configuredCodes);
}
}
}

0 comments on commit b2cda45

Please sign in to comment.