Skip to content

Commit b1fd69e

Browse files
committed
Start of request bodiy work
1 parent 0c3a760 commit b1fd69e

8 files changed

Lines changed: 126 additions & 6 deletions

File tree

APITestingRunner.Unit.Tests/DataAccessTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void TestInit()
2424
HeaderParam = new List<Param> {
2525
new Param("accept","application/json")
2626
},
27+
RequestBody = null,
2728
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
2829
DBQuery = "select id as sqlId from dbo.sampleTable;",
2930
DBFields = new List<Param>

APITestingRunner.Unit.Tests/DataRequestTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public void TestInit()
2424
HeaderParam = new List<Param> {
2525
new Param("accept","application/json")
2626
},
27+
RequestBody = null,
2728
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
2829
DBQuery = "select id as sqlId from dbo.sampleTable;",
2930
DBFields = new List<Param>

APITestingRunner/Config.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Config
2121
/// <summary>
2222
/// Contains a compare url path. Allows user to point to the same api under different name.
2323
/// </summary>
24-
public string CompareUrlPath { get; set; }
24+
public required string CompareUrlPath { get; set; }
2525

2626
/// <summary>
2727
/// Any query parameters api requires
@@ -32,6 +32,11 @@ public class Config
3232
/// </summary>
3333
public required List<Param> HeaderParam { get; set; }
3434

35+
/// <summary>
36+
/// Request body optional. When present the replace values will apply the same way like for url.
37+
/// </summary>
38+
public string? RequestBody { get; set; }
39+
3540
/// <summary>
3641
/// What type of HTTP verb to use
3742
/// </summary>

APITestingRunner/Program.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,21 @@ private static async Task Main(string[] args)
101101
//await IndividualActions.RunTests(pathConfigJson);
102102
//Console.WriteLine("======================");
103103

104+
//Console.ReadLine();
105+
106+
//Console.WriteLine("==========CreateConfigForDatabaseBasedAPIComparrisonCall============");
107+
108+
//CreateConfigForDatabaseBasedAPIComparrisonCall(pathConfigJson, StoreResultsOption.None);
109+
110+
//await IndividualActions.RunTests(pathConfigJson);
111+
112+
//Console.WriteLine("======================");
113+
104114
Console.ReadLine();
105115

106116
Console.WriteLine("==========CreateConfigForDatabaseBasedAPIComparrisonCall============");
107117

108-
CreateConfigForDatabaseBasedAPIComparrisonCall(pathConfigJson, StoreResultsOption.None);
118+
CreateConfigForSingleAPICallWithUrlParamAndBodyModel(pathConfigJson);
109119

110120
await IndividualActions.RunTests(pathConfigJson);
111121

@@ -126,6 +136,7 @@ private static void CreateConfigForSingleAPICall(string pathConfigJson)
126136
CompareUrlPath = string.Empty,
127137
UrlPath = "/WeatherForecast",
128138
UrlParam = null,
139+
RequestBody = null,
129140

130141
HeaderParam = new List<Param> {
131142
new Param("accept","application/json")
@@ -157,6 +168,7 @@ private static void CreateConfigForSingleAPICallWithUrlParam(string pathConfigJs
157168
HeaderParam = new List<Param> {
158169
new Param("accept","application/json")
159170
},
171+
RequestBody = null,
160172
DBConnectionString = null,
161173
DBQuery = null,
162174
DBFields = null,
@@ -186,6 +198,7 @@ private static void CreateConfigForDatabaseBasedAPICall(string pathConfigJson, S
186198
HeaderParam = new List<Param> {
187199
new Param("accept","application/json")
188200
},
201+
RequestBody = null,
189202
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
190203
DBQuery = "select id as sqlId from dbo.sampleTable;",
191204
DBFields = new List<Param>
@@ -217,6 +230,7 @@ private static void CreateConfigForDatabaseBasedAPICallWithFailures(string pathC
217230
HeaderParam = new List<Param> {
218231
new Param("accept","application/json")
219232
},
233+
RequestBody = null,
220234
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
221235
DBQuery = "select id as sqlId from dbo.sampleTable;",
222236
DBFields = new List<Param>
@@ -248,6 +262,7 @@ private static void CreateConfigForDatabaseBasedAPIComparrisonCall(string pathCo
248262
HeaderParam = new List<Param> {
249263
new Param("accept","application/json")
250264
},
265+
RequestBody = null,
251266
DBConnectionString = "Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True;",
252267
DBQuery = "select id as sqlId from dbo.sampleTable;",
253268
DBFields = new List<Param>
@@ -262,6 +277,35 @@ private static void CreateConfigForDatabaseBasedAPIComparrisonCall(string pathCo
262277

263278
_ = IndividualActions.CreateConfig(DirectoryServices.AssemblyDirectory, pathConfigJson, config);
264279
}
280+
281+
282+
private static void CreateConfigForSingleAPICallWithUrlParamAndBodyModel(string pathConfigJson)
283+
{
284+
Config config = new()
285+
{
286+
UrlBase = "https://localhost:7055",
287+
CompareUrlBase = string.Empty,
288+
CompareUrlPath = string.Empty,
289+
UrlPath = "/datamodel/123456789",
290+
UrlParam = new List<Param>
291+
{
292+
new Param("location","UK")
293+
},
294+
HeaderParam = new List<Param> {
295+
new Param("accept","application/json")
296+
},
297+
RequestBody = "{Id={sqlId},StaticData=\"data\"}",
298+
DBConnectionString = null,
299+
DBQuery = null,
300+
DBFields = null,
301+
RequestType = RequestType.GET,
302+
ResultsStoreOption = StoreResultsOption.None,
303+
ConfigMode = TesterConfigMode.Run,
304+
LogLocation = DirectoryServices.AssemblyDirectory
305+
};
306+
307+
_ = IndividualActions.CreateConfig(DirectoryServices.AssemblyDirectory, pathConfigJson, config);
308+
}
265309
}
266310

267311
public class IndividualActions

APITestingRunner/TestRunner.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// See https://aka.ms/new-console-template for more information
22

3+
using System.Net.Mime;
4+
using System.Text;
5+
36
namespace APITestingRunner
47
{
58
internal class TestRunner
@@ -131,15 +134,31 @@ private async Task MakeApiCorCollectionCall(HttpClient client, DataQueryResult i
131134
return;
132135
}
133136

134-
private async Task MakeApiCorCollectionCall(HttpClient client, string url, DataQueryResult? item = null)
137+
private async Task MakeApiCorCollectionCall(HttpClient client, string url, DataQueryResult? item = null, string requestBody = "")
135138
{
139+
HttpResponseMessage response;
136140
try
137141
{
138142
switch (_config.RequestType)
139143
{
140144
case ConfigurationManager.RequestType.GET:
141145

142-
HttpResponseMessage response = await client.GetAsync(url);
146+
if (!string.IsNullOrWhiteSpace(requestBody))
147+
{
148+
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+
}
143162

144163
string content = await response.Content.ReadAsStringAsync();
145164

SampleAPI/Controllers/DataController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SampleAPI.Controllers
66
{
77
[ApiController]
88
[Route("[controller]")]
9-
public class DataController : ControllerBase
9+
public partial class DataController : ControllerBase
1010
{
1111
private readonly ILogger<DataController> _logger;
1212
public DataController(ILogger<DataController> logger)
@@ -30,4 +30,4 @@ public async Task<IActionResult> GetAsync(int id, string urlKey = "")
3030
return result == null ? NotFound() : Ok(result);
3131
}
3232
}
33-
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Dapper;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Data.SqlClient;
4+
using SampleAPI.Models;
5+
6+
namespace SampleAPI.Controllers
7+
{
8+
[ApiController]
9+
[Route("[controller]")]
10+
public partial class DataModelController : ControllerBase
11+
{
12+
private readonly ILogger<DataController> _logger;
13+
public DataModelController(ILogger<DataController> logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
19+
[HttpGet("/DataModel")]
20+
public async Task<IActionResult> GetFromBodyAsync([FromBody] RequestFromBodyModel model, [FromQuery] string urlKey = "")
21+
{
22+
if (string.IsNullOrEmpty(urlKey))
23+
{
24+
return BadRequest("Provide url key");
25+
}
26+
27+
if (string.IsNullOrWhiteSpace(model.StaticData))
28+
{
29+
return NotFound();
30+
}
31+
32+
string connectionString = $"Server=127.0.0.1; Database=test; User Id=sa; Password=<YourStrong@Passw0rd>;TrustServerCertificate=True";
33+
34+
using SqlConnection connection = new(connectionString);
35+
string sql = $"select * from dbo.sampleTable where id={model.Id};";
36+
SampleData result = await connection.QuerySingleAsync<SampleData>(sql);
37+
return result == null ? NotFound() : Ok(result);
38+
}
39+
}
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace SampleAPI.Models
4+
{
5+
public class RequestFromBodyModel
6+
{
7+
[Required] public int Id { get; set; }
8+
[Required] public required string StaticData { get; set; }
9+
}
10+
}

0 commit comments

Comments
 (0)