Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
16m 21s
When building and testing modern applications, particularly those with web front ends, developers often face the need to simulate real-world scenarios like API delays and unexpected error responses. To support this, Tim Corey presents a highly practical walkthrough in his course lesson titled "Adding Slowdowns and Error Codes - Building a Sample API in C#". In this video, Tim illustrates how to enrich a minimal C# API with artificial delays and custom error responses - invaluable tools for simulating less-than-ideal situations during testing.
In this article, we'll walk you through the concepts and implementations Tim demonstrates in the video.
Tim begins the lesson by reiterating the value of having a sample API when learning web development. Such an API gives you something concrete to test your front-end applications against.
By the end of the course, Tim aims to build a robust API that includes:
In this specific lesson, Tim focuses on simulating slowdowns and generating error codes for realistic API behavior under adverse conditions.
Tim starts by adding an optional delay parameter to specific API endpoints - particularly those dealing with course data. His goal is to simulate slow API responses for better front-end testing.
Task<IResult> instead of just IResult.At 2:33, Tim emphasizes the importance of bounding the delay. He sets a cap at 5 minutes to prevent unreasonable wait times that could make the application unresponsive or appear broken.
if (delay > 300000)
{
delay = 300000;
}
await Task.Delay(delay.Value);If delay > 300000 Then
delay = 300000
End If
Await Task.Delay(delay.Value)This addition ensures that developers can simulate delays up to five minutes, useful for testing timeouts and responsiveness in the client application.
Tim runs a few tests using Postman (or a Postman clone) to verify the delay logic. For instance:
He observes that the actual delay will always be slightly longer than the specified value due to processing overhead - an important real-world nuance. As Tim notes at 5:09, you're not timing the API down to the millisecond but rather simulating a threshold.
Tim doesn't stop with just the "load all courses" endpoint. He wants consistency, so he implements the same delay capability in the "load course by ID" endpoint.
At 6:15, he hits a snag: a naming conflict due to the automatic addition of "Async" to the method name when converting to asynchronous. Tim adjusts both method names to align with the Async naming convention for clarity and consistency.
Testing confirms the implementation:
Next, Tim introduces a valuable tool for API testing: a dedicated endpoint that can simulate various HTTP error codes.
At 9:13, he explains that while some endpoints (like the one returning a course by ID) naturally return 404 for missing data, there's no built-in way to test for other error codes - unless explicitly simulated.
Tim builds a new endpoint at /error/{code} that:
code switch
{
400 => Results.BadRequest(),
401 => Results.Unauthorized(),
403 => Results.Forbid(),
404 => Results.NotFound(),
_ => Results.StatusCode(code)
};Select Case code
Case 400
Results.BadRequest()
Case 401
Results.Unauthorized()
Case 403
Results.Forbid()
Case 404
Results.NotFound()
Case Else
Results.StatusCode(code)
End SelectThis covers both common client-side errors and any custom codes a developer might wish to test against.
At 12:03, he adds this new endpoint to the program via app.AddErrorEndpoints() and marks the error class as static.
Tim now tests the error endpoint by passing various status codes:
This shows the flexibility of the endpoint - not just for error codes, but for any HTTP status code. At 13:04, Tim confirms this approach is ideal for testing how front-end apps handle different server responses.
While he considered naming it /httpcode, he sticks with /error for simplicity, as it's primarily used for simulating error conditions.
Tim wraps up the video by summarizing the improvements made to the API:
At 14:16, Tim stresses the importance of these tools for testing how your app behaves under different API states, such as delayed responses or various server errors.
Although not covered in detail in this video, Tim hints at the next step: Dockerizing the API. This allows developers to run the sample API locally in a self-contained Docker container, making it easier to deploy and share in different environments.
Tim closes the video by reiterating his commitment to building a comprehensive sample API that includes realistic features developers actually need to test against. These include:
The goal is simple but powerful - equip developers with a tool that mimics the quirks of real APIs, so their applications are robust, reliable, and user-friendly.
By the end of this lesson, viewers are equipped with a better understanding of how and why to introduce artificial delays and error responses in their APIs. Tim Corey's approach is methodical, practical, and directly tied to real-world application testing needs. If you're looking to enhance your API testing game, this lesson is an excellent resource to follow - and now you know exactly where to look.
Watch the full video lesson by Tim Corey for hands-on guidance.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy