{ | |||||
"Version": 1, | |||||
"WorkspaceRootPath": "C:\\Users\\christophe_brachet\\Desktop\\TestApiDocker\\", | |||||
"Documents": [], | |||||
"DocumentGroupContainers": [ | |||||
{ | |||||
"Orientation": 0, | |||||
"VerticalTabListWidth": 256, | |||||
"DocumentGroups": [] | |||||
} | |||||
] | |||||
} |
{ | |||||
"Version": 1, | |||||
"WorkspaceRootPath": "C:\\Users\\christophe_brachet\\Desktop\\TestApiDocker\\", | |||||
"Documents": [ | |||||
{ | |||||
"AbsoluteMoniker": "D:0:0:{9EA9D523-C331-4E33-8204-B4024392D267}|TestApiDocker\\TestApiDocker.csproj|c:\\users\\christophe_brachet\\desktop\\testapidocker\\testapidocker\\myunittest.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", | |||||
"RelativeMoniker": "D:0:0:{9EA9D523-C331-4E33-8204-B4024392D267}|TestApiDocker\\TestApiDocker.csproj|solutionrelative:testapidocker\\myunittest.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" | |||||
}, | |||||
{ | |||||
"AbsoluteMoniker": "D:0:0:{9EA9D523-C331-4E33-8204-B4024392D267}|TestApiDocker\\TestApiDocker.csproj|c:\\users\\christophe_brachet\\desktop\\testapidocker\\testapidocker\\models\\weatherforecast.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", | |||||
"RelativeMoniker": "D:0:0:{9EA9D523-C331-4E33-8204-B4024392D267}|TestApiDocker\\TestApiDocker.csproj|solutionrelative:testapidocker\\models\\weatherforecast.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" | |||||
} | |||||
], | |||||
"DocumentGroupContainers": [ | |||||
{ | |||||
"Orientation": 0, | |||||
"VerticalTabListWidth": 256, | |||||
"DocumentGroups": [ | |||||
{ | |||||
"DockedWidth": 200, | |||||
"SelectedChildIndex": 1, | |||||
"Children": [ | |||||
{ | |||||
"$type": "Document", | |||||
"DocumentIndex": 1, | |||||
"Title": "WeatherForecast.cs", | |||||
"DocumentMoniker": "C:\\Users\\christophe_brachet\\Desktop\\TestApiDocker\\TestApiDocker\\Models\\WeatherForecast.cs", | |||||
"RelativeDocumentMoniker": "TestApiDocker\\Models\\WeatherForecast.cs", | |||||
"ToolTip": "C:\\Users\\christophe_brachet\\Desktop\\TestApiDocker\\TestApiDocker\\Models\\WeatherForecast.cs", | |||||
"RelativeToolTip": "TestApiDocker\\Models\\WeatherForecast.cs", | |||||
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAwAAAAkAAAAAAAAAA==", | |||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", | |||||
"WhenOpened": "2024-08-26T21:34:59.064Z", | |||||
"EditorCaption": "" | |||||
}, | |||||
{ | |||||
"$type": "Document", | |||||
"DocumentIndex": 0, | |||||
"Title": "MyUnitTest.cs", | |||||
"DocumentMoniker": "C:\\Users\\christophe_brachet\\Desktop\\TestApiDocker\\TestApiDocker\\MyUnitTest.cs", | |||||
"RelativeDocumentMoniker": "TestApiDocker\\MyUnitTest.cs", | |||||
"ToolTip": "C:\\Users\\christophe_brachet\\Desktop\\TestApiDocker\\TestApiDocker\\MyUnitTest.cs", | |||||
"RelativeToolTip": "TestApiDocker\\MyUnitTest.cs", | |||||
"ViewState": "AgIAAAkAAAAAAAAAAAAAAB4AAAALAAAAAAAAAA==", | |||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", | |||||
"WhenOpened": "2024-08-26T21:21:10.154Z", | |||||
"EditorCaption": "" | |||||
} | |||||
] | |||||
} | |||||
] | |||||
} | |||||
] | |||||
} |
| |||||
Microsoft Visual Studio Solution File, Format Version 12.00 | |||||
# Visual Studio Version 17 | |||||
VisualStudioVersion = 17.11.35208.52 | |||||
MinimumVisualStudioVersion = 10.0.40219.1 | |||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApiDocker", "TestApiDocker\TestApiDocker.csproj", "{9EA9D523-C331-4E33-8204-B4024392D267}" | |||||
EndProject | |||||
Global | |||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||||
Debug|Any CPU = Debug|Any CPU | |||||
Release|Any CPU = Release|Any CPU | |||||
EndGlobalSection | |||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | |||||
{9EA9D523-C331-4E33-8204-B4024392D267}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||||
{9EA9D523-C331-4E33-8204-B4024392D267}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||||
{9EA9D523-C331-4E33-8204-B4024392D267}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||||
{9EA9D523-C331-4E33-8204-B4024392D267}.Release|Any CPU.Build.0 = Release|Any CPU | |||||
EndGlobalSection | |||||
GlobalSection(SolutionProperties) = preSolution | |||||
HideSolutionNode = FALSE | |||||
EndGlobalSection | |||||
GlobalSection(ExtensibilityGlobals) = postSolution | |||||
SolutionGuid = {90F8E6B7-23C2-4D64-AA1A-21BA19F95C47} | |||||
EndGlobalSection | |||||
EndGlobal |
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
namespace TestApiDocker.Models | |||||
{ | |||||
public class WeatherForecast | |||||
{ | |||||
public DateOnly Date { get; set; } | |||||
public int TemperatureC { get; set; } | |||||
public string? Summary { get; set; } | |||||
} | |||||
} |
using DotNet.Testcontainers.Builders; | |||||
using DotNet.Testcontainers.Containers; | |||||
using DotNet.Testcontainers.Images; | |||||
using System.Net.Http.Json; | |||||
using TestApiDocker.Models; | |||||
namespace TestApiDocker | |||||
{ | |||||
[TestClass] | |||||
public class MyUnitTest | |||||
{ | |||||
private static readonly IContainer _container = new ContainerBuilder() | |||||
.WithImage("ecolocale/my-api:latest") | |||||
.WithPortBinding(9090, 9090) | |||||
.WithCleanUp(true) | |||||
.Build(); | |||||
[TestInitialize] | |||||
public async Task StartContainer() | |||||
{ | |||||
await _container.StartAsync(); | |||||
} | |||||
[TestMethod] | |||||
public async Task ShouldReturnWeatherForecast() | |||||
{ | |||||
//Laisser le temps de démarrer que le container se charge | |||||
await Task.Delay(1000); | |||||
HttpClient httpClient = new HttpClient(); | |||||
Uri requestUri = | |||||
new UriBuilder( | |||||
Uri.UriSchemeHttp, | |||||
_container.Hostname, | |||||
_container.GetMappedPublicPort(9090), | |||||
"WeatherForecast" | |||||
).Uri; | |||||
List<WeatherForecast> weatherForecasts = await httpClient.GetFromJsonAsync<List<WeatherForecast>>(requestUri); | |||||
Assert.AreEqual(5, weatherForecasts.Count()); | |||||
} | |||||
[TestCleanup] | |||||
public async Task DisposeContainer() | |||||
{ | |||||
await _container.StopAsync(); | |||||
} | |||||
} | |||||
} |
<Project Sdk="Microsoft.NET.Sdk"> | |||||
<PropertyGroup> | |||||
<TargetFramework>net8.0</TargetFramework> | |||||
<ImplicitUsings>enable</ImplicitUsings> | |||||
<Nullable>enable</Nullable> | |||||
<IsPackable>false</IsPackable> | |||||
<IsTestProject>true</IsTestProject> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | |||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | |||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" /> | |||||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" /> | |||||
<PackageReference Include="Testcontainers" Version="3.9.0" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> | |||||
</ItemGroup> | |||||
</Project> |
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base | |||||
WORKDIR /app | |||||
EXPOSE 9090 | |||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build | |||||
WORKDIR /src | |||||
COPY ["docker-dotnetcore-api.csproj", "."] | |||||
RUN dotnet restore "./docker-dotnetcore-api.csproj" | |||||
COPY . . | |||||
WORKDIR "/src/." | |||||
RUN dotnet build "docker-dotnetcore-api.csproj" -c Release -o /app/build | |||||
FROM build AS publish | |||||
RUN dotnet publish "docker-dotnetcore-api.csproj" -c Release -o /app/publish | |||||
FROM base AS final | |||||
WORKDIR /app | |||||
COPY --from=publish /app/publish . | |||||
ENTRYPOINT ["dotnet", "docker-dotnetcore-api.dll"] |
var builder = WebApplication.CreateBuilder(args); | |||||
builder.WebHost.UseUrls("http://0.0.0.0:9090"); | |||||
// Add services to the container. | |||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | |||||
builder.Services.AddEndpointsApiExplorer(); | |||||
builder.Services.AddSwaggerGen(); | |||||
var app = builder.Build(); | |||||
// Configure the HTTP request pipeline. | |||||
if (app.Environment.IsDevelopment()) | |||||
{ | |||||
app.UseSwagger(); | |||||
app.UseSwaggerUI(); | |||||
} | |||||
app.UseHttpsRedirection(); | |||||
var summaries = new[] | |||||
{ | |||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |||||
}; | |||||
app.MapGet("/weatherforecast", () => | |||||
{ | |||||
var forecast = Enumerable.Range(1, 5).Select(index => | |||||
new WeatherForecast | |||||
( | |||||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | |||||
Random.Shared.Next(-20, 55), | |||||
summaries[Random.Shared.Next(summaries.Length)] | |||||
)) | |||||
.ToArray(); | |||||
return forecast; | |||||
}) | |||||
.WithName("GetWeatherForecast") | |||||
.WithOpenApi(); | |||||
app.Run(); | |||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | |||||
{ | |||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | |||||
} |
{ | |||||
"$schema": "http://json.schemastore.org/launchsettings.json", | |||||
"iisSettings": { | |||||
"windowsAuthentication": false, | |||||
"anonymousAuthentication": true, | |||||
"iisExpress": { | |||||
"applicationUrl": "http://localhost:40366", | |||||
"sslPort": 44378 | |||||
} | |||||
}, | |||||
"profiles": { | |||||
"http": { | |||||
"commandName": "Project", | |||||
"dotnetRunMessages": true, | |||||
"launchBrowser": true, | |||||
"launchUrl": "swagger", | |||||
"applicationUrl": "http://localhost:5281", | |||||
"environmentVariables": { | |||||
"ASPNETCORE_ENVIRONMENT": "Development" | |||||
} | |||||
}, | |||||
"https": { | |||||
"commandName": "Project", | |||||
"dotnetRunMessages": true, | |||||
"launchBrowser": true, | |||||
"launchUrl": "swagger", | |||||
"applicationUrl": "https://localhost:7047;http://localhost:5281", | |||||
"environmentVariables": { | |||||
"ASPNETCORE_ENVIRONMENT": "Development" | |||||
} | |||||
}, | |||||
"IIS Express": { | |||||
"commandName": "IISExpress", | |||||
"launchBrowser": true, | |||||
"launchUrl": "swagger", | |||||
"environmentVariables": { | |||||
"ASPNETCORE_ENVIRONMENT": "Development" | |||||
} | |||||
} | |||||
} | |||||
} |
{ | |||||
"Logging": { | |||||
"LogLevel": { | |||||
"Default": "Information", | |||||
"Microsoft.AspNetCore": "Warning" | |||||
} | |||||
} | |||||
} |
{ | |||||
"Logging": { | |||||
"LogLevel": { | |||||
"Default": "Information", | |||||
"Microsoft.AspNetCore": "Warning" | |||||
} | |||||
}, | |||||
"AllowedHosts": "*" | |||||
} |
<Project Sdk="Microsoft.NET.Sdk.Web"> | |||||
<PropertyGroup> | |||||
<TargetFramework>net8.0</TargetFramework> | |||||
<Nullable>enable</Nullable> | |||||
<ImplicitUsings>enable</ImplicitUsings> | |||||
<RootNamespace>docker_dotnetcore_api</RootNamespace> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" /> | |||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | |||||
</ItemGroup> | |||||
</Project> |
<?xml version="1.0" encoding="utf-8"?> | |||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||||
<PropertyGroup> | |||||
<ActiveDebugProfile>https</ActiveDebugProfile> | |||||
</PropertyGroup> | |||||
</Project> |
@docker_dotnetcore_api_HostAddress = http://localhost:5281 | |||||
GET {{docker_dotnetcore_api_HostAddress}}/weatherforecast/ | |||||
Accept: application/json | |||||
### |