Skip to content

Commit 772894c

Browse files
committed
nuget tests update, readme file update
1 parent ba48093 commit 772894c

File tree

5 files changed

+126
-27
lines changed

5 files changed

+126
-27
lines changed

README.md

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,27 @@ Bombardier version [1.2.5](https://github.com/codesenberg/bombardier/releases/ta
1515
## Sample
1616

1717
```csharp
18+
//Generate requests from previously stored JSON file:
19+
var content = File.ReadAllText("Assets/getPetById.json");
20+
var httpRequest = JsonConvert.DeserializeObject<IList<HttpRequest>>(content);
1821

19-
//Instantiate a Bombardier test generator, by specifying Bombardier options
22+
//Create bombardier tests generator
2023
var bombardierTestsGenerator = new BombardierTestsGenerator(options =>
2124
{
22-
//if your api endpoints you are testing are protected by Oauth2 access tokens
23-
options.AddOAuth2Token("eyJhbGciOiJSUzI1N....", AuthenticationType.Customer);
24-
//if your api endpoints are protected by simple "ApiKey: <apikey>" authentication header
25-
options.AddApiKey("myAPIKey123423456");
26-
//if your api endpoints are protected by basic authentication
27-
options.AddBasicAuthentication("username", "password");
2825
options.BombardierConcurrentUsers = 1;
2926
options.BombardierDuration = 1;
3027
options.BombardierTimeout = 30;
3128
options.BombardierUseHttp2 = true;
3229
});
33-
//Generate Bombardier Tests
34-
var bombardierTests = await bombardierTestsGenerator.Generate(requests);
30+
31+
//Generate bomardier tests
32+
var bombardierTests = await bombardierTestsGenerator.Generate(httpRequest);
3533

3634
//Run Bombardier Tests
37-
var bombardierTestsRunner = new BombardierTestsRunner(bombardierTests.ToList(),
38-
options =>
39-
{
40-
options.ObfuscateAuthenticationHeader = false;
41-
});
35+
var bombardierTestsRunner = new BombardierTestsRunner(bombardierTests.ToList(), options =>
36+
{
37+
options.ObfuscateAuthenticationHeader = true;
38+
});
4239
var bombardierResults = await bombardierTestsRunner.Run();
4340
```
4441

@@ -82,23 +79,61 @@ Use `AddApiKey` if your APIs are protected by simple API Key. Pass Api Key in th
8279
##### 1.3 AddBasicAuthentication
8380
Use `AddBasicAuthentication` if your APIs are protected by basic authentication. Pass username and password in the method. A basic `Authentication` HTTP header will be generated. This is not best practice, but it's here if you need it.
8481

85-
#### 2. Bombardier parameters
86-
87-
You can set 5 Bombardier properties:
82+
##### 1.4 AddReplacementValues
83+
When you use `AddReplacementValues` values those can set or replace URL and HTTP body parameters before executing the tests. Replacement values have precedence over the `example` values that are set in Swagger file.
8884

89-
- `BombardierConcurrentUsers`: How many concurrent users should be used in Bombardier tests.
90-
- `BombardierDuration`: How long the Bombardier tests should execute in seconds. Use this depending on the type of test you want to perform and should not be used with `BombardierRateLimit`.
91-
- `BombardierTimeout`: What is the Bombardier timeout to wait for the requests to finish.
92-
- `BombardierUseHttp2`: Use HTTP2?
93-
- `BombardierRateLimit`: Rate limit Bombardier tests per second. Use this depending on the type of test you want to perform and should not be used with `BombardierDuration`.
85+
#### 2. Bombardier parameters
9486

87+
You can also set those `BombardierGeneratorOptions` options:
88+
89+
- `BombardierConcurrentUsers`: How many concurrent users should be used in Bombardier tests. Default is `3`.
90+
- `BombardierDuration`: How long the Bombardier tests should execute in seconds. Use this depending on the type of test you want to perform and should not be used with `BombardierRateLimit`. Default is `30` seconds.
91+
- `BombardierTimeout`: What is the Bombardier timeout to wait for the requests to finish. Default is `30` seconds.
92+
- `BombardierUseHttp2`: Use HTTP2 protocol. Otherwise HTTP1 is used. By default this is set to `true`.
93+
- `BombardierRateLimit`: Rate limit Bombardier tests per second. Use this depending on the type of test you want to perform and should not be used with `BombardierDuration`. By default rate limit is not set.
94+
- `BombardierNumberOfTotalRequests`: Limit the test to run only certain amount of requests. By default total number of requests is not set.
95+
- `BombardierInsecure`: Instead of HTTPS use HTTP protocol. Default value is `false`.
96+
- `BombardierBodyContentType`: Force only certain HTTP Content type. By default is set to `application/json`.
97+
9598
#### 3. Obfuscate Auth tokens for Bombardier output
9699

97100
Output is obfuscated by default, but you can turn it off with `options.ObfuscateAuthenticationHeader = false;` in `BombardierTestsRunner` options.
98101

99102
## How to use
100103

101-
TO-DO
104+
In the sample code above we generate HTTP requests from previously generated object which was serialized to JSON.
105+
106+
If you use Swagger files, you need to check the `QAToolKit.Source.Swagger` NuGet package, where you can generate that object from the Swagger file.
107+
108+
Let's replace
109+
110+
```csharp
111+
//Generate requests from previously stored JSON file:
112+
var content = File.ReadAllText("Assets/getPetById.json");
113+
var httpRequest = JsonConvert.DeserializeObject<IList<HttpRequest>>(content);
114+
```
115+
116+
with
117+
118+
```csharp
119+
//Setup Swagger source
120+
var urlSource = new SwaggerUrlSource(options =>
121+
{
122+
options.AddBaseUrl(new Uri("https://qatoolkitapi.azurewebsites.net/"));
123+
options.AddRequestFilters(new RequestFilter()
124+
{
125+
EndpointNameWhitelist = new string[] { "GetAllBikes" }
126+
});
127+
options.UseSwaggerExampleValues = true;
128+
});
129+
130+
//Load requests object
131+
var requests = await urlSource.Load(new Uri[] {
132+
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v1/swagger.json")
133+
});
134+
```
135+
136+
in the sample code above. Check the [QAToolKit.Source.Swagger](https://github.com/qatoolkit/qatoolkit-source-swagger-net) library for more details.
102137

103138
## TO-DO
104139

src/QAToolKit.Engine.Bombardier.Test/BombardierOutputOptionsTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ public class BombardierOutputOptionsTests
77
[Fact]
88
public void BombardierOutputOptionsTrueTest_Successful()
99
{
10-
var options = new BombardierOutputOptions();
11-
options.ObfuscateAuthenticationHeader = true;
10+
var options = new BombardierOutputOptions
11+
{
12+
ObfuscateAuthenticationHeader = true
13+
};
1214

1315
Assert.True(options.ObfuscateAuthenticationHeader);
1416
}

src/QAToolKit.Engine.Bombardier.Test/BombardierTestsGeneratorTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,5 +390,67 @@ public async Task GenerateBombardierTestGetBikesExampleWithFilterTest_Successful
390390
Assert.Equal(HttpMethod.Get, bombardierTests.FirstOrDefault().Method);
391391
Assert.Equal("https://qatoolkitapi.azurewebsites.net/api/bicycles?bicycleType=1&api-version=2", bombardierTests.FirstOrDefault().Url.ToString());
392392
}
393+
394+
[Fact]
395+
public async Task GenerateBombardierTestGetBikesExampleWithFilterAndTotalRequestTest_Successfull()
396+
{
397+
var bombardierTestsGenerator = new BombardierTestsGenerator(options =>
398+
{
399+
options.AddReplacementValues(new ReplacementValue[] {
400+
new ReplacementValue(){
401+
Key = "api-version",
402+
Value = "1"
403+
},
404+
new ReplacementValue(){
405+
Key = "bicycleType",
406+
Value = "1"
407+
}
408+
});
409+
options.BombardierNumberOfTotalRequests = 10;
410+
});
411+
412+
var content = File.ReadAllText("Assets/GetAllBikes.json");
413+
var httpRequest = JsonConvert.DeserializeObject<IList<HttpRequest>>(content);
414+
415+
var bombardierTests = await bombardierTestsGenerator.Generate(httpRequest);
416+
417+
Assert.NotNull(bombardierTests);
418+
Assert.Single(bombardierTests);
419+
Assert.Contains($@" -m GET https://qatoolkitapi.azurewebsites.net/api/bicycles?bicycleType=1&api-version=1 -c 3 --http2 --timeout=30s --duration=5s --requests=10", bombardierTests.FirstOrDefault().Command);
420+
Assert.Equal(HttpMethod.Get, bombardierTests.FirstOrDefault().Method);
421+
Assert.Equal("https://qatoolkitapi.azurewebsites.net/api/bicycles?bicycleType=1&api-version=1", bombardierTests.FirstOrDefault().Url.ToString());
422+
}
423+
424+
[Fact]
425+
public async Task GenerateBombardierTestGetBikesExampleWithFilterAndTotalRequestInsecureTest_Successfull()
426+
{
427+
var bombardierTestsGenerator = new BombardierTestsGenerator(options =>
428+
{
429+
options.AddReplacementValues(new ReplacementValue[] {
430+
new ReplacementValue(){
431+
Key = "api-version",
432+
Value = "2"
433+
},
434+
new ReplacementValue(){
435+
Key = "bicycleType",
436+
Value = "1"
437+
}
438+
});
439+
options.BombardierNumberOfTotalRequests = 10;
440+
options.BombardierInsecure = true;
441+
options.BombardierUseHttp2 = false;
442+
});
443+
444+
var content = File.ReadAllText("Assets/GetAllBikes.json");
445+
var httpRequest = JsonConvert.DeserializeObject<IList<HttpRequest>>(content);
446+
447+
var bombardierTests = await bombardierTestsGenerator.Generate(httpRequest);
448+
449+
Assert.NotNull(bombardierTests);
450+
Assert.Single(bombardierTests);
451+
Assert.Contains($@" -m GET https://qatoolkitapi.azurewebsites.net/api/bicycles?bicycleType=1&api-version=2 -c 3 --http1 --timeout=30s --duration=5s --insecure --requests=10", bombardierTests.FirstOrDefault().Command);
452+
Assert.Equal(HttpMethod.Get, bombardierTests.FirstOrDefault().Method);
453+
Assert.Equal("https://qatoolkitapi.azurewebsites.net/api/bicycles?bicycleType=1&api-version=2", bombardierTests.FirstOrDefault().Url.ToString());
454+
}
393455
}
394456
}

src/QAToolKit.Engine.Bombardier/BombardierGeneratorOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class BombardierGeneratorOptions
4141
/// <summary>
4242
/// Bombardier rate limiting per second
4343
/// </summary>
44-
public int BombardierRateLimit { get; set; }
44+
public int? BombardierRateLimit { get; set; } = null;
4545
/// <summary>
4646
/// Bombardier use HTTP2 protocol
4747
/// </summary>

src/QAToolKit.Engine.Bombardier/Helpers/BombardierSwitchGeneratorHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal static string GenerateTotalRequestsSwitch(BombardierGeneratorOptions bo
4545
/// <returns></returns>
4646
internal static string GenerateRateLimitSwitch(BombardierGeneratorOptions bombardierOptions)
4747
{
48-
if (bombardierOptions.BombardierRateLimit > 0)
48+
if (bombardierOptions.BombardierRateLimit != null)
4949
{
5050
return $" --rate={bombardierOptions.BombardierRateLimit}";
5151
}

0 commit comments

Comments
 (0)