Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types for Minimal API #35

Merged
merged 7 commits into from
Feb 24, 2022
Merged
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
21 changes: 10 additions & 11 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Stop-Script($exitCode) {
if ($exitCode -eq 0) {
Write-Host "SUCCESS" -ForegroundColor Green
} else {
Write-Host "FAILIURE" -ForegroundColor Red
Write-Host "FAILURE" -ForegroundColor Red
}
exit $exitCode
}
Expand All @@ -31,38 +31,37 @@ Remove-EntirePath .\Twilio.AspNet.Mvc\obj

# Build Twilio.AspNet.Common first
Push-Location .\Twilio.AspNet.Common
dotnet restore
dotnet restore -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }
dotnet msbuild /t:clean /verbosity:minimal
dotnet clean -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }
dotnet msbuild /p:Configuration=Release /verbosity:minimal
dotnet build -c Release -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }
dotnet msbuild /t:pack /p:Configuration=Release /p:OutputPath=..\..\ /verbosity:minimal
dotnet pack -c Release -o ..\..\ -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }
Pop-Location

& $nugetExe ('restore', '-Source', ($PSScriptRoot + ';https://api.nuget.org/v3/index.json'))
dotnet restore -v m --source $($PSScriptRoot + ';https://api.nuget.org/v3/index.json')
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }

dotnet msbuild /t:clean /verbosity:minimal
dotnet clean -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }

dotnet msbuild /p:Configuration=Release /verbosity:minimal
dotnet build -c Release -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }

# Run tests
& $xunitExe (".\Twilio.AspNet.Mvc.UnitTests\bin\Release\Twilio.AspNet.Mvc.UnitTests.dll")
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }

Push-Location .\Twilio.AspNet.Core.UnitTests
dotnet test
dotnet test -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }
Pop-Location

# Create the NuGet Packages

Push-Location .\Twilio.AspNet.Core
dotnet msbuild /t:pack /p:Configuration=Release /p:OutputPath=..\..\ /verbosity:minimal
dotnet pack -c Release -o ..\..\ -v m
if ($lastExitCode -ne 0) { Stop-Script $lastExitCode }
Pop-Location

Expand Down
2 changes: 1 addition & 1 deletion src/Twilio.AspNet.Common/Twilio.AspNet.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>netstandard1.0;net45</TargetFrameworks>
<OutputType>Library</OutputType>
<PackageId>Twilio.AspNet.Common</PackageId>
<PackageVersion>5.68.3</PackageVersion>
<PackageVersion>5.71.0</PackageVersion>
<Authors>Twilio</Authors>
<Description>Twilio request classes for handling Twilio webhooks.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand Down
38 changes: 38 additions & 0 deletions src/Twilio.AspNet.Core.UnitTests/MinimalApiTwiMLResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Twilio.AspNet.Core.MinimalApi;
using Twilio.TwiML;
using Xunit;

namespace Twilio.AspNet.Core.UnitTests;

