Adding new website url validation rules using URI properties in .net

Job ID: 37000669

Budget: ₹750 – ₹1,250 INR

Need to add few rules to the exsiting code.



public void Define()
{
Flight flight = default;
Provider sourceRecord = default;
Provider resultRecord = default;

When()
.Match(() => flight)
.Match(() => sourceRecord, i => i.ProviderId == flight.ProviderId && i.Metadata.RecordType.ToLower() == "source" && !string.IsNullOrEmpty(i.Location.LocationEmail))
.Match(() => resultRecord, i => i.ProviderId == flight.ProviderId && i.Metadata.RecordType.ToLower() == "result" && !string.IsNullOrEmpty(i.Location.LocationWebSite))
.Having(() => CheckUrlMatches(sourceRecord.Location.LocationWebSite, resultRecord.Location.LocationWebSite));

Then()
.Do(ctx => ctx.Update(flight));
}

public bool CheckUrlMatches(string sourceUrl, string resultUrl)
{
if (!IsValidWebsite(sourceUrl) || !IsValidWebsite(resultUrl))
{
return false;
}

Uri.TryCreate(sourceUrl, UriKind.Absolute, out Uri sourceUri);
Uri.TryCreate(resultUrl, UriKind.Absolute, out Uri resultUri);

if (sourceUri == null || resultUri == null)
{
return false;
}

// Check if source and result domains match
if (!string.Equals(sourceUri.Host, resultUri.Host, StringComparison.OrdinalIgnoreCase))
{
// Check if source is blank and result is a valid URL
if (string.IsNullOrWhiteSpace(sourceUrl) && IsValidWebsite(resultUrl))
{
return true; // Updates in quest
}

return false; // Go to polaris
}

// Check if source and result URLs match except for scheme, host, and query
if (sourceUri.Scheme != resultUri.Scheme || sourceUri.PathAndQuery != resultUri.PathAndQuery)
{
return false;
}

// Check if source and result URLs match except for scheme, host, and extension
string sourceAdjustedValue = AdjustedUrlValue(sourceUrl);
string resultAdjustedValue = AdjustedUrlValue(resultUrl);

if (!string.Equals(sourceAdjustedValue, resultAdjustedValue, StringComparison.OrdinalIgnoreCase))
{
return false;
}

return true;
}

public string AdjustedUrlValue(string url)
{
string adjustedValue = url.ToLower().Replace("https://", "").Replace("http://", "").Replace("www.", "");

int indexOfLastPeriod = adjustedValue.LastIndexOf(".");

if (indexOfLastPeriod < 3 || adjustedValue.Substring(indexOfLastPeriod).Length < 2)
{
return adjustedValue;
}

return adjustedValue.Substring(0, indexOfLastPeriod);
}

public bool IsValidWebsite(string url)
{
Uri uri;
return Uri.TryCreate(url, UriKind.Absolute, out uri) &&
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
}
```
Need to verify above code works for all the below scenario


Whether this code checks below scenario Whether the source or result matches
whether the source or result value matches except beginning of the website such as (htpps://,http:// ,www.)
Whether the source or result value matches except domain extension like (.com,.org,.edu)
Usecase

Source Result ExceptedREsult
https//www.spine.com https://www.spine.com
www.medical.com ww.medical.com
htps:/www.spine.org https://www.spine.org
www.spine.com wwwspine.com
www.spine.com N/A
None
www.aa.org N/A
www.abc.com www.spine.com source and result domain doesn’t match go to polaris
www.spine.com Need to update in quest only when source is blanck and result is valid url
Related categories: PHP .NET C# Programming Software Architecture ASP.NET