Exercise: The Request Pipeline

Take a ticket from Riverside Community Library's platform team: every request's checks are written out in one function, and two other teams need them as separate pieces. Build a tiny request pipeline with no framework, turn logging, the sign-in check and timing into middleware, and let the security team add their own check in the middle, with the tests green throughout.

Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated

By

Every framework in this pass has a request pipeline, and in this exercise you build one yourself, with no framework at all. The library's API works today, but every check it runs is written out in one function, and two other teams are waiting to use those checks, or add their own. Your job is to take that function apart into pieces they can arrange, without changing what the API does.

Before you start

You need one language set up, as in Setup: VS Code and Your Language, and to know how to run the tests, as in Running the Exercise Tests. The exercise is written in all five of the course's languages; pick the one you use.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-request-pipeline, also linked from the Exercise box beside the lesson. Clone it into your course folder. In a terminal, in that folder:

git clone https://github.com/ask-codey/exercise-dp-request-pipeline.git

Git makes a folder, exercise-dp-request-pipeline, with the exercise code inside.

The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:

  • the API's code, as it is today: a request and a response, the library's endpoints, a log, and App.Handle, the one function that runs every check (the repository's README.md names the files for each language);
  • the platform team's names for what they want: a Pipeline, and Checks.LogRequests, Checks.SignIn and Checks.Timing, which are there but don't do anything yet;
  • the test team's clock, FakeClock, which only moves when a test tells it to, so timing can be tested;
  • the tests, which you don't need to change;
  • a demo program that runs the library's inventory weekend, with the security team's check written outside the API's code.

In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.

The ticket

From: the platform team, Riverside Community Library

  1. We want each of today's checks as its own piece: logging (a line on the way in, the status on the way out), the sign-in check (anything but a GET needs someone signed in), and timing (how long the rest took). A step is handed the request and next, the rest of the line, and answers the request itself or passes it on.
  2. new Pipeline(endpoint).Use(step)...Handle(request) has to run the steps, in the order they were added, in front of the endpoint.
  3. The security team is adding its own check for the inventory weekend: everything but a GET answers 503 Closed for inventory. They'll put it between the sign-in check and the timing, without editing ours.

App.Handle is what the web server calls for every request, and it has to keep doing exactly what it does today.

What to do

  1. Run the tests first. Four pass: they're what the API does today. Four fail: they're the ticket.
  2. Read App.Handle and find the smells. How many jobs does it do? What would the security team have to edit to add their check?
  3. Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
  4. Stop when all eight are green and the demo prints what When you're done shows.

The README's Stuck? section names the pattern, if you want it. Try a few minutes without it first.

Check your work

Each language has its own command, run in the terminal in your language's folder (Terminal > New Terminal in VS Code). The repository's README.md has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (npm install).

Language Run the tests Run the demo
C# dotnet test dotnet run --project RequestPipeline
Java ./mvnw test (.\mvnw test in PowerShell) java src/main/java/Demo.java
Python python -m pytest python demo.py
JavaScript npm test node demo.js
TypeScript npm test node demo.ts

A failing test's message starts with Not yet: for the ticket's work, or Keep this working: if a change broke something that worked, and says what it expected, for example Not yet: the sign-in step answers by itself, so the endpoint never runs for a stranger.

Hints

  • Start with one check. Make logging work on its own in a pipeline of one step, and get that test green. A step does its work, calls next, and can do more work with what next gives back.
  • Then the pipeline. Keep the steps in a list; to handle a request, wrap them around the endpoint so the first one added runs first.
  • The sign-in check doesn't always call next. That's how a step stops a request.
  • Timing reads the clock twice: before calling next and after. The test's clock moves only while the rest of the line runs.
  • Last, App.Handle. Once the three pieces work, it can be a pipeline of them, in the order its lines run now.

When you're done

All 8 tests are green, and the demo prints the same lines in every language:

GET /books: 200 The Lighthouse Keeper, A Map of Small Things, Rivers of Glass
POST /loans: 401 Please sign in
POST /loans: 503 Closed for inventory
The log:
  GET /books
  GET /books took 12 ms
  GET /books: 200
  POST /loans
  POST /loans: 401
  POST /loans
  POST /loans: 503

The inventory check was written in the demo, outside the API's code, and it sits in the middle of the line without a single edit to the checks around it. That's the test of the fix: the next check (a rate limit, an error page) is one more step, in the right place.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-dp-request-pipeline), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-dp-request-pipeline.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-dp-request-pipeline, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

What's next: problems none of the apps ran into

That's the end of Pass 4. You've built the pieces a framework is made of (a composition root, a container, repositories, controllers, view models and a request pipeline), and you can follow a request through any framework and name what it meets.

Four passes have now met the problems ordinary apps meet. A few patterns solve problems none of these apps ran into, and the course never squeezes a pattern into an app to teach it. Module 6, More Problems, More Patterns, takes those problems one at a time, each in a small example of its own: rules that an online store's marketing team writes for itself, read into a tree of small objects (Interpreter) and then printed and checked without touching every class (Visitor); and a stadium's 50,000 seats, nearly all alike, stored without running out of memory (Flyweight), with expensive things such as database connections shared or made only when first needed (Object Pool and Lazy Loading). After that, Pass 5 rescues an old gym booking system with everything the course has taught.

So what

A request pipeline is a list of small steps and a few lines that wrap them around an endpoint. You've now written one with no framework at all, which means the next framework's app.Use, @app.middleware or OncePerRequestFilter is something you've already built.

Key terms

Related