// ReSharper disable once InconsistentNaming
public class MinimalApiTwiMLResultTests
{
[Fact]
public Task TestTwimlResultWritesVoiceResponseToResponseBody() =>
ValidateTwimlResultWritesToResponseBody(GetVoiceResponse());

[Fact]
public Task TestTwimlResultWritesMessagingResponseToResponseBody() =>
ValidateTwimlResultWritesToResponseBody(GetMessagingResponse());

private static async Task ValidateTwimlResultWritesToResponseBody(TwiML.TwiML twiMlResponse)
{
var httpContext = new DefaultHttpContext();
httpContext.Response.Body = new MemoryStream();

var twimlResult = Results.Extensions.TwiML(twiMlResponse);
await twimlResult.ExecuteAsync(httpContext);

httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(httpContext.Response.Body);
var responseBody = await reader.ReadToEndAsync();
Assert.Equal(twiMlResponse.ToString(), responseBody);
}

private static VoiceResponse GetVoiceResponse() => new VoiceResponse().Say("Hello World");
private static MessagingResponse GetMessagingResponse() => new MessagingResponse().Message("Hello World");
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Twilio.AspNet.Core;
using Xunit;

namespace Twilio.AspNet.Mvc.UnitTests
namespace Twilio.AspNet.Core.UnitTests
{
public class ContextMocks
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
53 changes: 53 additions & 0 deletions src/Twilio.AspNet.Core.UnitTests/TwilioControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Routing;
using Twilio.TwiML;
using Xunit;

namespace Twilio.AspNet.Core.UnitTests
{
public class TwilioControllerTests
{
[Fact]
public async Task TestTwimlResultWritesVoiceResponseToResponseBody()
{
var twiml = new VoiceResponse().Say("Hello World");
var result = new TwilioController().TwiML(twiml);
var actionContext = CreateActionContext();
await result.ExecuteResultAsync(actionContext);

actionContext.HttpContext.Response.Body.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(actionContext.HttpContext.Response.Body);
var responseBody = await reader.ReadToEndAsync();
Assert.Equal(twiml.ToString(), responseBody);
}

[Fact]
public async Task TestTwimlResultWritesMessageResponseToResponseBody()
{
var twiml = new MessagingResponse().Message("Hello World");
var result = new TwilioController().TwiML(twiml);
var actionContext = CreateActionContext();
await result.ExecuteResultAsync(actionContext);

actionContext.HttpContext.Response.Body.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(actionContext.HttpContext.Response.Body);
var responseBody = await reader.ReadToEndAsync();
Assert.Equal(twiml.ToString(), responseBody);
}

private ActionContext CreateActionContext()
{
var httpContext = new DefaultHttpContext();
httpContext.Response.Body = new MemoryStream();
return new ActionContext(httpContext,
new RouteData(),
new ActionDescriptor(),
new ModelStateDictionary());
}
}
}
59 changes: 59 additions & 0 deletions src/Twilio.AspNet.Core/MinimalApiTwiMLResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#if NET6_0_OR_GREATER
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

// ReSharper disable once CheckNamespace
namespace Twilio.AspNet.Core.MinimalApi;

/// <summary>
/// Adds extension methods to Results.Extensions to write TwiML objects to the HTTP response body
/// </summary>
public static class ResultsExtensions
{
/// <summary>
/// Returns a TwiMLResult
/// </summary>
/// <param name="results">Results extensions interface</param>
/// <param name="twimlResponse">The TwiML to write to the HTTP response body</param>
/// <returns>The TwiMLResult will write the TwiML to the HTTP response body</returns>
// ReSharper disable once InconsistentNaming
// ReSharper disable once UnusedParameter.Global
public static TwiMLResult TwiML(this IResultExtensions results, TwiML.TwiML twimlResponse)
=> new TwiMLResult(twimlResponse);
}

/// <summary>
/// Writes TwiML object to the HTTP response body
/// </summary>
// ReSharper disable once InconsistentNaming
public class TwiMLResult : IResult
{
// ReSharper disable once InconsistentNaming
private string twiML;

/// <summary>
/// Creates a TwiMLResult object
/// </summary>
/// <param name="twimlResponse">The TwiML to write to the HTTP response body</param>
// ReSharper disable once InconsistentNaming
public TwiMLResult(TwiML.TwiML twimlResponse)
{
twiML = twimlResponse?.ToString();
}

/// <summary>
/// Writes the TwiML to the HTTP response body
/// </summary>
/// <param name="httpContext">The HttpContext containing the Response to write the TwiML to</param>
public Task ExecuteAsync(HttpContext httpContext)
{
twiML ??= "<Response></Response>";

httpContext.Response.ContentType = "application/xml";
httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(twiML);
return httpContext.Response.WriteAsync(twiML);
}
}

#endif
11 changes: 6 additions & 5 deletions src/Twilio.AspNet.Core/Twilio.AspNet.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<TargetFrameworks>netstandard1.6;net6.0</TargetFrameworks>
<OutputType>Library</OutputType>
<PackageId>Twilio.AspNet.Core</PackageId>
<PackageVersion>5.68.3</PackageVersion>
<PackageVersion>5.71.0</PackageVersion>
<Authors>Twilio</Authors>
<Description>Twilio helpers for ASP.NET Core 1.0 to 2.x.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand All @@ -14,12 +14,13 @@
<PackageProjectUrl>http://github.com/twilio/twilio-aspnet</PackageProjectUrl>
<PackageIconUrl>https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/twilio-icon-64x64.png</PackageIconUrl>
<PackageIcon>icon.png</PackageIcon>
<Version>5.68.3</Version>
<Version>5.71.0</Version>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="2.2.8" Condition="'$(TargetFramework)' == 'net6.0' " />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.0.4" />
<PackageReference Include="Twilio" Version="5.68.1" />
<PackageReference Include="Twilio.AspNet.Common" Version="5.68.3" />
<PackageReference Include="Twilio" Version="5.71.0" />
<PackageReference Include="Twilio.AspNet.Common" Version="5.71.0" />
<None Include="icon.png" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions src/Twilio.AspNet.Core/TwilioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TwilioController : ControllerBase
/// <param name="response"></param>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
protected TwiMLResult TwiML(MessagingResponse response)
public TwiMLResult TwiML(MessagingResponse response)
{
return new TwiMLResult(response);
}
Expand All @@ -25,7 +25,7 @@ protected TwiMLResult TwiML(MessagingResponse response)
/// <param name="response"></param>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
protected TwiMLResult TwiML(VoiceResponse response)
public TwiMLResult TwiML(VoiceResponse response)
{
return new TwiMLResult(response);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Twilio.AspNet.Mvc.UnitTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

[assembly: Guid("b92bda0d-ad69-4756-bbd6-6e77c4b51906")]

[assembly: AssemblyVersion("5.68.3")]
[assembly: AssemblyFileVersion("5.68.3")]
[assembly: AssemblyVersion("5.71.0")]
[assembly: AssemblyFileVersion("5.71.0")]


Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Twilio, Version=5.68.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Twilio.5.68.1\lib\net451\Twilio.dll</HintPath>
<Reference Include="Twilio, Version=5.71.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Twilio.5.71.0\lib\net451\Twilio.dll</HintPath>
</Reference>
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
Expand Down Expand Up @@ -147,3 +147,4 @@
</Target>
<Import Project="..\packages\xunit.core.2.3.1\build\xunit.core.targets" Condition="Exists('..\packages\xunit.core.2.3.1\build\xunit.core.targets')" />
</Project>

2 changes: 1 addition & 1 deletion src/Twilio.AspNet.Mvc.UnitTests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<package id="System.IdentityModel.Tokens.Jwt" version="5.2.1" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net471" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net471" />
<package id="Twilio" version="5.68.1" targetFramework="net471" />
<package id="Twilio" version="5.71.0" targetFramework="net471" />
<package id="xunit" version="2.3.1" targetFramework="net471" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net471" />
<package id="xunit.analyzers" version="0.8.0" targetFramework="net471" />
Expand Down
5 changes: 3 additions & 2 deletions src/Twilio.AspNet.Mvc/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.68.3")]
[assembly: AssemblyFileVersion("5.68.3")]
[assembly: AssemblyVersion("5.71.0")]
[assembly: AssemblyFileVersion("5.71.0")]


9 changes: 5 additions & 4 deletions src/Twilio.AspNet.Mvc/Twilio.AspNet.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Twilio, Version=5.68.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Twilio.5.68.1\lib\net451\Twilio.dll</HintPath>
<Reference Include="Twilio, Version=5.71.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Twilio.5.71.0\lib\net451\Twilio.dll</HintPath>
</Reference>
<Reference Include="Twilio.AspNet.Common, Version=5.68.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Twilio.AspNet.Common.5.68.3\lib\net45\Twilio.AspNet.Common.dll</HintPath>
<Reference Include="Twilio.AspNet.Common, Version=5.71.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Twilio.AspNet.Common.5.71.0\lib\net45\Twilio.AspNet.Common.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -121,3 +121,4 @@




4 changes: 2 additions & 2 deletions src/Twilio.AspNet.Mvc/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net451" />
<package id="System.IdentityModel.Tokens.Jwt" version="5.1.2" targetFramework="net451" />
<package id="Twilio" version="5.68.1" targetFramework="net451" />
<package id="Twilio.AspNet.Common" version="5.68.3" targetFramework="net451" />
<package id="Twilio" version="5.71.0" targetFramework="net451" />
<package id="Twilio.AspNet.Common" version="5.71.0" targetFramework="net451" />
</packages>
10 changes: 10 additions & 0 deletions src/Twilio.AspNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Twilio.AspNet.Core", "Twili
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Twilio.AspNet.Mvc.UnitTests", "Twilio.AspNet.Mvc.UnitTests\Twilio.AspNet.Mvc.UnitTests.csproj", "{B92BDA0D-AD69-4756-BBD6-6E77C4B51906}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Twilio.AspNet.Core.UnitTests", "Twilio.AspNet.Core.UnitTests\Twilio.AspNet.Core.UnitTests.csproj", "{B3E732C9-27EF-4E96-B620-5B5DA57D9AD3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,14 @@ Global
{B92BDA0D-AD69-4756-BBD6-6E77C4B51906}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B92BDA0D-AD69-4756-BBD6-6E77C4B51906}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B92BDA0D-AD69-4756-BBD6-6E77C4B51906}.Release|Any CPU.Build.0 = Release|Any CPU
{FABCBDFB-E9F8-4B0F-9657-7B156CDA98C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FABCBDFB-E9F8-4B0F-9657-7B156CDA98C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FABCBDFB-E9F8-4B0F-9657-7B156CDA98C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FABCBDFB-E9F8-4B0F-9657-7B156CDA98C2}.Release|Any CPU.Build.0 = Release|Any CPU
{B3E732C9-27EF-4E96-B620-5B5DA57D9AD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3E732C9-27EF-4E96-B620-5B5DA57D9AD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3E732C9-27EF-4E96-B620-5B5DA57D9AD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3E732C9-27EF-4E96-B620-5B5DA57D9AD3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading