Skip to content

Commit

Permalink
Add unit tests for IResult
Browse files Browse the repository at this point in the history
  • Loading branch information
ycanardeau committed May 25, 2024
1 parent 5ba6f1b commit 9e9eaff
Show file tree
Hide file tree
Showing 35 changed files with 1,269 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/antiforgery/test/DefaultAntiforgery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getServices(): IServiceProvider {
function getHttpContext(
antiforgeryFeature?: IAntiforgeryFeature,
): IHttpContext {
const httpContext = HttpContext.create();
const httpContext = HttpContext.createWithDefaultFeatureCollection();
antiforgeryFeature = antiforgeryFeature ?? new AntiforgeryFeature();
httpContext.features.set(IAntiforgeryFeature, antiforgeryFeature);
httpContext.requestServices = getServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export class AuthenticationProperties {
this.parameters = parameters ?? new Map();
}

static create(): AuthenticationProperties {
return new AuthenticationProperties(undefined, undefined);
}

static createWithItems(
items: Map<string, string | undefined>,
): AuthenticationProperties {
return new AuthenticationProperties(items, undefined);
}

protected getString(key: string): string | undefined {
const tryGetValueResult = tryGetValue(this.items, key);
return tryGetValueResult.ok ? tryGetValueResult.val : undefined;
Expand Down Expand Up @@ -64,8 +74,8 @@ export class AuthenticationProperties {
? tryGetValueResult.val === 'true'
? true
: tryGetValueResult.val === 'false'
? false
: undefined
? false
: undefined
: undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class AuthenticationTicket {

constructor(
readonly principal: ClaimsPrincipal,
properties = new AuthenticationProperties(undefined, undefined),
properties = AuthenticationProperties.create(),
readonly authenticationScheme: string,
) {
this.properties = properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,7 @@ export class CookieAuthenticationHandler extends SignInAuthenticationHandler<Coo

protected async handleSignIn(
user: ClaimsPrincipal,
properties: AuthenticationProperties = new AuthenticationProperties(
undefined,
undefined,
),
properties: AuthenticationProperties = AuthenticationProperties.create(),
): Promise<void> {
this.signInCalled = true;

Expand Down Expand Up @@ -384,8 +381,7 @@ export class CookieAuthenticationHandler extends SignInAuthenticationHandler<Coo
protected async handleSignOut(
properties: AuthenticationProperties | undefined,
): Promise<void> {
properties =
properties ?? new AuthenticationProperties(undefined, undefined);
properties = properties ?? AuthenticationProperties.create();

this.signOutCalled = true;

Expand Down
3 changes: 1 addition & 2 deletions packages/authentication/src/PropertiesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export abstract class PropertiesContext<
) {
super(context, scheme, options);

this.properties =
properties ?? new AuthenticationProperties(undefined, undefined);
this.properties = properties ?? AuthenticationProperties.create();
}
}
2 changes: 1 addition & 1 deletion packages/authentication/src/PropertiesSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class PropertiesSerializer
const value = reader.readString();
extra.set(key, value);
}
return new AuthenticationProperties(extra, undefined);
return AuthenticationProperties.createWithItems(extra);
}

deserialize(data: Buffer): AuthenticationProperties | undefined {
Expand Down
3 changes: 1 addition & 2 deletions packages/authentication/src/SignInAuthenticationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export abstract class SignInAuthenticationHandler<
? signIn(this.context, target, user, properties)
: this.handleSignIn(
user,
properties ??
new AuthenticationProperties(undefined, undefined),
properties ?? AuthenticationProperties.create(),
);
}
}
5 changes: 2 additions & 3 deletions packages/authentication/src/SignOutAuthenticationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export abstract class SignOutAuthenticationHandler<
return target !== undefined
? signOut(this.context, target, properties)
: this.handleSignOut(
properties ??
new AuthenticationProperties(undefined, undefined),
);
properties ?? AuthenticationProperties.create(),
);
}
}
8 changes: 4 additions & 4 deletions packages/authentication/test/TicketSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { expect, test } from 'vitest';
// https://github.com/dotnet/aspnetcore/blob/00d0038f937f0059a847fde504649fe33ec935e0/src/Security/Authentication/test/TicketSerializerTests.cs#L12
test('CanRoundTripEmptyPrincipal', () => {
const serializer = new TicketSerializer();
const properties = new AuthenticationProperties(undefined, undefined);
const properties = AuthenticationProperties.create();
properties.redirectUri = 'bye';
const ticket = new AuthenticationTicket(
new ClaimsPrincipal(),
Expand All @@ -42,7 +42,7 @@ test('CanRoundTripEmptyPrincipal', () => {
// https://github.com/dotnet/aspnetcore/blob/00d0038f937f0059a847fde504649fe33ec935e0/src/Security/Authentication/test/TicketSerializerTests.cs#L33
test('CanRoundTripBootstrapContext', () => {
const serializer = new TicketSerializer();
const properties = new AuthenticationProperties(undefined, undefined);
const properties = AuthenticationProperties.create();

const ticket = new AuthenticationTicket(
new ClaimsPrincipal(),
Expand Down Expand Up @@ -84,7 +84,7 @@ test('CanRoundTripBootstrapContext', () => {
// https://github.com/dotnet/aspnetcore/blob/00d0038f937f0059a847fde504649fe33ec935e0/src/Security/Authentication/test/TicketSerializerTests.cs#L55
test('CanRoundTripActorIdentity', () => {
const serializer = new TicketSerializer();
const properties = new AuthenticationProperties(undefined, undefined);
const properties = AuthenticationProperties.create();

const actor = new ClaimsIdentity(
undefined,
Expand Down Expand Up @@ -135,7 +135,7 @@ test('CanRoundTripActorIdentity', () => {
// https://github.com/dotnet/aspnetcore/blob/00d0038f937f0059a847fde504649fe33ec935e0/src/Security/Authentication/test/TicketSerializerTests.cs#L84
test('CanRoundTripClaimProperties', () => {
const serializer = new TicketSerializer();
const properties = new AuthenticationProperties(undefined, undefined);
const properties = AuthenticationProperties.create();

const claim = new Claim(
'type',
Expand Down
1 change: 1 addition & 0 deletions packages/extensions.logging.abstractions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './ILoggerT';
export * from './LoggerExtensions';
export * from './LoggerT';
export * from './LogLevel';
export * from './NullLogger';
export * from './NullLoggerFactory';
12 changes: 6 additions & 6 deletions packages/hosting/test/HostingApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function createApp(
// https://github.com/dotnet/aspnetcore/blob/e0aed17a16f5f6e5540925ef849bf87358fa10b3/src/Hosting/Hosting/test/HostingApplicationTests.cs#L22C17-L22C85
test('DisposeContextDoesNotClearHttpContextIfDefaultHttpContextFactoryUsed', () => {
const hostingApp = createApp();
const httpContext = HttpContext.create();
const httpContext = HttpContext.createWithDefaultFeatureCollection();

const context = hostingApp.createContext(httpContext.features);
expect(context.httpContext).not.toBeUndefined();
Expand All @@ -91,9 +91,9 @@ test('DisposeContextDoesNotClearHttpContextIfDefaultHttpContextFactoryUsed', ()
test('CreateContextReinitializesPreviouslyStoredDefaultHttpContext', () => {
const hostingApp = createApp();
const features = new FeaturesWithContext<HostingAppContext>(
HttpContext.create().features,
HttpContext.createWithDefaultFeatureCollection().features,
);
const previousContext = HttpContext.create();
const previousContext = HttpContext.createWithDefaultFeatureCollection();
features.hostContext = new HostingAppContext();
features.hostContext.httpContext = previousContext;

Expand All @@ -115,9 +115,9 @@ test('CreateContextCreatesNewContextIfNotUsingDefaultHttpContextFactory', () =>

const hostingApp = createApp(factory);
const features = new FeaturesWithContext<HostingAppContext>(
HttpContext.create().features,
HttpContext.createWithDefaultFeatureCollection().features,
);
const previousContext = HttpContext.create();
const previousContext = HttpContext.createWithDefaultFeatureCollection();
// Pretend like we had previous HttpContext
features.hostContext = new HostingAppContext();
features.hostContext.httpContext = previousContext;
Expand All @@ -137,7 +137,7 @@ class TestHttpActivityFeature implements IHttpActivityFeature {
// https://github.com/dotnet/aspnetcore/blob/e0aed17a16f5f6e5540925ef849bf87358fa10b3/src/Hosting/Hosting/test/HostingApplicationTests.cs#L165C17-L165C67
test('IHttpActivityFeatureIsNotPopulatedWithoutAListener', () => {
const hostingApp = createApp();
const httpContext = HttpContext.create();
const httpContext = HttpContext.createWithDefaultFeatureCollection();
httpContext.features.set<IHttpActivityFeature>(
IHttpActivityFeature,
new TestHttpActivityFeature(),
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/HttpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class HttpContext implements IHttpContext {
this.response = new HttpResponse(this);
}

static create(): HttpContext {
static createWithDefaultFeatureCollection(): HttpContext {
const httpContext = new HttpContext(new FeatureCollection());
httpContext.features.set<IHttpRequestFeature>(
IHttpRequestFeature,
Expand Down
8 changes: 4 additions & 4 deletions packages/http/test/HttpContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ test('RequestServicesAreNotOverwrittenIfAlreadySet', () => {
IServiceScopeFactory,
);

const context = HttpContext.create();
const context = HttpContext.createWithDefaultFeatureCollection();
context.serviceScopeFactory = scopeFactory;
context.requestServices = serviceProvider;

Expand Down Expand Up @@ -247,7 +247,7 @@ test('RequestServicesAreDisposedOnCompleted', async () => {
);
let instance: DisposableThing | undefined = undefined;

const context = HttpContext.create();
const context = HttpContext.createWithDefaultFeatureCollection();
context.serviceScopeFactory = scopeFactory;
const responseFeature = new TestHttpResponseFeature();
context.features.set<IHttpResponseFeature>(
Expand Down Expand Up @@ -336,7 +336,7 @@ test('RequestServicesAreDisposedAsyncOnCompleted', async () => {
);
let instance: DisposableThing | undefined = undefined;

const context = HttpContext.create();
const context = HttpContext.createWithDefaultFeatureCollection();
context.serviceScopeFactory = scopeFactory;
const responseFeature = new TestHttpResponseFeature();
context.features.set<IHttpResponseFeature>(
Expand Down Expand Up @@ -365,7 +365,7 @@ test('RequestServicesAreDisposedAsyncOnCompleted', async () => {

// https://github.com/dotnet/aspnetcore/blob/95b1de9ccf67adbc57919132464dac44c20e92b8/src/Http/Http/test/DefaultHttpContextTests.cs#L261C17-L261C48
test('InternalActiveFlagIsSetAndUnset', () => {
const context = HttpContext.create();
const context = HttpContext.createWithDefaultFeatureCollection();

expect(context.active).toBe(false);

Expand Down
10 changes: 2 additions & 8 deletions packages/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ export async function main(): Promise<void> {
undefined,
);

const authProperties = new AuthenticationProperties(
undefined,
undefined,
);
const authProperties = AuthenticationProperties.create();

await signIn(
context,
Expand All @@ -126,10 +123,7 @@ export async function main(): Promise<void> {
});

mapGet(app, '/signOut', async (context) => {
const authProperties = new AuthenticationProperties(
undefined,
undefined,
);
const authProperties = AuthenticationProperties.create();

await signOut(
context,
Expand Down
27 changes: 27 additions & 0 deletions packages/mvc.core/src/HttpActionResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ActionContext, IResult } from '@yohira/mvc.abstractions';

import { ActionResult } from './ActionResult';

// https://source.dot.net/#Microsoft.AspNetCore.Mvc.Core/HttpActionResult.cs,da90aa64087eee48,references
/**
* An {@link ActionResult} that when executed will produce a response based on the {@link IResult} provided.
*/
export class HttpActionResult extends ActionResult {
/**
* Initializes a new instance of the <see cref="HttpActionResult"/> class with the
* {@link IResult} provided.
* @param result The {@link IResult} instance to be used during the {@link executeResult} invocation.
*/
constructor(
/**
* Gets the instance of the current {@link IResult}.
*/
readonly result: IResult,
) {
super();
}

executeResult(context: ActionContext): Promise<void> {
return this.result.execute(context.httpContext);
}
}
4 changes: 4 additions & 0 deletions packages/mvc.core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './dependency-injection/MvcCoreServiceCollectionExtensions';
export * from './infrastructure/ContentResultExecutor';
export * from './infrastructure/RedirectResultExecutor';
export * from './ActionResult';
export * from './BadRequestResult';
export * from './ChallengeResult';
Expand All @@ -9,6 +11,8 @@ export * from './FileContentResult';
export * from './FileResult';
export * from './FileStreamResult';
export * from './ForbidResult';
export * from './HttpActionResult';
export * from './IStatusCodeActionResult';
export * from './JsonResult';
export * from './NoContentResult';
export * from './NotFoundResult';
Expand Down
10 changes: 10 additions & 0 deletions packages/mvc.core/test/BadRequestResult.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StatusCodes } from '@yohira/http.abstractions';
import { BadRequestResult } from '@yohira/mvc.core';
import { expect, test } from 'vitest';

// https://github.com/dotnet/aspnetcore/blob/49c1c68bf1ac4f1f28db5c1b33ec5b7bab35546b/src/Mvc/Mvc.Core/test/BadRequestResultTests.cs#L11
test('BadRequestResult_InitializesStatusCode', () => {
const badRequest = new BadRequestResult();

expect(badRequest.statusCode).toBe(StatusCodes.Status400BadRequest);
});
Loading

0 comments on commit 9e9eaff

Please sign in to comment.