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

Use Mockaco in an ASP.NET Core app #88

Closed
gomete opened this issue Mar 16, 2022 · 4 comments · Fixed by #89
Closed

Use Mockaco in an ASP.NET Core app #88

gomete opened this issue Mar 16, 2022 · 4 comments · Fixed by #89
Labels
enhancement New feature or request

Comments

@gomete
Copy link
Contributor

gomete commented Mar 16, 2022

Hi there,

I have been checking Mockaco to mock HTTP requests and I love how it works and how templating is conceived but it would be nice to extend it using the ASP.NET capabilities. Have you ever considered dividing Mockaco in the current dotnet tool and a package that can be used in an ASP.NET Core app in a similar way YARP did it?

Some of the advantages:

  • Users will be able to add middlewares to the mocking pipeline (e.g, if they want to log the request or whatever).
  • Users can add other controllers or endpoints can be added and manage the priority.
  • Users can use all the packages that ASP.NET Core also has like OpenTelemetry, prometheus-net.AspNetCore, etc.
  • Users can use their custom authentication/authorization.

I did a quick probe of concept and it is almost prepared (as it is using middlewares and the built-in dependency injection) to use it this way and it can be reused in the dotnet tool. If you are interested I can do the changes in a fork and submit a PR to discuss it.

So it could be used (in a similar way to YARP), registering the services and configuration:

    public static IServiceCollection AddCustomMockaco(this IServiceCollection services) =>
		services
	        .AddMockaco()
    		.LoadFromFolder("Mocks")
			.Services;

And then mapping the mock endpoints with the IApplicationBuilder:

Without any customization in the pipeline:

    public static IEndpointConventionBuilder MapCustomMockaco(this IEndpointRouteBuilder app) =>
        app.MapMockaco();

Customizing the pipeline:

    public static IEndpointConventionBuilder MapCustomMockaco(this IEndpointRouteBuilder app) =>
        app.MapMockaco(mockingPipeline =>
        {
            mockingPipeline.UseMiddleware<LoggingMiddleware>();
            mockingPipeline.UseMiddleware<AuditingMiddleware>();
        });
@natenho
Copy link
Owner

natenho commented Mar 16, 2022

Hello @gomete, it sounds great! The idea is to let Mockaco be injected and extended inside any application, right?
I would appreciate if you could share your PoC via PR, and we can discuss it further!

It somehow talks to #66 and #4 as it is some kind of extension aswell.

@natenho natenho added the enhancement New feature or request label Mar 16, 2022
@gomete
Copy link
Contributor Author

gomete commented Mar 17, 2022

Yes, that's the idea @natenho, let me share it today with you via PR.

@gomete
Copy link
Contributor Author

gomete commented Mar 17, 2022

Hi @natenho, PR created #89

@natenho
Copy link
Owner

natenho commented Jun 19, 2022

Hi @gomete, I finally could check out and merge your PR!
We can improve the feature as you suggested in the issue, but I think your current implementation is a good starting point with no breaking change, so I've made it public "as-is".

I had to do some adjustments and to tackle with some conflicts but it seems everything is OK now! The new nuget package is already available at https://www.nuget.org/packages/Mockaco.AspNetCore

Thanks again for the feature and for your patience.

@natenho natenho closed this as completed Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants