# Write Unit Tests for a Gorilla Mux REST API

Yes. An agent writes the first Go tests for a Gorilla Mux API with httptest, pins what each handler really returns, runs them with the race detector, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/write-unit-tests/gorilla-mux/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the whole API before it writes a test, which in a repository like saloneepathan/task-manager-api takes a few minutes: `go.mod` declares `go 1.24.4` with `github.com/gorilla/mux` v1.8.1 and `github.com/google/uuid` v1.6.0; `routes/routes.go` registers `GET` and `POST /tasks` and `GET`, `PUT` and `DELETE /tasks/{id}` with `.Methods(...)`; `handlers/task_handler.go` holds the five handlers; `storage/memory_store.go` is one line, `var Tasks = []models.Task{}`; `models/task.go` is a struct with `ID`, `Title` and `Status`. `go test ./...` prints `[no test files]` for all five packages, and the Makefile's `test` target exists for the day someone adds one. The Dockerfile builds on `golang:1.21-alpine`, older than the `go.mod` line, which the agent notes and does not fix.

Then it runs each handler through `httptest` to learn what the tests must pin, because several behaviours are not what the README implies. `CreateTask` ignores the decode error, so a body of `not json` is accepted and stored with an empty title; it overwrites any `status` the client sends with `pending`; and it answers `200`, not `201`. `GetTask` sets a JSON content type and then calls `http.Error`, which replaces it, so a missing id comes back as `404` with `text/plain; charset=utf-8` and the body `Task not found`. `UpdateTask` decodes the body straight into the stored struct, so a `PUT` with only `status` keeps the old title, and an `id` in the body is written and then restored. `DeleteTask` returns the deleted task. A `PATCH` is `405` from the router, since `.Methods` registered none.

The tests go in `handlers/task_handler_test.go` as `package handlers_test`, because `routes` imports `handlers` and an internal test package importing `routes` would be an import cycle. Each test resets `storage.Tasks` to an empty slice, builds the router with `routes.RegisterRoutes()`, and sends `httptest.NewRequest` through `ServeHTTP` so the `{id}` variables are set the way they are in production, rather than calling a handler function with a bare request that `mux.Vars` cannot read. One test per route covers the happy path and the not-found path, and the surprising behaviours above are pinned with a comment saying they are current behaviour, not a specification. A short `routes/routes_test.go` asserts the method table, including the `405`.

The agent also writes a test that fires concurrent `POST /tasks` and `GET /tasks` requests and runs the package with `go test -race`. `storage.Tasks` is a package-level slice that `CreateTask` appends to and every other handler ranges over with no mutex, so the race detector reports a write in `CreateTask` racing reads in the other handlers. Since the prompt says not to change production code, the test is kept, marked `t.Skip` with a one-line reason until a lock is added, and the report goes into the pull request as a finding. It runs `go vet ./...`, `gofmt -l` on the new files and `go test ./... -race -v` before opening the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write unit tests for this Gorilla Mux API. Read go.mod, main.go, routes/routes.go, handlers/task_handler.go, storage/memory_store.go, models/task.go, Makefile and Dockerfile first. Put the handler tests in handlers/task_handler_test.go as an external test package so they can build the router with routes.RegisterRoutes() and send httptest requests through ServeHTTP, resetting storage.Tasks before each test. Cover every route's success and not-found paths and pin the current behaviour exactly: the status code and content type CreateTask and GetTask really return, what CreateTask does with an invalid body and a client-supplied status, what a partial PUT body does, what DeleteTask returns, and the 405 for an unregistered method. Add a concurrent create-and-list test and run go test ./... -race -v; do not change production code, and if the race detector reports storage.Tasks, keep the test, skip it with a reason, and write the report into the pull request as a finding. Run go vet ./... and gofmt -l on the new files, and open a pull request that pastes the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `handlers/task_handler_test.go` and `routes/routes_test.go` and changes nothing in `handlers/`, `routes/`, `storage/`, `models/` or `main.go`. The description pastes the `go vet`, `gofmt -l` and `go test ./... -race -v` output from the sandbox, with the race detector's report quoted under its own heading.

