Testing Network Requests
Relevant source files
The following files were used as context for generating this wiki page:
The networking layer in Social-Connect is built around a centralized APIClient that manages HTTP communication with the backend services. Testing these network requests involves understanding the interaction between high-level services like UserService, the underlying URLSession data tasks, and the authentication mechanisms managed by the KeychainManager.
This system ensures that requests to specific endpoints—such as following or unfollowing users—are properly authenticated and that the application state is updated upon successful completion of these operations.
Network Architecture and Data Flow
The networking architecture follows a layered approach where requests are initiated by services or handlers and processed by a core client.
Core Networking Components
The primary components involved in the network request lifecycle are:
| Component | Responsibility |
|---|---|
APIClient | Handles the construction of URLRequest objects, manages headers, and executes tasks using URLSession. |
UserService | Provides high-level methods for user-specific actions like followUser and unfollowUser. |
KeychainManager | Retrieves the stored authentication token required for authorized requests. |
DeepLinkHandler | Triggers network requests based on incoming external URLs. |
Sources: APIClient.swift:3-5, UserService.swift:3-6, KeychainManager.swift:6-8, DeepLinkHandler.swift:3-6
Request Flow Diagram
The following diagram illustrates the flow of a network request initiated by a deep link, passing through the service layer to the API client.
The flow shows how an external trigger (Deep Link) results in an authenticated network request and a subsequent local state update via notifications. Sources: AppDelegate.swift:16-19, DeepLinkHandler.swift:30-38, UserService.swift:9-15, APIClient.swift:18-21
API Implementation Details
Request Configuration
All outgoing POST requests are configured with a base URL of https://api.socialconnect.com/v1/ and include specific headers.
- Content-Type: Always set to
application/json. - Authorization: Includes a
Bearertoken if one is available in theKeychainManager. - Validation: The system considers any status code in the
200...299range as a successful request.
Sources: APIClient.swift:4-16, APIClient.swift:36-38
User Service Endpoints
The UserService abstracts the specific URL paths for user interactions.
| Action | Endpoint Path | Implementation |
|---|---|---|
| Follow User | users/{userId}/follow | UserService.followUser(userId:completion:) |
| Unfollow User | users/{userId}/unfollow | UserService.unfollowUser(userId:completion:) |
Sources: UserService.swift:9-25
Authentication Integration
Network requests are dependent on the KeychainManager to provide security credentials. The APIClient attempts to fetch an authToken during request construction.
// APIClient.swift
if let token = KeychainManager.shared.getAuthToken() {
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
Sources: APIClient.swift:18-21, KeychainManager.swift:10-25
State Synchronization
Successful network requests often trigger side effects to keep the UI in sync. The UserService utilizes NotificationCenter to broadcast changes when follow or unfollow operations succeed.
- Notification Name:
.userFollowStatusChanged - Trigger: Invoked within the
apiClient.postcompletion handler only ifsuccessis true.
Sources: UserService.swift:12-14, UserService.swift:21-23
Conclusion
Testing network requests in Social-Connect requires verifying the integrity of the APIClient's request construction, ensuring the KeychainManager correctly provides tokens, and confirming that UserService properly handles responses by notifying the rest of the application. The system's reliance on URLSession and NotificationCenter provides a standard pattern for managing asynchronous data operations and local state consistency.