Skip to content

Commit 02115cd

Browse files
committed
Add basic library infrastructure and first extension methods.
1 parent 74b1830 commit 02115cd

9 files changed

+466
-0
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ indent_style = space
66
indent_size = 4
77
trim_trailing_whitespace = true
88

9+
[*.csproj]
10+
indent_size = 2
11+
912
[*.cs]
1013
dotnet_style_qualification_for_field = false:warning
1114
dotnet_style_qualification_for_property = false:warning

HttpClient.TestHelpers.sln

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29709.97
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4C8914F8-D732-462B-978E-3BB5DBE547D7}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClient.TestHelpers", "src\HttpClient.TestHelpers\HttpClient.TestHelpers.csproj", "{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{BBCED492-E92B-4FA8-A4A5-B5A76091F25E}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClient.TestHelpers.Tests", "test\HttpClient.TestHelpers.Tests\HttpClient.TestHelpers.Tests.csproj", "{70673E72-C346-4AC2-946D-D9F99816FC72}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{800147F1-758C-406D-AF75-CB20EB7CDB18}"
15+
ProjectSection(SolutionItems) = preProject
16+
.editorconfig = .editorconfig
17+
EndProjectSection
18+
EndProject
19+
Global
20+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
21+
Debug|Any CPU = Debug|Any CPU
22+
Debug|x64 = Debug|x64
23+
Debug|x86 = Debug|x86
24+
Release|Any CPU = Release|Any CPU
25+
Release|x64 = Release|x64
26+
Release|x86 = Release|x86
27+
EndGlobalSection
28+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
29+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Debug|x64.ActiveCfg = Debug|Any CPU
32+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Debug|x64.Build.0 = Debug|Any CPU
33+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Debug|x86.ActiveCfg = Debug|Any CPU
34+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Debug|x86.Build.0 = Debug|Any CPU
35+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Release|x64.ActiveCfg = Release|Any CPU
38+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Release|x64.Build.0 = Release|Any CPU
39+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Release|x86.ActiveCfg = Release|Any CPU
40+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3}.Release|x86.Build.0 = Release|Any CPU
41+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Debug|x64.ActiveCfg = Debug|Any CPU
44+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Debug|x64.Build.0 = Debug|Any CPU
45+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Debug|x86.ActiveCfg = Debug|Any CPU
46+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Debug|x86.Build.0 = Debug|Any CPU
47+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Release|Any CPU.ActiveCfg = Release|Any CPU
48+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Release|Any CPU.Build.0 = Release|Any CPU
49+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Release|x64.ActiveCfg = Release|Any CPU
50+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Release|x64.Build.0 = Release|Any CPU
51+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Release|x86.ActiveCfg = Release|Any CPU
52+
{70673E72-C346-4AC2-946D-D9F99816FC72}.Release|x86.Build.0 = Release|Any CPU
53+
EndGlobalSection
54+
GlobalSection(SolutionProperties) = preSolution
55+
HideSolutionNode = FALSE
56+
EndGlobalSection
57+
GlobalSection(NestedProjects) = preSolution
58+
{FD5111E1-2970-4DC4-84DD-E4966E17DCB3} = {4C8914F8-D732-462B-978E-3BB5DBE547D7}
59+
{70673E72-C346-4AC2-946D-D9F99816FC72} = {BBCED492-E92B-4FA8-A4A5-B5A76091F25E}
60+
EndGlobalSection
61+
GlobalSection(ExtensibilityGlobals) = postSolution
62+
SolutionGuid = {CD31CAB7-6661-4E80-9A70-BC8BA6B9B764}
63+
EndGlobalSection
64+
EndGlobal
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
8+
<Version>0.0.1</Version>
9+
<Authors>David Perfors</Authors>
10+
<Product />
11+
<Copyright>Copyright (c) 2020 David Perfors</Copyright>
12+
<Description>A simple library to make testing HttpClient and related classes easier.</Description>
13+
<PackageProjectUrl>https://github.com/dnperfors/httpclienttesthelpers</PackageProjectUrl>
14+
<RepositoryUrl>https://github.com/dnperfors/httpclienttesthelpers</RepositoryUrl>
15+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<None Include="..\..\LICENSE">
27+
<Pack>True</Pack>
28+
<PackagePath></PackagePath>
29+
</None>
30+
</ItemGroup>
31+
32+
</Project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
namespace HttpClient.TestHelpers
5+
{
6+
/// <summary>
7+
/// A set of static methods for checking values on a <see cref="HttpRequestMessage"/>.
8+
/// </summary>
9+
public static class HttpRequestMessageExtensions
10+
{
11+
/// <summary>
12+
/// Determines whether a specific HttpVersion is set on a request.
13+
/// </summary>
14+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct version on.</param>
15+
/// <param name="httpVersion">The expected version.</param>
16+
/// <returns>true when the HttpVersion matches; otherwise, false.</returns>
17+
public static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, Version httpVersion)
18+
{
19+
if (httpRequestMessage == null)
20+
{
21+
throw new ArgumentNullException(nameof(httpRequestMessage));
22+
}
23+
24+
if (httpVersion == null)
25+
{
26+
throw new ArgumentNullException(nameof(httpVersion));
27+
}
28+
29+
return httpRequestMessage.Version == httpVersion;
30+
}
31+
32+
/// <summary>
33+
/// Determines whether a specific HttpVersion is set on a request.
34+
/// </summary>
35+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct version on.</param>
36+
/// <param name="httpVersion">The expected version.</param>
37+
/// <returns>true when the HttpVersion matches; otherwise, false.</returns>
38+
public static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, string httpVersion)
39+
{
40+
if (httpRequestMessage == null)
41+
{
42+
throw new ArgumentNullException(nameof(httpRequestMessage));
43+
}
44+
45+
if (string.IsNullOrEmpty(httpVersion))
46+
{
47+
throw new ArgumentNullException(nameof(httpVersion));
48+
}
49+
50+
return httpRequestMessage.HasHttpVersion(new Version(httpVersion));
51+
}
52+
53+
/// <summary>
54+
/// Determines whether a specific HttpMethod is set on a request.
55+
/// </summary>
56+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
57+
/// <param name="httpMethod">The expected method.</param>
58+
/// <returns>true when the HttpMethod matches; otherwise, false.</returns>
59+
public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, HttpMethod httpMethod)
60+
{
61+
if (httpRequestMessage == null)
62+
{
63+
throw new ArgumentNullException(nameof(httpRequestMessage));
64+
}
65+
66+
if (httpMethod == null)
67+
{
68+
throw new ArgumentNullException(nameof(httpMethod));
69+
}
70+
71+
return httpRequestMessage.Method == httpMethod;
72+
}
73+
74+
/// <summary>
75+
/// Determines whether a specific HttpMethod is set on a request.
76+
/// </summary>
77+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
78+
/// <param name="httpMethod">The expected method.</param>
79+
/// <returns>true when the HttpMethod matches; otherwise, false.</returns>
80+
public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, string httpMethod)
81+
{
82+
if (httpRequestMessage == null)
83+
{
84+
throw new ArgumentNullException(nameof(httpRequestMessage));
85+
}
86+
87+
if (string.IsNullOrEmpty(httpMethod))
88+
{
89+
throw new ArgumentNullException(nameof(httpMethod));
90+
}
91+
92+
return httpRequestMessage.HasHttpMethod(new HttpMethod(httpMethod));
93+
}
94+
95+
/// <summary>
96+
/// Determines whether a specific header is set on a request.
97+
/// </summary>
98+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
99+
/// <param name="headerName">The name of the header to locate on the request.</param>
100+
/// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
101+
public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string headerName)
102+
{
103+
if (httpRequestMessage == null)
104+
{
105+
throw new ArgumentNullException(nameof(httpRequestMessage));
106+
}
107+
108+
if(string.IsNullOrEmpty(headerName))
109+
{
110+
throw new ArgumentNullException(nameof(headerName));
111+
}
112+
113+
return httpRequestMessage.Headers.Contains(headerName);
114+
}
115+
}
116+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*.cs]
2+
# CA1707: Identifiers should not contain underscores
3+
dotnet_diagnostic.CA1707.severity = none
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
16+
<PackageReference Include="xunit" Version="2.4.1" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
<PackageReference Include="coverlet.collector" Version="1.2.0">
22+
<PrivateAssets>all</PrivateAssets>
23+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
24+
</PackageReference>
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\..\src\HttpClient.TestHelpers\HttpClient.TestHelpers.csproj" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
using Xunit;
5+
6+
namespace HttpClient.TestHelpers.Tests
7+
{
8+
public partial class HttpRequestMessageExtensionsTests
9+
{
10+
#nullable disable
11+
[Fact]
12+
public void HasHeader_NullRequest_ThrowsArgumentNullException()
13+
{
14+
HttpRequestMessage sut = null;
15+
16+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasHeader("host"));
17+
Assert.Equal("httpRequestMessage", exception.ParamName);
18+
}
19+
20+
[Theory]
21+
[InlineData(null)]
22+
[InlineData("")]
23+
public void HasHeader_NullHeaderName_ThrowsArgumentNullException(string headerName)
24+
{
25+
using var sut = new HttpRequestMessage();
26+
27+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasHeader(headerName));
28+
Assert.Equal("headerName", exception.ParamName);
29+
}
30+
#nullable restore
31+
32+
[Fact]
33+
public void HasHeader_ExistingHeaderName_ReturnsTrue()
34+
{
35+
using var sut = new HttpRequestMessage();
36+
sut.Headers.Host = "example.com";
37+
38+
Assert.True(sut.HasHeader("Host"));
39+
}
40+
41+
[Fact]
42+
public void HasHeader_NotExistingHeaderName_ReturnsFalse()
43+
{
44+
using var sut = new HttpRequestMessage();
45+
46+
Assert.False(sut.HasHeader("Host"));
47+
}
48+
}
49+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
using Xunit;
5+
6+
namespace HttpClient.TestHelpers.Tests
7+
{
8+
public partial class HttpRequestMessageExtensionsTests
9+
{
10+
#nullable disable
11+
[Fact]
12+
public void HasHttpMethod_WithHttpMethod_NullRequest_ThrowsArgumentNullException()
13+
{
14+
HttpRequestMessage sut = null;
15+
16+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasHttpMethod(HttpMethod.Get));
17+
Assert.Equal("httpRequestMessage", exception.ParamName);
18+
}
19+
20+
[Fact]
21+
public void HasHttpMethod_WithString_NullRequest_ThrowsArgumentNullException()
22+
{
23+
HttpRequestMessage sut = null;
24+
25+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasHttpMethod("GET"));
26+
Assert.Equal("httpRequestMessage", exception.ParamName);
27+
}
28+
29+
[Fact]
30+
public void HasHttpMethod_WithHttpMethod_NullHttpMethod_ThrowsArgumentNullException()
31+
{
32+
using var sut = new HttpRequestMessage { Method = HttpMethod.Get };
33+
34+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasHttpMethod((HttpMethod)null));
35+
Assert.Equal("httpMethod", exception.ParamName);
36+
}
37+
38+
[Theory]
39+
[InlineData(null)]
40+
[InlineData("")]
41+
public void HasHttpMethod_WithString_NullHttpMethod_ThrowsArgumentNullException(string httpMethod)
42+
{
43+
using var sut = new HttpRequestMessage { Method = HttpMethod.Get };
44+
45+
var exception = Assert.Throws<ArgumentNullException>(() => sut.HasHttpMethod(httpMethod));
46+
Assert.Equal("httpMethod", exception.ParamName);
47+
}
48+
#nullable restore
49+
50+
[Fact]
51+
public void HasHttpMethod_WithHttpMethod_CorrectHttpMethod_ReturnsTrue()
52+
{
53+
using var sut = new HttpRequestMessage { Method = HttpMethod.Get };
54+
55+
Assert.True(sut.HasHttpMethod(HttpMethod.Get));
56+
}
57+
58+
[Theory]
59+
[InlineData("GET")]
60+
[InlineData("Get")]
61+
[InlineData("get")]
62+
public void HasHttpMethod_WithString_CorrectHttpMethod_ReturnsTrue(string httpMethod)
63+
{
64+
using var sut = new HttpRequestMessage { Method = HttpMethod.Get };
65+
66+
Assert.True(sut.HasHttpMethod(httpMethod));
67+
}
68+
69+
[Fact]
70+
public void HasHttpMethod_WithHttpMethod_IncorrectHttpMethod_ReturnsFalse()
71+
{
72+
using var sut = new HttpRequestMessage { Method = HttpMethod.Get };
73+
74+
Assert.False(sut.HasHttpMethod(HttpMethod.Patch));
75+
}
76+
77+
[Fact]
78+
public void HasHttpMethod_WithString_IncorrectHttpMethod_ReturnsFalse()
79+
{
80+
using var sut = new HttpRequestMessage { Method = HttpMethod.Get };
81+
82+
Assert.False(sut.HasHttpMethod("DELETE"));
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)