The findings are the point of the description. The race is one: `CreateTask` appends to `storage.Tasks` while the other handlers range over it, and the pull request shows the few-line fix, a `sync.RWMutex` in `storage` taken by every handler, and leaves it to the reviewer. The rest are behaviours the tests now pin as questions: a create that answers `200` and accepts an invalid body, a not-found that is `text/plain`, and a `PUT` that silently merges. Each test that pins one of these carries a comment so a later change to the intended behaviour fails loudly in the right place.

Before merging, a reviewer decides whether to apply the mutex in the same pull request or a follow-up and un-skips the concurrent test once it is applied, and reads the pinned behaviours to decide which are bugs to fix next, in which case the test is changed to the intended response in the same change as the handler.

## Run it on every pull request that adds a route

An automation in the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` runs this prompt against a pull request that touches `routes/routes.go` or `handlers/`; the trigger adds the pull request to the prompt, and the run clones that branch, writes tests for the route that changed, runs them with the race detector and reports on the pull request. Pull requests from forks never start a run.

A team that wants first tests across several Go services instead sends one `POST /o/{org}/sessions` per repository from a script, with this prompt and the repository in `resources`, and a `request_id` that is a UUID v5 of the repository's full name so a retry returns the run already started. Each run opens at most one pull request.

## Why run it on Rush?

The first tests for an API like this are mostly discovery: finding out that a create answers `200`, that a not-found is plain text, that a `PUT` merges, and that the in-memory store races. None of that is written down anywhere, and all of it is wrong to assume. The agent learns it by sending real requests through the real router in its own sandbox and pins what it saw, so the pull request is a description of the API as it is, with the race report from a `-race` run that already happened.

Every command and its output stays on the run, so a reviewer reads the race detector's report and the test log without checking out the branch. Several Go services can get the same first tests at once, each in its own sandbox, and a problem the tests expose is reported in the pull request rather than quietly worked around by a weaker assertion.

## Frequently asked questions

### Why are the handler tests an external test package?

`routes/routes.go` imports `handlers`, so a file in `package handlers` that imports `routes` is an import cycle and does not compile. Declaring the test file as `package handlers_test` makes it an external test, which may import `routes`, build the real router with `routes.RegisterRoutes()` and send requests through `ServeHTTP`, so `mux.Vars` sees the `{id}` variable exactly as in production.

### Why pin a 200 on create instead of asserting 201?

Because `CreateTask` writes the JSON body and never calls `WriteHeader`, so it answers `200` today, and a test that asserts `201` fails on day one for a reason unrelated to the test. The agent pins the current response with a comment naming it as current behaviour and lists it in the pull request as a question; changing the handler and the assertion together is one small follow-up.

### What does the race detector report?

A write in `CreateTask`, the `append` to `storage.Tasks`, racing reads in the handlers that range over the same slice, with no lock anywhere. The concurrent test that triggers it is kept but skipped with a reason until a `sync.RWMutex` is added in `storage`, and the pull request quotes the report and shows the fix.

### Why is the not-found response plain text?

`GetTask` sets `Content-Type: application/json` and then calls `http.Error`, which sets `text/plain; charset=utf-8` and writes `Task not found`. The test asserts the status and the body it actually gets, so a later change to a JSON error, which the agent suggests, fails that test at the right line.

### Does the race test need cgo?

Yes, `-race` needs a C compiler, which the sandbox has. The repository has no CI, so the pull request also shows the exact `go test ./... -race -v` line the Makefile's `test` target could run once a workflow exists; adding one is a separate job.

## More Gorilla Mux jobs

- [Replace gorilla/mux With the Go Standard Library Router](https://getrush.ai/run/migrate-gorilla-mux-to-net-http/gorilla-mux/)

## The same job on other stacks

- [Write Unit Tests for a grammY Telegram Bot](https://getrush.ai/run/write-unit-tests/grammy/)
- [Write Jest Unit Tests for the Untested Modules of an Express API](https://getrush.ai/run/write-unit-tests/jest/)
- [Write the Missing Jest Tests for a Koa API](https://getrush.ai/run/write-unit-tests/koa/)
- [Write Unit Tests for a LangChain.js App](https://getrush.ai/run/write-unit-tests/langchain/)
- [Write Unit Tests for a Laravel Project](https://getrush.ai/run/write-unit-tests/laravel/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
