Skip to content

Commit e254a31

Browse files
committed
Initial commit of HttpClientMock
0 parents  commit e254a31

File tree

4 files changed

+382
-0
lines changed

4 files changed

+382
-0
lines changed

.gitignore

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
##
4+
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5+
6+
# Ignore automatic OpenAPI generated files and setup
7+
src/*/Swagger/
8+
dotnet-tools.json
9+
10+
# User-specific files
11+
*.suo
12+
*.user
13+
*.userosscache
14+
*.sln.docstates
15+
16+
# User-specific files (MonoDevelop/Xamarin Studio)
17+
*.userprefs
18+
19+
# User-specific files created by instructions in NOTES.txt (Helm)
20+
promitor-scrape-config.yaml
21+
22+
# Build results
23+
[Dd]ebug/
24+
[Dd]ebugPublic/
25+
[Rr]elease/
26+
x64/
27+
x86/
28+
bld/
29+
[Bb]in/
30+
[Oo]bj/
31+
[Ll]og/
32+
33+
# Environment specific configuration files
34+
appsettings.[Dd]evelopment.json
35+
36+
# Visual Studio 2015 cache/options directory
37+
.vs/
38+
# Uncomment if you have tasks that create the project's static files in wwwroot
39+
#wwwroot/
40+
41+
# MSTest test Results
42+
[Tt]est[Rr]esult*/
43+
[Bb]uild[Ll]og.*
44+
45+
# NUNIT
46+
*.VisualState.xml
47+
TestResult.xml
48+
49+
# Build Results of an ATL Project
50+
[Dd]ebugPS/
51+
[Rr]eleasePS/
52+
dlldata.c
53+
54+
# .NET Core
55+
project.lock.json
56+
project.fragment.lock.json
57+
artifacts/
58+
**/Properties/launchSettings.json
59+
60+
*_i.c
61+
*_p.c
62+
*_i.h
63+
*.ilk
64+
*.meta
65+
*.obj
66+
*.pch
67+
*.pdb
68+
*.pgc
69+
*.pgd
70+
*.rsp
71+
*.sbr
72+
*.tlb
73+
*.tli
74+
*.tlh
75+
*.tmp
76+
*.tmp_proj
77+
*.log
78+
*.vspscc
79+
*.vssscc
80+
.builds
81+
*.pidb
82+
*.svclog
83+
*.scc
84+
85+
# Chutzpah Test files
86+
_Chutzpah*
87+
88+
# Visual C++ cache files
89+
ipch/
90+
*.aps
91+
*.ncb
92+
*.opendb
93+
*.opensdf
94+
*.sdf
95+
*.cachefile
96+
*.VC.db
97+
*.VC.VC.opendb
98+
99+
# Visual Studio profiler
100+
*.psess
101+
*.vsp
102+
*.vspx
103+
*.sap
104+
105+
# TFS 2012 Local Workspace
106+
$tf/
107+
108+
# Guidance Automation Toolkit
109+
*.gpState
110+
111+
# ReSharper is a .NET coding add-in
112+
_ReSharper*/
113+
*.[Rr]e[Ss]harper
114+
*.DotSettings.user
115+
116+
# JustCode is a .NET coding add-in
117+
.JustCode
118+
119+
# TeamCity is a build add-in
120+
_TeamCity*
121+
122+
# DotCover is a Code Coverage Tool
123+
*.dotCover
124+
125+
# Visual Studio code coverage results
126+
*.coverage
127+
*.coveragexml
128+
129+
# NCrunch
130+
_NCrunch_*
131+
.*crunch*.local.xml
132+
nCrunchTemp_*
133+
134+
# MightyMoose
135+
*.mm.*
136+
AutoTest.Net/
137+
138+
# Web workbench (sass)
139+
.sass-cache/
140+
141+
# Installshield output folder
142+
[Ee]xpress/
143+
144+
# DocProject is a documentation generator add-in
145+
DocProject/buildhelp/
146+
DocProject/Help/*.HxT
147+
DocProject/Help/*.HxC
148+
DocProject/Help/*.hhc
149+
DocProject/Help/*.hhk
150+
DocProject/Help/*.hhp
151+
DocProject/Help/Html2
152+
DocProject/Help/html
153+
154+
# Click-Once directory
155+
publish/
156+
157+
# Publish Web Output
158+
*.[Pp]ublish.xml
159+
*.azurePubxml
160+
# TODO: Comment the next line if you want to checkin your web deploy settings
161+
# but database connection strings (with potential passwords) will be unencrypted
162+
*.pubxml
163+
*.publishproj
164+
165+
# Microsoft Azure Web App publish settings. Comment the next line if you want to
166+
# checkin your Azure Web App publish settings, but sensitive information contained
167+
# in these scripts will be unencrypted
168+
PublishScripts/
169+
170+
# NuGet Packages
171+
*.nupkg
172+
# The packages folder can be ignored because of Package Restore
173+
**/packages/*
174+
# except build/, which is used as an MSBuild target.
175+
!**/packages/build/
176+
# Uncomment if necessary however generally it will be regenerated when needed
177+
#!**/packages/repositories.config
178+
# NuGet v3's project.json files produces more ignorable files
179+
*.nuget.props
180+
*.nuget.targets
181+
182+
# Microsoft Azure Build Output
183+
csx/
184+
*.build.csdef
185+
186+
# Microsoft Azure Emulator
187+
ecf/
188+
rcf/
189+
190+
# Windows Store app package directories and files
191+
AppPackages/
192+
BundleArtifacts/
193+
Package.StoreAssociation.xml
194+
_pkginfo.txt
195+
196+
# Visual Studio cache files
197+
# files ending in .cache can be ignored
198+
*.[Cc]ache
199+
# but keep track of directories ending in .cache
200+
!*.[Cc]ache/
201+
202+
# Others
203+
ClientBin/
204+
~$*
205+
*~
206+
*.dbmdl
207+
*.dbproj.schemaview
208+
*.jfm
209+
*.pfx
210+
*.publishsettings
211+
orleans.codegen.cs
212+
213+
# Since there are multiple workflows, uncomment next line to ignore bower_components
214+
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
215+
#bower_components/
216+
217+
# RIA/Silverlight projects
218+
Generated_Code/
219+
220+
# Backup & report files from converting an old project file
221+
# to a newer Visual Studio version. Backup files are not needed,
222+
# because we have git ;-)
223+
_UpgradeReport_Files/
224+
Backup*/
225+
UpgradeLog*.XML
226+
UpgradeLog*.htm
227+
228+
# SQL Server files
229+
*.mdf
230+
*.ldf
231+
*.ndf
232+
233+
# Business Intelligence projects
234+
*.rdl.data
235+
*.bim.layout
236+
*.bim_*.settings
237+
238+
# Microsoft Fakes
239+
FakesAssemblies/
240+
241+
# GhostDoc plugin setting file
242+
*.GhostDoc.xml
243+
244+
# Node.js Tools for Visual Studio
245+
.ntvs_analysis.dat
246+
node_modules/
247+
248+
# Typescript v1 declaration files
249+
typings/
250+
251+
# Visual Studio 6 build log
252+
*.plg
253+
254+
# Visual Studio 6 workspace options file
255+
*.opt
256+
257+
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
258+
*.vbw
259+
260+
# Visual Studio LightSwitch build output
261+
**/*.HTMLClient/GeneratedArtifacts
262+
**/*.DesktopClient/GeneratedArtifacts
263+
**/*.DesktopClient/ModelManifest.xml
264+
**/*.Server/GeneratedArtifacts
265+
**/*.Server/ModelManifest.xml
266+
_Pvt_Extensions
267+
268+
# Paket dependency manager
269+
.paket/paket.exe
270+
paket-files/
271+
272+
# FAKE - F# Make
273+
.fake/
274+
275+
# JetBrains Rider
276+
.idea/
277+
*.sln.iml
278+
279+
# CodeRush
280+
.cr/
281+
282+
# Python Tools for Visual Studio (PTVS)
283+
__pycache__/
284+
*.pyc
285+
286+
# Cake - Uncomment if you are using it
287+
# tools/**
288+
# !tools/packages.config
289+
290+
# Telerik's JustMock configuration file
291+
*.jmconfig
292+
293+
# BizTalk build output
294+
*.btp.cs
295+
*.btm.cs
296+
*.odx.cs
297+
*.xsd.cs
298+
299+
# VS Code
300+
.vscode/*
301+
302+
#MAC
303+
.DS_Store
304+
305+
*.orig

src/HttpTestUtils/HttpTestUtils.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29920.165
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpTestUtils", "HttpTestUtils\HttpTestUtils.csproj", "{195D5F59-2550-400E-B68F-51F4782D184A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{195D5F59-2550-400E-B68F-51F4782D184A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{195D5F59-2550-400E-B68F-51F4782D184A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{195D5F59-2550-400E-B68F-51F4782D184A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{195D5F59-2550-400E-B68F-51F4782D184A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {50E8F34C-A757-41CE-A4E6-BBB548D0487B}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Runtime.InteropServices;
6+
using System.Runtime.InteropServices.ComTypes;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace HttpTestUtils
12+
{
13+
public static class HttpClientMock
14+
{
15+
public static HttpClient SetupHttpClientWithJsonResponse<T>(HttpStatusCode responseStatusCode, T responseBody)
16+
{
17+
var messageHandler = new TestHttpMessageHandler( _ => Task.FromResult(new HttpResponseMessage(responseStatusCode) { Content = new StringContent(JsonConvert.SerializeObject(responseBody), Encoding.UTF8, "application/json") }));
18+
19+
return new HttpClient(messageHandler);
20+
}
21+
22+
private class TestHttpMessageHandler : HttpMessageHandler
23+
{
24+
private readonly Func<HttpRequestMessage, Task<HttpResponseMessage>> _createResponseFunction;
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="TestHttpMessageHandler"/> class.
28+
/// </summary>
29+
public TestHttpMessageHandler(Func<HttpRequestMessage, Task<HttpResponseMessage>> createResponse)
30+
{
31+
_createResponseFunction = createResponse;
32+
}
33+
34+
/// <summary>Send an HTTP request as an asynchronous operation.</summary>
35+
/// <param name="request">The HTTP request message to send.</param>
36+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
37+
/// <returns>The task object representing the asynchronous operation.</returns>
38+
/// <exception cref="T:System.ArgumentNullException">The <paramref name="request">request</paramref> was null.</exception>
39+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
40+
{
41+
return await _createResponseFunction(request);
42+
}
43+
}
44+
}
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

0 commit comments

Comments
 (0)