Skip to content

Commit

Permalink
feat:wasm test (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
MayueCif committed Oct 31, 2023
1 parent a7b71b0 commit bf13bbb
Show file tree
Hide file tree
Showing 37 changed files with 667 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "Masa.Stack.Components.sln"
}
6 changes: 6 additions & 0 deletions Masa.Stack.Components.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MasaWebApp", "tests\MasaWeb
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Stack.Components", "src\Masa.Stack.Components\Masa.Stack.Components.csproj", "{DEE455D8-999F-426D-A5D4-D5EAF5E337B8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MasaWasmApp", "tests\MasaWasmApp\MasaWasmApp.csproj", "{5C2DD1F3-214F-475C-81D4-D13EC7ACEFE4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{DEE455D8-999F-426D-A5D4-D5EAF5E337B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEE455D8-999F-426D-A5D4-D5EAF5E337B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEE455D8-999F-426D-A5D4-D5EAF5E337B8}.Release|Any CPU.Build.0 = Release|Any CPU
{5C2DD1F3-214F-475C-81D4-D13EC7ACEFE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C2DD1F3-214F-475C-81D4-D13EC7ACEFE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C2DD1F3-214F-475C-81D4-D13EC7ACEFE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C2DD1F3-214F-475C-81D4-D13EC7ACEFE4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
25 changes: 25 additions & 0 deletions tests/MasaWasmApp/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin />
}
else
{
<p>You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
16 changes: 16 additions & 0 deletions tests/MasaWasmApp/MasaWasmApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Blazor" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.3" PrivateAssets="all" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions tests/MasaWasmApp/Pages/Authentication.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@page "/authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
<RemoteAuthenticatorView Action="@Action" >
<LoggingIn>
<h2>Checking login state.</h2>
</LoggingIn>
<LogInFailed>
<h2>Your have canceled the Login action.</h2>
</LogInFailed>
<LogOutSucceeded>
<h2>The Logout action was completed successfully.</h2>
</LogOutSucceeded>
</RemoteAuthenticatorView>

@code {
[Parameter] public string Action { get; set; }
}
18 changes: 18 additions & 0 deletions tests/MasaWasmApp/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @_currentCount</p>

<MButton Color="primary" OnClick="IncrementCount">Click me</MButton>

@code {
private int _currentCount = 0;

private void IncrementCount()
{
_currentCount++;
}
}
57 changes: 57 additions & 0 deletions tests/MasaWasmApp/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@page "/fetchdata"
@inject HttpClient Http
@attribute [Authorize]

<PageTitle>Weather forecast</PageTitle>

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (_forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<MDataTable Headers="_headers" Items="_forecasts" ItemsPerPage="5">
<ItemColContent>
@if (context.Header.Value == nameof(WeatherForecast.Date))
{
@context.Item.Date.ToShortDateString()
}
else
{
@context.Value
}
</ItemColContent>
</MDataTable>
}

@code {
private WeatherForecast[]? _forecasts;

private List<DataTableHeader<WeatherForecast>> _headers = new()
{
new() { Text = "Date", Value = nameof(WeatherForecast.Date) },
new() { Text = "Temp. (C)", Value = nameof(WeatherForecast.TemperatureC) },
new() { Text = "Temp. (F)", Value = nameof(WeatherForecast.TemperatureF) },
new() { Text = "Summary", Value = nameof(WeatherForecast.Summary), Sortable = false }
};

protected override async Task OnInitializedAsync()
{
_forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
}

public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public string? Summary { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
9 changes: 9 additions & 0 deletions tests/MasaWasmApp/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@page "/"

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="This is fire-new Blazor UI!" />
23 changes: 23 additions & 0 deletions tests/MasaWasmApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using MasaWasmApp;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddMasaBlazor();

builder.Services.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("oidc", options.ProviderOptions);
});

builder.Services.AddAuthorizationCore(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});

await builder.Build().RunAsync();
40 changes: 40 additions & 0 deletions tests/MasaWasmApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58793",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7245;http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
53 changes: 53 additions & 0 deletions tests/MasaWasmApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@inherits LayoutComponentBase

<MApp>
<MAppBar App>
<MAppBarNavIcon @onclick="() => _drawer = !_drawer"></MAppBarNavIcon>
<MToolbarTitle>MASA Blazor</MToolbarTitle>
<MSpacer></MSpacer>
<MButton Text Color="primary" Target="_blank" Href="https://docs.masastack.com/blazor/introduction/why-masa-blazor">About</MButton>
</MAppBar>

<MNavigationDrawer App @bind-Value="_drawer">
<MList Nav Routable>
<MListItem Href="/" ActiveClass="primary--text">
<MListItemIcon>
<MIcon>mdi-home</MIcon>
</MListItemIcon>
<MListItemContent>
<MListItemTitle>Home</MListItemTitle>
</MListItemContent>
</MListItem>
<MListItem Href="/counter" ActiveClass="primary--text">
<MListItemIcon>
<MIcon>mdi-plus</MIcon>
</MListItemIcon>
<MListItemContent>
<MListItemTitle>Counter</MListItemTitle>
</MListItemContent>
</MListItem>
<MListItem Href="/fetchdata" ActiveClass="primary--text">
<MListItemIcon>
<MIcon>mdi-list-box</MIcon>
</MListItemIcon>
<MListItemContent>
<MListItemTitle>Fetch data</MListItemTitle>
</MListItemContent>
</MListItem>
</MList>
</MNavigationDrawer>

<MMain>
<MContainer Fluid>
<MErrorHandler>
@Body
</MErrorHandler>
</MContainer>
</MMain>
</MApp>

@code {

private bool? _drawer;

}
8 changes: 8 additions & 0 deletions tests/MasaWasmApp/Shared/RedirectToLogin.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@inject NavigationManager Navigation
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@code {
protected override void OnInitialized()
{
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
}
}
20 changes: 20 additions & 0 deletions tests/MasaWasmApp/Shared/SurveyPrompt.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<MAlert Type="AlertTypes.Info" Text Icon="@("mdi-pencil")" Class="mt-2">
<strong>@Title</strong>

<span class="text-nowrap">
More Information go to
<a target="_blank"
class="font-weight-bold"
href="https://docs.masastack.com/blazor/introduction/why-masa-blazor">
MASA Blazor
</a>.
</span>
</MAlert>

@code {

// Demonstrates how a parent component can supply parameters
[Parameter]
public string? Title { get; set; }

}
19 changes: 19 additions & 0 deletions tests/MasaWasmApp/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using BlazorComponent
@using Masa.Blazor
@using Masa.Blazor.Presets
@using MasaWasmApp
@using MasaWasmApp.Shared
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Microsoft.AspNetCore.Components.Authorization
@* authorization all blazor pages *@
@* https://stackoverflow.com/questions/60687879/require-authorization-on-all-blazor-pages *@
@* @attribute [Authorize] *@
12 changes: 12 additions & 0 deletions tests/MasaWasmApp/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"oidc": {
"Authority": "https://localhost:5000/",
"ClientId": "blazor",
"DefaultScopes": [
"openid",
"profile"
],
"PostLogoutRedirectUri": "/",
"ResponseType": "code"
}
}
Loading

0 comments on commit bf13bbb

Please sign in to comment.