feat(tests,config): add tests, response delay, and logging fixes#105
Merged
feat(tests,config): add tests, response delay, and logging fixes#105
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the main function into a testable run() function and adds comprehensive unit tests for the CLI logic. The refactoring enables testing without actually starting the HTTP server by introducing a startServer flag parameter.
- Extracts main logic into testable
run()function with dependency injection for args, env, stderr, and server startup control - Adds
getEnvMap()helper to capture environment variables as a map for testing - Introduces comprehensive unit tests covering config file handling, port precedence, and error scenarios
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| main.go | Refactors main into testable run() function with better error handling and flag parsing |
| main_test.go | Adds unit tests for run() function covering various CLI scenarios and error cases |
| integration_test.go | Adds integration test that starts real HTTP server and exercises endpoints |
| config.go | Introduces JsonContentType constant to reduce string literal duplication |
| responses_writer_test.go | Updates tests to use new JsonContentType constant instead of hardcoded strings |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
9e63794 to
4255bb5
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
201552d to
e25c074
Compare
Add comprehensive for configuration parsing and port precedence. New main_test.go covers missing config file handling, invalid PORT env values, using env port when no flag is provided, flag overriding env, config+flag precedence, and decoding unknown fields. Helpers are added to write temporary config files. Introduce a Delay field on Response (time.Duration) and apply the delay when writing responses. Extract JSON content type into a constant (JsonContentType) and use it when setting the header. Simplify responsesWriter by removing the logger parameter and use slog.Error for error reporting when writing response bodies. Misc: update test logger helper signature to accept testing.TB and adjust imports to include time. These changes enable testing of port/config behavior and add configurable response delays.
e25c074 to
0761f18
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add comprehensive for configuration parsing and port
precedence. New main_test.go covers missing config file handling,
invalid PORT env values, using env port when no flag is provided,
flag overriding env, config+flag precedence, and decoding unknown
fields. Helpers are added to write temporary config files.
Introduce a Delay field on Response (time.Duration) and apply the
delay when writing responses. Extract JSON content type into a
constant (JsonContentType) and use it when setting the header.
Simplify responsesWriter by removing the logger parameter and use
slog.Error for error reporting when writing response bodies.
Misc: update test logger helper signature to accept testing.TB and
adjust imports to include time. These changes enable testing of
port/config behavior and add configurable response delays.