Skip to content

Commit da333ab

Browse files
committed
Fix sonar issues
1 parent 71d0ef2 commit da333ab

2 files changed

Lines changed: 41 additions & 51 deletions

File tree

Source/Mockolate/Web/ItExtensions.HttpContent.WithFormData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public void Exactly()
9292
)
9393
);
9494

95-
private IEnumerable<(string, string)> GetFormData(string rawContent)
95+
private static IEnumerable<(string, string)> GetFormData(string rawContent)
9696
{
9797
string rawFormData = ExtractFormDataFromMultipartContent(rawContent);
9898
return ParseFormDataParameters(rawFormData);
9999
}
100100

101-
private string ExtractFormDataFromMultipartContent(string rawContent)
101+
private static string ExtractFormDataFromMultipartContent(string rawContent)
102102
{
103103
string[] lines = rawContent.Split('\n');
104104
StringBuilder sb = new();

Source/Mockolate/Web/ItExtensions.HttpContent.cs

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -148,66 +148,56 @@ public void InvokeCallbacks(object? value)
148148
}
149149
}
150150

151-
#if NET8_0_OR_GREATER
152-
private static string GetStringFromHttpContent(HttpContent content, HttpRequestMessage? _)
153-
#else
154-
private static string GetStringFromHttpContent(HttpContent content, HttpRequestMessage? message)
155-
#endif
151+
private interface IContentMatcher
156152
{
157-
static Encoding GetEncodingFromCharset(string? charset)
153+
bool Matches(HttpContent content, HttpRequestMessage? message);
154+
}
155+
156+
private sealed class PredicateStringMatcher(Func<string, bool> predicate) : IContentMatcher
157+
{
158+
public bool Matches(HttpContent content, HttpRequestMessage? message)
158159
{
159-
if (!string.IsNullOrEmpty(charset))
160+
static Encoding GetEncodingFromCharset(string? charset)
160161
{
161-
try
162+
if (!string.IsNullOrEmpty(charset))
162163
{
163-
return Encoding.GetEncoding(charset);
164+
try
165+
{
166+
return Encoding.GetEncoding(charset);
167+
}
168+
catch (ArgumentException)
169+
{
170+
// If the charset is invalid or not supported, we fall back to the default encoding (UTF-8).
171+
}
164172
}
165-
catch (ArgumentException)
166-
{
167-
// If the charset is invalid or not supported, we fall back to the default encoding (UTF-8).
168-
}
169-
}
170173

171-
return Encoding.UTF8;
172-
}
174+
return Encoding.UTF8;
175+
}
173176

174-
string? charset = content.Headers.ContentType?.CharSet;
175-
Encoding encoding = GetEncodingFromCharset(charset);
177+
string? charset = content.Headers.ContentType?.CharSet;
178+
Encoding encoding = GetEncodingFromCharset(charset);
176179
#if NET8_0_OR_GREATER
177-
Stream stream = content.ReadAsStream();
178-
long position = stream.Position;
179-
using StreamReader reader = new(stream, encoding, leaveOpen: true);
180-
string stringContent = reader.ReadToEnd();
181-
stream.Position = position;
182-
#else
183-
string stringContent;
184-
if (message?.Properties.TryGetValue("Mockolate:HttpContent", out object value) == true
185-
&& value is byte[] bytes)
186-
{
187-
stringContent = encoding.GetString(bytes);
188-
}
189-
else
190-
{
191-
Stream stream = content.ReadAsStreamAsync().ConfigureAwait(false).GetAwaiter().GetResult();
180+
Stream stream = content.ReadAsStream();
192181
long position = stream.Position;
193-
using StreamReader reader = new(stream, encoding, true, 1024, true);
194-
stringContent = reader.ReadToEnd();
182+
using StreamReader reader = new(stream, encoding, leaveOpen: true);
183+
string stringContent = reader.ReadToEnd();
195184
stream.Position = position;
196-
}
185+
#else
186+
string stringContent;
187+
if (message?.Properties.TryGetValue("Mockolate:HttpContent", out object value) == true
188+
&& value is byte[] bytes)
189+
{
190+
stringContent = encoding.GetString(bytes);
191+
}
192+
else
193+
{
194+
Stream stream = content.ReadAsStreamAsync().ConfigureAwait(false).GetAwaiter().GetResult();
195+
long position = stream.Position;
196+
using StreamReader reader = new(stream, encoding, true, 1024, true);
197+
stringContent = reader.ReadToEnd();
198+
stream.Position = position;
199+
}
197200
#endif
198-
return stringContent;
199-
}
200-
201-
private interface IContentMatcher
202-
{
203-
bool Matches(HttpContent content, HttpRequestMessage? message);
204-
}
205-
206-
private sealed class PredicateStringMatcher(Func<string, bool> predicate) : IContentMatcher
207-
{
208-
public bool Matches(HttpContent content, HttpRequestMessage? message)
209-
{
210-
string stringContent = GetStringFromHttpContent(content, message);
211201
return predicate.Invoke(stringContent);
212202
}
213203
}

0 commit comments

Comments
 (0)