Skip to content

Commit 169ad23

Browse files
committed
Added implementation for different methods
1 parent b1fd69e commit 169ad23

4 files changed

Lines changed: 101 additions & 70 deletions

File tree

APITestingRunner/Config.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Config
1414
public required string CompareUrlBase { get; set; }
1515

1616
/// <summary>
17-
/// Relative path for the client
17+
/// Relative path for the client.
1818
/// </summary>
1919
public required string UrlPath { get; set; }
2020

@@ -24,7 +24,7 @@ public class Config
2424
public required string CompareUrlPath { get; set; }
2525

2626
/// <summary>
27-
/// Any query parameters api requires
27+
/// Any query parameters api requires.
2828
/// </summary>
2929
public required List<Param> UrlParam { get; set; }
3030
/// <summary>
@@ -38,7 +38,7 @@ public class Config
3838
public string? RequestBody { get; set; }
3939

4040
/// <summary>
41-
/// What type of HTTP verb to use
41+
/// What type of HTTP verb to use for the API call.
4242
/// </summary>
4343
public required RequestType RequestType { get; set; }
4444

@@ -58,7 +58,18 @@ public class Config
5858
/// </summary>
5959
public string? LogLocation { get; set; }
6060

61+
/// <summary>
62+
/// Database connection string to target your database source.
63+
/// </summary>
6164
public string? DBConnectionString { get; set; }
65+
66+
/// <summary>
67+
/// Query to generate the data to use for data generation for api arguments.
68+
/// </summary>
6269
public string? DBQuery { get; set; }
70+
71+
/// <summary>
72+
/// Database fields mapping to parameters
73+
/// </summary>
6374
public List<Param>? DBFields { get; set; }
6475
}

APITestingRunner/RequestType.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+

2+
public partial class ConfigurationManager
3+
{
4+
/// <summary>
5+
/// Contains request types that can be used to make the api call.
6+
/// </summary>
7+
public enum RequestType
8+
{
9+
GET = 1,
10+
POST = 2,
11+
PUT = 3,
12+
PATCH = 4,
13+
DELETE = 5,
14+
}
15+
}

APITestingRunner/StoreResultsOption.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
public partial class ConfigurationManager
44
{
5+
/// <summary>
6+
/// Options to to store the results for the response
7+
/// </summary>
58
public enum StoreResultsOption
69
{
710
/// <summary>
@@ -17,12 +20,4 @@ public enum StoreResultsOption
1720
/// </summary>
1821
FailuresOnly = 2,
1922
}
20-
21-
public enum RequestType
22-
{
23-
GET = 1,
24-
POST = 2,
25-
PUT = 3,
26-
DELETE = 4,
27-
}
2823
}

