From 5b2c3c2866f7d2954e2dd6f2af9f89f95c68ecbb Mon Sep 17 00:00:00 2001 From: Manish Rathi Date: Tue, 4 Apr 2023 19:48:02 +0200 Subject: [PATCH] OR-5242 Practice `URLSession` (#68) --- .../CombineDemo.xcodeproj/project.pbxproj | 4 ++ .../Publishers/Practice/URLSessionTests.swift | 65 +++++++++++++++++++ README.md | 1 + 3 files changed, 70 insertions(+) create mode 100644 CombineDemo/CombineDemoTests/Publishers/Practice/URLSessionTests.swift diff --git a/CombineDemo/CombineDemo.xcodeproj/project.pbxproj b/CombineDemo/CombineDemo.xcodeproj/project.pbxproj index e1fb990..bbae93d 100644 --- a/CombineDemo/CombineDemo.xcodeproj/project.pbxproj +++ b/CombineDemo/CombineDemo.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 9631D29729D70A6900A9D790 /* DefaultError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9631D29629D70A6900A9D790 /* DefaultError.swift */; }; 9631D29929D70D6A00A9D790 /* DefaultErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9631D29829D70D6A00A9D790 /* DefaultErrorTests.swift */; }; 9631D29C29DA736200A9D790 /* NotificationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9631D29B29DA736200A9D790 /* NotificationTests.swift */; }; + 9631D29E29DB503900A9D790 /* URLSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9631D29D29DB503900A9D790 /* URLSessionTests.swift */; }; 9642B75129D2130500CB89C8 /* CombineDemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9642B75029D2130500CB89C8 /* CombineDemoApp.swift */; }; 9642B75329D2130500CB89C8 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9642B75229D2130500CB89C8 /* ContentView.swift */; }; 9642B75529D2130600CB89C8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9642B75429D2130600CB89C8 /* Assets.xcassets */; }; @@ -54,6 +55,7 @@ 9631D29629D70A6900A9D790 /* DefaultError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultError.swift; sourceTree = ""; }; 9631D29829D70D6A00A9D790 /* DefaultErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultErrorTests.swift; sourceTree = ""; }; 9631D29B29DA736200A9D790 /* NotificationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationTests.swift; sourceTree = ""; }; + 9631D29D29DB503900A9D790 /* URLSessionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTests.swift; sourceTree = ""; }; 9642B74D29D2130500CB89C8 /* CombineDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CombineDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9642B75029D2130500CB89C8 /* CombineDemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineDemoApp.swift; sourceTree = ""; }; 9642B75229D2130500CB89C8 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -111,6 +113,7 @@ isa = PBXGroup; children = ( 9631D29B29DA736200A9D790 /* NotificationTests.swift */, + 9631D29D29DB503900A9D790 /* URLSessionTests.swift */, ); path = Practice; sourceTree = ""; @@ -355,6 +358,7 @@ 9631D1EB29D56B8600A9D790 /* DeferredTests.swift in Sources */, 9631D29C29DA736200A9D790 /* NotificationTests.swift in Sources */, 9642B78329D28DEA00CB89C8 /* DispatchQueueType.swift in Sources */, + 9631D29E29DB503900A9D790 /* URLSessionTests.swift in Sources */, 9642B77E29D2149A00CB89C8 /* JustTests.swift in Sources */, 9631D28029D6242700A9D790 /* RecordTests.swift in Sources */, 9642B78029D2876200CB89C8 /* FutureTests.swift in Sources */, diff --git a/CombineDemo/CombineDemoTests/Publishers/Practice/URLSessionTests.swift b/CombineDemo/CombineDemoTests/Publishers/Practice/URLSessionTests.swift new file mode 100644 index 0000000..aada0f5 --- /dev/null +++ b/CombineDemo/CombineDemoTests/Publishers/Practice/URLSessionTests.swift @@ -0,0 +1,65 @@ +// +// URLSessionTests.swift +// CombineDemoTests +// +// Created by Manish Rathi on 03/04/2023. +// + +import Foundation +import Combine +import XCTest + +final class URLSessionTests: XCTestCase { + var urlSession: URLSession! + var publisher: URLSession.DataTaskPublisher! + var cancellables: Set! + var isFinishedCalled: Bool! + var receivedData: Data? + var receivedError: Error? + + override func setUp() { + super.setUp() + + let apiURL = URL(string: "https://api.github.com/users/crazymanish") + let urlRequest = URLRequest(url: apiURL!) + urlSession = URLSession.shared + publisher = URLSession.DataTaskPublisher(request: urlRequest, session: urlSession) + cancellables = [] + isFinishedCalled = false + } + + override func tearDown() { + urlSession = nil + publisher = nil + cancellables = nil + isFinishedCalled = nil + receivedData = nil + receivedError = nil + + super.tearDown() + } + + func testURLSessionPublisher() { + let expectation = XCTestExpectation(description: "Fetching GitHub user info") + + publisher.sink { [weak self] completion in + switch completion { + case .finished: + self?.isFinishedCalled = true + case .failure(let error): + self?.receivedError = error + } + } receiveValue: { [weak self] apiResponse in + self?.receivedData = apiResponse.data + + let jsonResponse = try? JSONSerialization.jsonObject(with: apiResponse.data, options: []) as? [String: Any] + XCTAssertEqual(jsonResponse?["login"] as? String, "crazymanish") + XCTAssertEqual(jsonResponse?["name"] as? String, "Manish Rathi") + + expectation.fulfill() + } + .store(in: &cancellables) + + wait(for: [expectation], timeout: 10.0) + } +} diff --git a/README.md b/README.md index 6083acc..4f0d1eb 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ - `DefaultError` https://github.com/crazymanish/what-matters-most/pull/66 - [x] Practices - `NotificationCenter` https://github.com/crazymanish/what-matters-most/pull/67 + - `URLSession` https://github.com/crazymanish/what-matters-most/pull/68 - [ ] Subscriber - [ ] Read about subscriber - [ ] Built-in subscribers