Skip to content

Table of Contents

Month 5 · Week 2 — Exercises (interceptors, metadata, transcoding)

Standard-library-only Go packages with table-driven tests. They rebuild the mechanics that gRPC middleware relies on — interceptor composition, metadata multi-maps, and grpc-gateway path templates — so go test runs anywhere with no protoc, no google.golang.org/grpc, and no network.

How to run

go test ./exercises/month-05/week-2/...
# or one at a time, verbose:
go test -v ./exercises/month-05/week-2/chain

Prompts

  1. chain/ — Implement Chain(...Interceptor) Interceptor that composes unary interceptors so interceptors[0] is the OUTERMOST layer (first in, last out), the final handler sits in the middle, and an interceptor can short-circuit by not calling next. Context changes must propagate inward. Concepts: middleware composition, the onion model, grpc.ChainUnaryInterceptor (Day 120).

  2. metadata/ — Implement an MD (gRPC metadata.MD) multi-map with CASE-INSENSITIVE keys (Set/Append/Get/New) and a ParseBearer that pulls the token out of an Authorization: Bearer <token> header (case-insensitive scheme, trimmed, ErrNoToken on failure). Concepts: HTTP/2 header case-insensitivity, metadata propagation, auth interceptors (Day 121).

  3. pathtemplate/ — Implement grpc-gateway path matching: Parse a template like /v1/users/{id}/books/{book_id} and Match request paths, extracting the {name} parameters into a map. Reject {} (ErrEmptyParam); ignore leading/trailing slashes; non-match returns (nil, false). Concepts: REST transcoding, path-parameter extraction (Day 123).

Results

Exercise Focus go test
chain interceptor composition / ordering
metadata case-insensitive metadata + bearer parsing
pathtemplate grpc-gateway path templates