test: add unit tests for pkg/server/response package#201
test: add unit tests for pkg/server/response package#201mason5052 wants to merge 1 commit intovxcontrol:masterfrom
Conversation
Add unit tests for HttpError type (constructor, accessors, error interface), predefined error variables (HTTP codes, error codes for 12 categories), Success/Error response functions with gin test context including dev mode vs production mode behavior for error detail exposure.
There was a problem hiding this comment.
Pull request overview
Adds initial unit test coverage for the pkg/server/response package to validate HttpError behavior, a sample of predefined HttpError variables, and the JSON contract of Success/Error response helpers (including dev vs production behavior around exposing original errors).
Changes:
- Add unit tests for
HttpErrorconstructor/accessors anderrorinterface formatting. - Add table-driven assertions for selected predefined
*HttpErrorvariables (HTTP status + error code). - Add gin-based tests for
SuccessandErrorJSON responses, including develop-mode vs production-mode error detail exposure.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func TestErrorResponse_DevMode(t *testing.T) { | ||
| // Enable dev mode | ||
| version.PackageVer = "" | ||
|
|
There was a problem hiding this comment.
This test mutates the global version.PackageVer to force dev mode but never restores the prior value. If PackageVer is set via build flags (or future tests set it), this can leak state across tests in this package. Capture the current value before changing it and defer restoring it at the end of the test.
| version.PackageVer = "1.0.0" | ||
| defer func() { version.PackageVer = "" }() |
There was a problem hiding this comment.
TestErrorResponse_ProductionMode resets version.PackageVer to an empty string unconditionally. If PackageVer had a non-empty value before the test (e.g., injected via ldflags), this will not restore the original state. Store the previous value and restore it in the deferred function instead of hardcoding "".
| version.PackageVer = "1.0.0" | |
| defer func() { version.PackageVer = "" }() | |
| originalPackageVer := version.PackageVer | |
| version.PackageVer = "1.0.0" | |
| defer func() { version.PackageVer = originalPackageVer }() |
Description of Change
Problem: The
pkg/server/responsepackage has no unit test coverage. This package defines the HttpError type, 90+ predefined error variables, and the Success/Error HTTP response functions used across all API endpoints.Solution: Add unit tests for HttpError type (constructor, accessors, error interface implementation), predefined error variables (HTTP codes and error codes across 12 domain categories), and Success/Error response functions with gin test context including dev mode vs production mode behavior for error detail exposure.
Type of Change
Areas Affected
Testing and Verification
Test Configuration
Test Steps
go test ./pkg/server/response/... -vTest Results
Checklist
go fmtandgo vetrun