-
Notifications
You must be signed in to change notification settings - Fork 8
/
HttpClientExtensions.cs
128 lines (111 loc) · 5.98 KB
/
HttpClientExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using SystemTestingTools.Internal;
namespace SystemTestingTools
{
/// <summary>
/// extensions to HttpClient to allow stubbing and assertions
/// </summary>
public static class HttpClientExtensions
{
/// <summary>
/// Create a new session, so logs and requests and responses can be tracked
/// </summary>
/// <param name="httpClient"></param>
/// <returns>the SessionId, can be used to interact with other tools that might need a session</returns>
public static string CreateSession(this HttpClient httpClient)
{
var sessionId = Guid.NewGuid().ToString();
httpClient.DefaultRequestHeaders.Add(Constants.sessionHeaderName, sessionId);
Global.TestStubs.StubbedEndpoints.Add(sessionId, new List<StubEndpoint>());
Global.TestStubs.Logs.Add(sessionId, new List<LoggedEvent>());
Global.TestStubs.Events.Add(sessionId, new List<string>());
Global.TestStubs.OutgoingRequests.Add(sessionId, new List<HttpRequestMessage>());
return sessionId;
}
/// <summary>
/// Get Session ID allocated to httpClient, can be useful to interact with other tools
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
public static string GetSessionId(this HttpClient httpClient)
{
var values = httpClient.DefaultRequestHeaders.GetValues(Constants.sessionHeaderName);
if (values.Count() != 1) throw new ApplicationException("You need to call 'CreateSession' first");
return values.First();
}
/// <summary>
/// Will return the response when a matching call gets fired, but only once
/// if you expect this endpoint to be called X times, add X stub endpoints
/// </summary>
/// <param name="httpClient"></param>
/// <param name="httpMethod"></param>
/// <param name="Url"></param>
/// <param name="response">You can create your response, or use ResponseFactory to create one for you</param>
/// <param name="headerMatches">Optional headers that must match for the response to be returned</param>
/// <param name="counter">How many times should this response be returned, if requested one more time than the limit, it will throw an exception. choose 0 for infinite</param>
public static void AppendHttpCallStub(this HttpClient httpClient, HttpMethod httpMethod, Uri Url, HttpResponseMessage response, Dictionary<string, string> headerMatches = null, int counter = 1)
{
var sessionId = GetSessionId(httpClient);
Global.TestStubs.StubbedEndpoints[sessionId].Add(new StubEndpoint(httpMethod, Url, response, headerMatches, counter));
}
/// <summary>
/// Will throw an exception when a matching call gets fired, but only once
/// if you expect this endpoint to be called X times, add X stub endpoints
/// </summary>
/// <param name="httpClient"></param>
/// <param name="httpMethod"></param>
/// <param name="Url"></param>
/// <param name="exception">The exception that will be throw when HttpClient.SendAsync gets called</param>
/// <param name="headerMatches">Optional headers that must match for the response to be returned</param>
/// /// <param name="counter">How many times should this response be returned, if requested one more time than the limit, it will throw an exception. choose 0 for infinite</param>
public static void AppendHttpCallStub(this HttpClient httpClient, HttpMethod httpMethod, System.Uri Url, Exception exception, Dictionary<string, string> headerMatches = null, int counter = 1)
{
var sessionId = GetSessionId(httpClient);
Global.TestStubs.StubbedEndpoints[sessionId].Add(new StubEndpoint(httpMethod, Url, exception, headerMatches, counter));
}
/// <summary>
/// Get all logs related to the current session
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
public static List<LoggedEvent> GetSessionLogs(this HttpClient httpClient)
{
var sessionId = GetSessionId(httpClient);
return Global.TestStubs.Logs[sessionId];
}
/// <summary>
/// Get all events related to the current session
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
public static List<string> GetSessionEvents(this HttpClient httpClient)
{
var sessionId = GetSessionId(httpClient);
return Global.TestStubs.Events[sessionId];
}
/// <summary>
/// Get all outgoing Http requests related to the current session
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
public static List<HttpRequestMessage> GetSessionOutgoingRequests(this HttpClient httpClient)
{
if (!Global.KeepListOfSentRequests) return null;
var sessionId = GetSessionId(httpClient);
return Global.TestStubs.OutgoingRequests[sessionId];
}
[Obsolete("This call has been renamed to AppendHttpCallStub", true)]
public static void AppendMockHttpCall(this HttpClient httpClient, HttpMethod httpMethod, System.Uri Url, HttpResponseMessage response, Dictionary<string, string> headerMatches = null)
{
// old method, renamed it because Stub is a better word for what it does
}
[Obsolete("This call has been renamed to AppendHttpCallStub", true)]
public static void AppendMockHttpCall(this HttpClient httpClient, HttpMethod httpMethod, System.Uri Url, Exception exception, Dictionary<string, string> headerMatches = null)
{
// old method, renamed it because Stub is a better word for what it does
}
}
}