Global Filters VS Middleware - .net-core

I have wrote some API in my project for getting some data. Now I have a condition for filtering result data. I don't want to repeat the same filtering in each controller, so I decided filtering result after executing. I have to option: using result filter (after fetching data) or using middleware where I will filter data in each request. Could you explain me when I should use result filter and when middleware. Is there any difference in these approaches?

This is the request pipeline for ASP.NET Core Web API
The result filter will be executed at the last of the execution pipeline, so if you do not want to execute the entire pipeline just for
filter better use middleware.
Middleware does not have access to ASP.NET MVC context, Model binding, IActionResult, ModelState so if you need any of these you will have to use filters otherwise middleware.

Related

How to properly mock API results for testing with NextJS and MSW?

First of all, if I should ask this question elsewhere please let me know, as I'm not sure where it belongs.
I have a working application with NextJS and MSW, where I can capture the server-side API requests with MSW and return mocked JSON results.
The issue I have is that there are probably about 15 API calls that I need to mock in order to test my application properly
While I could just call each one of these manually, copy and paste the results into a file and then just return that data from the file when I capture the API call, this not a very scalable solution, especially if the back-end API changes.
Question: What are your best methods for automating the generation of these results?
In the past I have created JSON files that have all of the URL paths and query parameters explicitely listed, and I would parse through this file and query every endpoint, and then I had template files which would be used to re-populate my fixtures directory with all of the mocked responses, however this was also very cumbersome.
(For reference, the API has a somewhat similar structure to this one: https://api.gouv.fr/documentation/api-geo, where there are multiple endpoints for fetching data, and each endpoint supports a number of different query parameters to tweak the call.)

Pact Request That Depends on the Response from A Previous Request

I am using the Pact framework to test some APIs from a service. I have one API that initiates some backend execution. Let's call it request A and the response returns a unique execution ID. The second API (request B) send the execution ID returned from request A to pull the execution status. How do I set up the pact test in this case? The problem is the execution ID that is generated dynamically. I know a provider can inject some provider state to the consumer. So potentially, the execution ID could be injected. But I am not sure how to make the injection from the provider side. It requires access to the response from the response A and inject the execution ID (with the provider state callback, perhaps) for the second request.
You need to have a lot of control over what is happening in your provider for Pact to work for you.
Each interaction is verified individually (and in some frameworks, in a random order), and all state should be cleared in between interactions, so you need to use provider states to set up any data that would have been created by the initial request. In regards to something like the execution IDs, you could use a different implementation of the code that generates the IDs that you only use for Pact Tests.

ASP.NET MVC Controllers/Views invoked

When I invoke a controller, it's possible that the view response calls other controllers. Is there a way to determine, in MVC, all of the views and/or controllers were called in a single response?
Thanks.
I don't think that the framework exposes this directly, but you could hook into the OnActionExecuted method and log each action that gets invoked along with a unique request identifier. You could hook into Application_BeginRequest in global.asax.cs to generate a GUID to use as the unique id for that request (stored in the Session, but overwritten for each new request). If you use a base controller and derive all your controllers from it, you could put the logging in the base controllers OnActionExecuted method to keep it DRY.
Alternatively, you could look at creating a custom ActionInvoker and put the logging there.
This is where I would start anyway, though, there might be a better way.
NOTE: This will only tie together actions that are invoked server-side for the request. Any AJAX requests kicked off client-side when client receives the rendered view will show up as different requests. If you need to include those as well, your unique id generation code should probably only run on non-AJAX requests, leaving the existing id alone in the session otherwise. Typically AJAX requests have an HTTP_X_Requested_With header to distinguish them.

ASP.Net asynch operations

Using a Generic Handler, I'd like to 2 do things in parallel. First, I need to send a request to a 3rd part and get the result back (this can take up to 25 seconds) and while that is happening, parse some XML and insert a record into a database.
How would I go about issuing an Http request (like GetResponse()) but not have it pause until it gets a response?
Create an asynchronous HTTP handler class. In your BeginProcessRequest, you can create two tasks, one for the 3rd party request, one for the XML parse and then run them in parallel. Here's a pretty good tutorial that shows how to define tasks that return values and run them in parallel.
Another option is creating a web service class in your project and having the web service do the work for the 3rd party request. Methods in a web service can be called asynchronously, so you can create a method in the service, reference the service from your page, then use the asynchronous overload of the method.

Should I be relying on WebTests for data validation?

I have a suite of web tests created for a web service. I use it for testing a particular input method that updates a SQL Database. The web service doesn't have a way to retrieve the data, that's not its purpose, only to update it. I have a validator that validates the response XML that the web service generates for each request. All that works fine.
It was suggested by a teammate that I add data validation so that I check the database to see the data after the initial response validator runs and compare it with what was in the input request. We have a number of services and libraries that are separate from the web service I'm testing that I can use to get the data and compare it. The problem is that when I run the web test the data validation always fails even when the request succeeds. I've tried putting the thread to sleep between the response validation and the data validation but to no avail; It always gets the data from before the response validation. I can set a break point and visually see that the data has been updated in the DB, funny thing is when I step through it in debug with the breakpoint it does validate successfully.
Before I get too much more into this issue I have to ask; Is this the purpose of web tests? Should I be able to validate data through service calls in this manner or am I asking too much of a web test and the response validation is as far as I should go?
That is not asking too much of the test, just make sure the database test gets called after you yield the WebTestRequest for the WebService call.
So in that case, the database check is separate from the call.
Post code for your webtest if there are still issues.

Resources