Skip to content

Commit

Permalink
added Http Builder API
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Nov 16, 2022
1 parent 174488e commit 7968124
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 37 deletions.
4 changes: 2 additions & 2 deletions examples/CSharp/CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NBomber.Http\NBomber.Http.fsproj"/>
<PackageReference Include="NBomber" Version="4.0.0-beta6" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NBomber" Version="4.0.0-beta2"/>
<ProjectReference Include="..\..\src\NBomber.Http\NBomber.Http.fsproj" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion examples/CSharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using NBomber.Contracts;

namespace CSharp
{
Expand Down
13 changes: 6 additions & 7 deletions examples/CSharp/SimpleExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ public void Run()

var scenario = Scenario.Create("http_scenario", async context =>
{
var request = new HttpRequestMessage(HttpMethod.Get, "https://nbomber.com");
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Accept", "text/html")
.WithBody(new StringContent("{ some JSON }"));
var response = await httpClient.SendAsync(request);
var response = await Http.Send(httpClient, request);
var dataSize = Http.GetRequestSize(request) + Http.GetResponseSize(response);
return response.IsSuccessStatusCode
? Response.Ok(statusCode: response.StatusCode.ToString(), sizeBytes: dataSize)
: Response.Fail(statusCode: response.StatusCode.ToString(), sizeBytes: dataSize);
return response;
})
.WithoutWarmUp()
.WithLoadSimulations(Simulation.InjectPerSec(100, TimeSpan.FromSeconds(30)));
Expand Down
12 changes: 6 additions & 6 deletions examples/FSharp/FSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="SequentialSteps.fs"/>
<Compile Include="SimpleExample.fs"/>
<Compile Include="Program.fs"/>
<Compile Include="SequentialSteps.fs" />
<Compile Include="SimpleExample.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NBomber.Http\NBomber.Http.fsproj"/>
<PackageReference Update="FSharp.Core" Version="6.0.5" />
<PackageReference Include="NBomber" Version="4.0.0-beta6" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="6.0.5"/>
<PackageReference Include="NBomber" Version="4.0.0-beta2"/>
<ProjectReference Include="..\..\src\NBomber.Http\NBomber.Http.fsproj" />
</ItemGroup>

</Project>
15 changes: 8 additions & 7 deletions examples/FSharp/SequentialSteps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ let run () =

Scenario.create("http_scenario", fun context -> task {

let! step1 = Step.run("step_1", context, fun () -> task {
let request = new HttpRequestMessage(HttpMethod.Get, "https://nbomber.com")
let! step1 = Step.run("step_1", context, fun _ -> task {

let! response = Http.send httpClient request
let! response =
Http.createRequest "GET" "https://nbomber.com"
|> Http.send httpClient

return response
})

let! step2 = Step.run("step_2", context, fun () -> task {
let request = new HttpRequestMessage(HttpMethod.Get, "https://nbomber.com")

let! response = Http.send httpClient request
let! step2 = Step.run("step_2", context, fun _ -> task {
let! response =
Http.createRequest "GET" "https://nbomber.com"
|> Http.send httpClient

return response
})
Expand Down
16 changes: 6 additions & 10 deletions examples/FSharp/SimpleExample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ let run () =

Scenario.create("http_scenario", fun context -> task {

let request = new HttpRequestMessage(HttpMethod.Get, "https://nbomber.com")
let! response =
Http.createRequest "GET" "https://nbomber.com"
|> Http.withHeader "Accept" "text/html"
|> Http.withBody (new StringContent("{ some JSON }"))
|> Http.send httpClient

let! response = httpClient.SendAsync request

let dataSize = Http.getRequestSize(request) + Http.getResponseSize(response)

return
if response.IsSuccessStatusCode then
Response.ok(statusCode = response.StatusCode.ToString(), sizeBytes = dataSize)
else
Response.fail(statusCode = response.StatusCode.ToString(), sizeBytes = dataSize)
return response
})
|> Scenario.withLoadSimulations [InjectPerSec(rate = 100, during = seconds 30)]
|> NBomberRunner.registerScenario
Expand Down
19 changes: 19 additions & 0 deletions src/NBomber.Http/CSharp.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace NBomber.Http.CSharp

open System.Net.Http
open System.Runtime.CompilerServices

type Http =

static member CreateRequest (method: string, url: string) =
NBomber.Http.FSharp.Http.createRequest method url

static member GetRequestSize (request: HttpRequestMessage) =
NBomber.Http.FSharp.Http.getRequestSize request

Expand All @@ -12,3 +16,18 @@ type Http =

static member Send (client: HttpClient, request: HttpRequestMessage) =
NBomber.Http.FSharp.Http.send client request

[<Extension>]
type HttpExt =

[<Extension>]
static member WithHeader(req: HttpRequestMessage, name: string, value: string) =
req |> NBomber.Http.FSharp.Http.withHeader name value

[<Extension>]
static member WithVersion(req: HttpRequestMessage, version: string) =
req |> NBomber.Http.FSharp.Http.withVersion version

[<Extension>]
static member WithBody(req: HttpRequestMessage, body: HttpContent) =
req |> NBomber.Http.FSharp.Http.withBody body
27 changes: 25 additions & 2 deletions src/NBomber.Http/FSharp.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NBomber.Http.FSharp

open System
open System.Net.Http
open System.Net.Http.Headers
open NBomber.Contracts
Expand All @@ -17,16 +18,38 @@ module Http =
else
0

let getRequestSize (request: HttpRequestMessage) =
let internal getRequestSize (request: HttpRequestMessage) =
let headersSize = getHeadersSize request.Headers
let bodySize = getBodySize request.Content
bodySize + headersSize

let getResponseSize (response: HttpResponseMessage) =
let internal getResponseSize (response: HttpResponseMessage) =
let headersSize = getHeadersSize response.Headers
let bodySize = getBodySize response.Content
bodySize + headersSize

let createRequest (method: string) (url: string) =
new HttpRequestMessage(
method = HttpMethod(method),
requestUri = Uri(url, UriKind.RelativeOrAbsolute)
)

let withHeader (name: string) (value: string) (req: HttpRequestMessage) =
req.Headers.TryAddWithoutValidation(name, value) |> ignore
req

let withHeaders (headers: (string * string) list) (req: HttpRequestMessage) =
headers |> List.iter(fun (name, value) -> req.Headers.TryAddWithoutValidation(name, value) |> ignore)
req

let withVersion (version: string) (req: HttpRequestMessage) =
req.Version <- Version.Parse version
req

let withBody (body: HttpContent) (req: HttpRequestMessage) =
req.Content <- body
req

let send (client: HttpClient) (request: HttpRequestMessage) = backgroundTask {
let! response = client.SendAsync request

Expand Down
4 changes: 2 additions & 2 deletions src/NBomber.Http/NBomber.Http.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Anton Moldovan</Authors>
<Company>NBomber</Company>
<Version>4.0.0-beta</Version>
<Version>4.0.0-beta1</Version>
<Copyright>NBomber@2022</Copyright>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand All @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NBomber.Contracts" Version="4.0.0-beta6" />
<PackageReference Include="NBomber.Contracts" Version="4.0.0-beta9" />
<PackageReference Update="FSharp.Core" Version="6.0.5" />
</ItemGroup>

Expand Down

0 comments on commit 7968124

Please sign in to comment.