Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock URLSession Behavior for Testing #11

Merged
merged 6 commits into from
May 10, 2023
Merged

Mock URLSession Behavior for Testing #11

merged 6 commits into from
May 10, 2023

Conversation

mliberatore
Copy link
Contributor

@mliberatore mliberatore commented May 9, 2023

Co-authored by @ashlirankin18

What it Does

Follow-up to #6

In #6 it was proposed that we’d test out the functionality, but timing-wise it didn’t make sense, so we’ve added some basic tests today.

And good news, the tests caught two bugs!

We decided to minimally mock URLSession and URLSessionDataTask with protocol conformances. This unfortunately changes some types in public APIs, but hopefully that's deemed worth it for the future of test writing in these libraries.

Additionally, strictly for time constraints, we only mocked the non-Combine URLSession method as we only intended to test the new async/await public API during this working session.

How I Tested

  1. Ran the new tests.

Notes

See GitHub comments in the code below on the bug fixes caught by tests.

Screenshot

N/A

let behaviors = defaultRequestBehaviors + requestBehaviors

let urlRequest = makeFinalizedRequest(fromOriginalRequest: request.urlRequest, behaviors: behaviors)
let dataTask = makeDataTask(forURLRequest: urlRequest, successHTTPStatusCodes: request.successHTTPStatusCodes, completion: completion)
let dataTask = makeDataTask(forURLRequest: urlRequest, behaviors: behaviors, successHTTPStatusCodes: request.successHTTPStatusCodes, completion: completion)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our test caught this bug that must've been around for a while. This change ensures that the behaviors’ requestDidFinish will get called, whereas before, it was not 😱

@@ -101,7 +101,7 @@ extension NetworkController: NetworkRequestPerformer {

public func send(_ request: any NetworkRequest, requestBehaviors: [RequestBehavior]) async throws -> NetworkResponse {
try await withCheckedThrowingContinuation { continuation in
send(request) { result in
send(request, requestBehaviors: requestBehaviors) { result in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, for our new async/await API, we weren’t passing along behaviors.

@mliberatore mliberatore requested review from bcapps, Pearapps and Twigz May 9, 2023 19:31
@mliberatore mliberatore marked this pull request as ready for review May 9, 2023 19:31
Copy link
Member

@bcapps bcapps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, one super minor comment

Tests/MockNetworkSession.swift Outdated Show resolved Hide resolved
@mliberatore mliberatore merged commit 0489b62 into main May 10, 2023
@mliberatore mliberatore deleted the mock-url-session branch May 10, 2023 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants