Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="RestSharp" Version="108.0.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
<PackageReference Include="RestSharp" Version="114.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using BenchmarkDotNet.Attributes;
using RestSharp;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;

namespace HttpClientVsRestSharp
{
[MemoryDiagnoser]
public class TodoBenchmark
{
private static readonly HttpClient httpClient = new HttpClient();
private static readonly RestClient restClient = new RestClient("https://jsonplaceholder.typicode.com/");

public TodoBenchmark()
{
httpClient.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
}
private static readonly HttpClient httpClient = new() { BaseAddress = new Uri("https://jsonplaceholder.typicode.com/") };
private static readonly RestClient restClient = new("https://jsonplaceholder.typicode.com/");

[Benchmark]
public async Task<List<Todo>?> GetAllTodos_HttpClient()
Expand Down Expand Up @@ -62,10 +55,7 @@ public TodoBenchmark()
{
var todoForUpdate = GetTodoForUpdate();

var serializedTodo = JsonSerializer.Serialize(todoForUpdate);
var requestContent = new StringContent(serializedTodo, Encoding.UTF8, "application/json");

var response = await httpClient.PutAsync($"todos/{todoForUpdate.Id}", requestContent);
var response = await httpClient.PutAsJsonAsync($"todos/{todoForUpdate.Id}", todoForUpdate);
var updatedTodo = await response.Content.ReadFromJsonAsync<Todo>();

return updatedTodo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
Expand All @@ -11,7 +12,7 @@

namespace Tests
{
public class Tests
public class LiveTests
{

[Fact]
Expand Down Expand Up @@ -145,6 +146,97 @@ public async Task GivenRestSharp_WhenDeletingTodo_ThenResponseIsSuccessful()
Assert.True(response.IsSuccessful);
}

[Fact]
public async Task GivenHttpClient_WhenGettingTodosWithGetFromJsonAsync_ThenReturnsListOfTodos()
{
//Arrange
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");

//Act
var todos = await httpClient.GetFromJsonAsync<List<Todo>>("todos");

//Assert
Assert.NotNull(todos);
Assert.NotEmpty(todos);
}

[Fact]
public async Task GivenRestSharp_WhenExecutingGetForTodos_ThenResponseIsSuccessful()
{
//Arrange
var restClient = new RestClient("https://jsonplaceholder.typicode.com/");
var request = new RestRequest("todos");

//Act
var response = await restClient.ExecuteGetAsync<List<Todo>>(request);

//Assert
Assert.True(response.IsSuccessful);
Assert.NotNull(response.Data);
Assert.NotEmpty(response.Data);
}

[Fact]
public async Task GivenHttpClient_WhenPathDoesNotExist_ThenEnsureSuccessStatusCodeThrows()
{
//Arrange
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");

//Act
var response = await httpClient.GetAsync("no-such-path");

//Assert
Assert.False(response.IsSuccessStatusCode);
Assert.Throws<HttpRequestException>(() => response.EnsureSuccessStatusCode());
}

[Fact]
public async Task GivenRestSharp_WhenPathDoesNotExist_ThenResponseIsNotSuccessfulAndNothingThrows()
{
//Arrange
var restClient = new RestClient("https://jsonplaceholder.typicode.com/");
var request = new RestRequest("no-such-path");

//Act
var response = await restClient.ExecuteGetAsync<Todo>(request);

//Assert
Assert.False(response.IsSuccessful);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public void GivenHttpClient_WhenSendingSynchronously_ThenResponseIsSuccessStatusCode()
{
//Arrange
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
var request = new HttpRequestMessage(HttpMethod.Get, "todos");

//Act
var response = httpClient.Send(request);

//Assert
Assert.True(response.IsSuccessStatusCode);
}

[Fact]
public void GivenRestSharp_WhenGettingTodosSynchronously_ThenReturnsListOfTodos()
{
//Arrange
var restClient = new RestClient("https://jsonplaceholder.typicode.com/");
var request = new RestRequest("todos");

//Act
var todos = restClient.Get<List<Todo>>(request);

//Assert
Assert.NotNull(todos);
Assert.NotEmpty(todos);
}

private static Todo GetTodoForCreation()
{
return new Todo
Expand Down
10 changes: 5 additions & 5 deletions dotnet-client-libraries/HttpClientVsRestSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading