Skip to content

Commit e7ffd9b

Browse files
authored
Remove duplication by extracting logic to a separate method.
1 parent 30ca66b commit e7ffd9b

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/TestableHttpClient.NFluent/HttpResponseMessageChecks.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net;
45
using System.Net.Http;
@@ -118,23 +119,7 @@ public static class HttpResponseMessageChecks
118119
#pragma warning disable CS8602 // Dereference of a possibly null reference. Justification = "Null reference check is performed by the FailIfNull check"
119120
.CheckSutAttributes(sut => sut.Headers.Select(x => new Header(x.Key, x.Value)), "headers")
120121
#pragma warning restore CS8602 // Dereference of a possibly null reference.
121-
.FailWhen(sut =>
122-
{
123-
if (sut.Any(x => x.Name == expectedHeader))
124-
{
125-
var header = sut.First(x => x.Name == expectedHeader);
126-
if (StringMatcher.Matches(header.Value, expectedValue))
127-
{
128-
return false;
129-
130-
}
131-
}
132-
133-
return true;
134-
}, "The {0} does not contain the expected header.")
135-
.DefineExpectedResult(new Header(expectedHeader, expectedValue), "The expected header:", "The forbidden header:")
136-
.OnNegate("The {0} should not contain the forbidden header.")
137-
.EndCheck();
122+
.HasHeaderWithValue(expectedHeader, expectedValue);
138123

139124
return ExtensibilityHelper.BuildCheckLink(check);
140125
}
@@ -180,7 +165,14 @@ public static class HttpResponseMessageChecks
180165
#pragma warning restore CS8602 // Dereference of a possibly null reference.
181166
.FailIfNull()
182167
.CheckSutAttributes(sut => sut.Headers.Select(x => new Header(x.Key, x.Value)), "headers")
183-
.FailWhen(sut =>
168+
.HasHeaderWithValue(expectedHeader, expectedValue);
169+
170+
return ExtensibilityHelper.BuildCheckLink(check);
171+
}
172+
173+
private static void HasHeaderWithValue(this ICheckLogic<IEnumerable<Header>> checkLogic, string expectedHeader, string expectedValue)
174+
{
175+
checkLogic.FailWhen(sut =>
184176
{
185177
if (sut.Any(x => x.Name == expectedHeader))
186178
{
@@ -195,8 +187,6 @@ public static class HttpResponseMessageChecks
195187
.DefineExpectedResult(new Header(expectedHeader, expectedValue), "The expected header:", "The forbidden header:")
196188
.OnNegate("The {0} should not contain the forbidden header.")
197189
.EndCheck();
198-
199-
return ExtensibilityHelper.BuildCheckLink(check);
200190
}
201191

202192
/// <summary>

0 commit comments

Comments
 (0)