APITestingRunner/TestRunner.cs

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -136,90 +136,95 @@ private async Task MakeApiCorCollectionCall(HttpClient client, DataQueryResult i
136136

137137
private async Task MakeApiCorCollectionCall(HttpClient client, string url, DataQueryResult? item = null, string requestBody = "")
138138
{
139-
HttpResponseMessage response;
139+
HttpResponseMessage? response = null;
140140
try
141141
{
142+
142143
switch (_config.RequestType)
143144
{
144145
case ConfigurationManager.RequestType.GET:
145146

146-
if (!string.IsNullOrWhiteSpace(requestBody))
147+
HttpRequestMessage request = new()
147148
{
149+
Method = HttpMethod.Get,
150+
RequestUri = new Uri(url),
151+
Content = CreateRequestContent(requestBody)
152+
};
148153

149-
HttpRequestMessage request = new()
150-
{
151-
Method = HttpMethod.Get,
152-
RequestUri = new Uri(url),
153-
Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json /* or "application/json" in older versions */),
154-
};
155-
156-
response = await client.SendAsync(request);
157-
}
158-
else
159-
{
160-
response = await client.GetAsync(url);
161-
}
162-
163-
string content = await response.Content.ReadAsStringAsync();
154+
response = !string.IsNullOrWhiteSpace(requestBody) ? await client.SendAsync(request) : await client.GetAsync(url);
164155

156+
break;
157+
case ConfigurationManager.RequestType.POST:
165158

166-
if (_config.ConfigMode == ConfigurationManager.TesterConfigMode.APICompare)
167-
{
168-
List<string> compareList = new();
159+
response = await client.PostAsync(url, CreateRequestContent(requestBody));
160+
break;
161+
case ConfigurationManager.RequestType.PUT:
162+
response = await client.PutAsync(url, CreateRequestContent(requestBody));
163+
break;
164+
case ConfigurationManager.RequestType.PATCH:
165+
response = await client.PatchAsync(url, CreateRequestContent(requestBody));
166+
break;
167+
case ConfigurationManager.RequestType.DELETE:
168+
response = await client.DeleteAsync(url);
169+
break;
170+
default:
171+
_errors.Add("Unsupported request type");
172+
break;
173+
}
169174

170-
if (compareClient != null)
171-
{
172-
HttpResponseMessage compareResponse = await compareClient.GetAsync(compareUrl);
175+
if (response != null)
176+
{
177+
string content = await response.Content.ReadAsStringAsync();
173178

174-
string responseCompareContent = await response.Content.ReadAsStringAsync();
179+
if (_config.ConfigMode == ConfigurationManager.TesterConfigMode.APICompare)
180+
{
181+
List<string> compareList = new();
175182

183+
if (compareClient != null)
184+
{
185+
HttpResponseMessage compareResponse = await compareClient.GetAsync(compareUrl);
176186

177-
// compare status code
178-
if (response.StatusCode == compareResponse.StatusCode)
179-
{
180-
compareList.Add($"Status code SourceAPI: {response.StatusCode} CompareAPI: {response.StatusCode}");
181-
}
187+
string responseCompareContent = await response.Content.ReadAsStringAsync();
182188

183-
// compare content
184-
if (content != responseCompareContent)
185-
{
186-
compareList.Add("APIs content does not match");
187-
}
188189

189-
if (compareList.Count == 0)
190-
{
191-
Console.ForegroundColor = ConsoleColor.Green;
192-
Console.WriteLine($"Comparing API for {item?.RowId} success");
193-
}
194-
else
195-
{
196-
Console.Write($"Comparing API for {item?.RowId} Failed");
197-
foreach (string errorsInComparrison in compareList)
198-
{
199-
Console.ForegroundColor = ConsoleColor.Red;
200-
Console.WriteLine($"- {errorsInComparrison}");
201-
}
202-
}
190+
// compare status code
191+
if (response.StatusCode == compareResponse.StatusCode)
192+
{
193+
compareList.Add($"Status code SourceAPI: {response.StatusCode} CompareAPI: {response.StatusCode}");
194+
}
203195

204-
Console.ForegroundColor = ConsoleColor.White;
196+
// compare content
197+
if (content != responseCompareContent)
198+
{
199+
compareList.Add("APIs content does not match");
200+
}
205201

206-
_ = ProcessResultCapture(new ApiCallResult(compareResponse.StatusCode, responseCompareContent, compareResponse.Headers, url, item, compareResponse.IsSuccessStatusCode), true);
202+
if (compareList.Count == 0)
203+
{
204+
Console.ForegroundColor = ConsoleColor.Green;
205+
Console.WriteLine($"Comparing API for {item?.RowId} success");
207206
}
208207
else
209208
{
210-
_errors.Add("Failed to find configuration for compare API");
209+
Console.Write($"Comparing API for {item?.RowId} Failed");
210+
foreach (string errorsInComparrison in compareList)
211+
{
212+
Console.ForegroundColor = ConsoleColor.Red;
213+
Console.WriteLine($"- {errorsInComparrison}");
214+
}
211215
}
216+
217+
Console.ForegroundColor = ConsoleColor.White;
218+
219+
_ = ProcessResultCapture(new ApiCallResult(compareResponse.StatusCode, responseCompareContent, compareResponse.Headers, url, item, compareResponse.IsSuccessStatusCode), true);
212220
}
213221

214222
await ProcessResultCapture(new ApiCallResult(response.StatusCode, content, response.Headers, url, item, response.IsSuccessStatusCode));
215-
216-
break;
217-
case ConfigurationManager.RequestType.POST:
218-
case ConfigurationManager.RequestType.PUT:
219-
case ConfigurationManager.RequestType.DELETE:
220-
default:
221-
_errors.Add("Unsupported request type");
222-
break;
223+
}
224+
else
225+
{
226+
_errors.Add("Failed to find configuration for compare API");
227+
}
223228
}
224229
}
225230
catch (Exception ex)
@@ -228,6 +233,11 @@ private async Task MakeApiCorCollectionCall(HttpClient client, string url, DataQ
228233
}
229234
}
230235

236+
private static StringContent CreateRequestContent(string requestBody)
237+
{
238+
return new(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json);
239+
}
240+
231241
private async Task ProcessResultCapture(ApiCallResult apiCallResult, bool IsCompareFile = false)
232242
{
233243
string response = $"{apiCallResult.statusCode} - {apiCallResult.responseContent}";

0 commit comments

Comments
 (0)