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

Update to ASP.NET Core 8 #220

Merged
merged 17 commits into from
Nov 14, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/approve-and-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: approve-and-merge

on:
pull_request:
branches: [ main ]
branches: [ main, dotnet-vnext ]

env:
REVIEWER_LOGIN: ${{ vars.REVIEWER_USER_NAME }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches: [ main, dotnet-vnext ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code-ql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches: [ main, dotnet-vnext ]
schedule:
- cron: '0 6 * * MON'
workflow_dispatch:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: dependency-review

on:
pull_request:
branches: [ main ]
branches: [ main, dotnet-vnext ]

permissions:
contents: read
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/ApplePayJS/bin/Debug/net7.0/JustEat.ApplePayJS.dll",
"program": "${workspaceRoot}/src/ApplePayJS/bin/Debug/net8.0/JustEat.ApplePayJS.dll",
"args": [],
"cwd": "${workspaceRoot}/src/ApplePayJS",
"stopAtEntry": false,
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>$(PackageProjectUrl).git</RepositoryUrl>
<TypeScriptToolsVersion>latest</TypeScriptToolsVersion>
<VersionPrefix>7.0.0</VersionPrefix>
<VersionPrefix>8.0.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if (($installDotNetSdk -eq $true) -And ($null -eq $env:TF_BUILD)) {
}

Write-Host "Publishing application..." -ForegroundColor Green
& $dotnet publish (Join-Path $solutionPath "src" "ApplePayJS" "ApplePayJS.csproj") --output $OutputPath --configuration $Configuration
& $dotnet publish (Join-Path $solutionPath "src" "ApplePayJS" "ApplePayJS.csproj") --output $OutputPath --configuration $Configuration --tl

$additionalArgs = @()

Expand All @@ -77,7 +77,7 @@ if (![string]::IsNullOrEmpty($env:GITHUB_SHA)) {
}

Write-Host "Running tests..." -ForegroundColor Green
& $dotnet test (Join-Path $solutionPath "tests" "ApplePayJS.Tests" "ApplePayJS.Tests.csproj") --output $OutputPath --configuration $Configuration $additionalArgs
& $dotnet test (Join-Path $solutionPath "tests" "ApplePayJS.Tests" "ApplePayJS.Tests.csproj") --output $OutputPath --configuration $Configuration --tl $additionalArgs

if ($LASTEXITCODE -ne 0) {
throw "dotnet publish failed with exit code $LASTEXITCODE"
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.403",
"version": "8.0.100",
"allowPrerelease": false
}
}
2 changes: 1 addition & 1 deletion src/ApplePayJS/ApplePayJS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageTags>applepay</PackageTags>
<PreserveCompilationContext>true</PreserveCompilationContext>
<RootNamespace>JustEat.ApplePayJS</RootNamespace>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<TypeScriptToolsVersion>latest</TypeScriptToolsVersion>
<UserSecretsId>JustEat.ApplePayJS</UserSecretsId>
</PropertyGroup>
Expand Down
22 changes: 4 additions & 18 deletions src/ApplePayJS/Clients/ApplePayClient.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
// Copyright (c) Just Eat, 2016. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Net.Mime;
using System.Text;
using System.Text.Json;

namespace JustEat.ApplePayJS.Clients;

public class ApplePayClient
public class ApplePayClient(HttpClient httpClient)
{
private readonly HttpClient _httpClient;

public ApplePayClient(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<JsonDocument> GetMerchantSessionAsync(
Uri requestUri,
MerchantSessionRequest request,
CancellationToken cancellationToken = default)
{
// POST the data to create a valid Apple Pay merchant session.
string json = JsonSerializer.Serialize(request);

using var content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);

using var response = await _httpClient.PostAsync(requestUri, content, cancellationToken);
using var response = await httpClient.PostAsJsonAsync(requestUri, request, cancellationToken);

response.EnsureSuccessStatusCode();

// Read the opaque merchant session JSON from the response body.
using var stream = await response.Content.ReadAsStreamAsync();

return await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
var merchantSession = await response.Content.ReadFromJsonAsync<JsonDocument>(cancellationToken);
return merchantSession!;
}
}
9 changes: 2 additions & 7 deletions src/ApplePayJS/Clients/MerchantCertificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

namespace JustEat.ApplePayJS.Clients;

public class MerchantCertificate
public class MerchantCertificate(IOptions<ApplePayOptions> options)
{
private readonly ApplePayOptions _options;

public MerchantCertificate(IOptions<ApplePayOptions> options)
{
_options = options.Value;
}
private readonly ApplePayOptions _options = options.Value;

public X509Certificate2 GetCertificate()
{
Expand Down
29 changes: 9 additions & 20 deletions src/ApplePayJS/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,18 @@ namespace JustEat.ApplePayJS.Controllers;
using Microsoft.Extensions.Options;
using Models;

public class HomeController : Controller
public class HomeController(
ApplePayClient client,
MerchantCertificate certificate,
IOptions<ApplePayOptions> options) : Controller
{
private readonly ApplePayClient _client;
private readonly MerchantCertificate _certificate;
private readonly ApplePayOptions _options;

public HomeController(
ApplePayClient client,
MerchantCertificate certificate,
IOptions<ApplePayOptions> options)
{
_client = client;
_certificate = certificate;
_options = options.Value;
}

public IActionResult Index()
{
// Get the merchant identifier and store name for use in the JavaScript by ApplePaySession.
var model = new HomeModel()
{
MerchantId = _certificate.GetMerchantIdentifier(),
StoreName = _options.StoreName,
MerchantId = certificate.GetMerchantIdentifier(),
StoreName = options.Value.StoreName,
};

return View(model);
Expand All @@ -56,13 +45,13 @@ public async Task<IActionResult> Validate([FromBody] ValidateMerchantSessionMode
// Create the JSON payload to POST to the Apple Pay merchant validation URL.
var request = new MerchantSessionRequest()
{
DisplayName = _options.StoreName,
DisplayName = options.Value.StoreName,
Initiative = "web",
InitiativeContext = Request.GetTypedHeaders().Host.Value,
MerchantIdentifier = _certificate.GetMerchantIdentifier(),
MerchantIdentifier = certificate.GetMerchantIdentifier(),
};

JsonDocument merchantSession = await _client.GetMerchantSessionAsync(requestUri, request, cancellationToken);
JsonDocument merchantSession = await client.GetMerchantSessionAsync(requestUri, request, cancellationToken);

// Return the merchant session as-is to the JavaScript as JSON.
return Json(merchantSession.RootElement);
Expand Down
2 changes: 1 addition & 1 deletion src/ApplePayJS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
var handler = new HttpClientHandler();
handler.ClientCertificates.Add(certificate);

// Apple Pay JS requires the use of at least TLS 1.2 to generate a merchange session:
// Apple Pay JS requires the use of at least TLS 1.2 to generate a merchant session:
// https://developer.apple.com/documentation/applepayjs/setting_up_server_requirements
// If you run an older operating system that does not negotiate this by default, uncomment the line below.
// handler.SslProtocols = SslProtocols.Tls12;
Expand Down
2 changes: 1 addition & 1 deletion src/ApplePayJS/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ApplePayJS/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "justeatapplepayjs",
"private": true,
"version": "7.0.0",
"version": "8.0.0",
"devDependencies": {
"@types/applepayjs": "3.0.0",
"@types/jquery": "3.3.34",
Expand Down
4 changes: 2 additions & 2 deletions tests/ApplePayJS.Tests/ApplePayJS.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
Expand All @@ -11,7 +11,7 @@
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageReference Include="JustEat.HttpClientInterception" Version="4.0.0" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.39.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
Expand Down
21 changes: 7 additions & 14 deletions tests/ApplePayJS.Tests/BrowserFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@

namespace ApplePayJS.Tests;

internal sealed class BrowserFixture
internal sealed class BrowserFixture(ITestOutputHelper? outputHelper)
{
public BrowserFixture(ITestOutputHelper? outputHelper)
{
OutputHelper = outputHelper;
}

private static bool IsRunningInGitHubActions { get; } = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"));

private ITestOutputHelper? OutputHelper { get; }

public async Task WithPageAsync(
Func<IPage, Task> action,
string browserType = "chromium",
Expand All @@ -32,8 +25,8 @@ public async Task WithPageAsync(

IPage page = await browser.NewPageAsync(options);

page.Console += (_, e) => OutputHelper?.WriteLine(e.Text);
page.PageError += (_, e) => OutputHelper?.WriteLine(e);
page.Console += (_, e) => outputHelper?.WriteLine(e.Text);
page.PageError += (_, e) => outputHelper?.WriteLine(e);

try
{
Expand Down Expand Up @@ -119,11 +112,11 @@ await page.ScreenshotAsync(new PageScreenshotOptions()
Path = path,
});

OutputHelper?.WriteLine($"Screenshot saved to {path}.");
outputHelper?.WriteLine($"Screenshot saved to {path}.");
}
catch (Exception ex)
{
OutputHelper?.WriteLine("Failed to capture screenshot: " + ex);
outputHelper?.WriteLine("Failed to capture screenshot: " + ex);
}
}

Expand Down Expand Up @@ -152,11 +145,11 @@ private async Task TryCaptureVideoAsync(

File.Move(videoSource, videoDestination);

OutputHelper?.WriteLine($"Video saved to {videoDestination}.");
outputHelper?.WriteLine($"Video saved to {videoDestination}.");
}
catch (Exception ex)
{
OutputHelper?.WriteLine("Failed to capture video: " + ex);
outputHelper?.WriteLine("Failed to capture video: " + ex);
}
}
}
18 changes: 5 additions & 13 deletions tests/ApplePayJS.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@

namespace ApplePayJS.Tests;

public class IntegrationTests : IAsyncLifetime
public class IntegrationTests(ITestOutputHelper outputHelper) : IAsyncLifetime
{
public IntegrationTests(ITestOutputHelper outputHelper)
{
Fixture = new TestFixture()
{
OutputHelper = outputHelper,
};
}

private TestFixture Fixture { get; }
private TestFixture Fixture { get; } = new() { OutputHelper = outputHelper };

public Task InitializeAsync()
{
Expand Down Expand Up @@ -64,7 +56,7 @@ await fixture.WithPageAsync(async (page) =>
await page.WaitForSelectorAsync(Selectors.CardName);
await page.InnerTextAsync(Selectors.CardName).ShouldBe("American Express");

foreach (string selector in new[] { Selectors.BillingContact, Selectors.ShipingContact })
foreach (string selector in new[] { Selectors.BillingContact, Selectors.ShippingContact })
{
var contact = await page.QuerySelectorAsync(selector);
contact.ShouldNotBeNull();
Expand All @@ -79,7 +71,7 @@ await fixture.WithPageAsync(async (page) =>

private static void InstallPlaywright()
{
int exitCode = Program.Main(new[] { "install" });
int exitCode = Program.Main(["install"]);

if (exitCode != 0)
{
Expand All @@ -94,6 +86,6 @@ private static class Selectors
internal const string CardName = ".card-name";
internal const string ContactName = ".contact-name";
internal const string Pay = "id=apple-pay-button";
internal const string ShipingContact = "id=shipping-contact";
internal const string ShippingContact = "id=shipping-contact";
}
}
11 changes: 2 additions & 9 deletions tests/ApplePayJS.Tests/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,14 @@ private void EnsureServer()
}
}

private sealed class HttpRequestInterceptionFilter : IHttpMessageHandlerBuilderFilter
private sealed class HttpRequestInterceptionFilter(HttpClientInterceptorOptions options) : IHttpMessageHandlerBuilderFilter
{
internal HttpRequestInterceptionFilter(HttpClientInterceptorOptions options)
{
Options = options;
}

private HttpClientInterceptorOptions Options { get; }

public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuilder> next)
{
return (builder) =>
{
next(builder);
builder.AdditionalHandlers.Add(Options.CreateHttpMessageHandler());
builder.AdditionalHandlers.Add(options.CreateHttpMessageHandler());
};
}
}
Expand Down