Fix HTTP Mock Services expectation setting for Testing#2706
Merged
Umang01-hash merged 5 commits intodevelopmentfrom Dec 12, 2025
Merged
Fix HTTP Mock Services expectation setting for Testing#2706Umang01-hash merged 5 commits intodevelopmentfrom
Umang01-hash merged 5 commits intodevelopmentfrom
Conversation
Umang01-hash
approved these changes
Dec 12, 2025
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.
Support for Multiple Independent HTTP Service Mocks in Testing
Summary
mocks.HTTPService) has been deprecated in favor of a map-based approach (mocks.HTTPServices) that allows each service to have its own isolated mock expectations.What Was Deprecated
mocks.HTTPService(Singular Field)Previous Usage:
Why It Was Deprecated:
Single Mock Instance Limitation: The old approach provided only one mock instance (
mocks.HTTPService) that was shared across all services. This made it impossible to:Confusion in Multi-Service Scenarios: When testing handlers that interact with multiple services (e.g., payment service, shipping service, notification service), developers couldn't distinguish which service was being called, leading to:
Backward Compatibility: The field is kept for backward compatibility but marked as deprecated and will be removed in a future version.
What Has Been Added
1.
mocks.HTTPServices(Map-Based Approach)New Usage:
Key Benefits:
Independent Mock Instances: Each service registered via
WithMockHTTPService()gets its own separate mock instance, allowing:Service Name Mapping: Services are accessed by name using
mocks.HTTPServices["serviceName"], making it clear which service is being configured.Multiple Service Support: Handlers that call multiple HTTP services can now be properly tested with each service having its own mock expectations.
2. Enhanced
WithMockHTTPService()FunctionThe function now:
WithMockHTTPService("service1", "service2", ...)HTTPServicefield3. Comprehensive Testing Documentation
gofr/examples/http-server/main_test.go):mocks.HTTPServices["serviceName"]TestTraceHandlerto use the new patternMigration Guide
For Single Service (Backward Compatible)
Old Way (Still Works):
New Way (Recommended):
Checklist:
goimportandgolangci-lint.