Documentation
¶
Overview ¶
The same tasks API, tested in main_test.go with nothing but the standard library: net/http/httptest, encoding/json and testing.
*iris.Application implements http.Handler once Build has run, so it plugs into any Go HTTP tooling: a ResponseRecorder, a real httptest.Server, an http.Client, a fuzzer, a contract-testing tool. No assertion library and no framework-specific test helper is required.
The sibling ../iris-httptest directory holds the SAME application tested with the framework's own httptest package. Read the two test files side by side to pick the style you prefer.
What makes this application testable is the newApp(cfg, repo) seam: it takes its configuration and its storage as arguments and returns an application that has not started listening. main passes production values, a test passes its own, and both drive the same router, middleware and error map.
Learn: test an Iris application with app.Build() and ServeHTTP on a ResponseRecorder, or on a real loopback server, with no extra dependency. Before: ../iris-httptest
Run:
go test -v ADMIN_PASSWORD=change-me go run main.go
Try:
curl -i -X POST localhost:8080/api/tasks -d '{"title":"buy milk"}' # 201 {"id":"..."}
curl localhost:8080/api/tasks # 200, the task list
curl -i localhost:8080/api/admin/stats # 401, no credentials
curl -u admin:change-me localhost:8080/api/admin/stats # 200 {"open":1,"admin":"admin"}
Next: ../../15-production/serving-and-tls