From c3cd3d9d3c6d8cdeb68ff3903969b8d2bd0decfe Mon Sep 17 00:00:00 2001 From: zunda Date: Tue, 20 Sep 2022 19:10:32 +0900 Subject: [PATCH 1/7] Add Dependency OHHTTPStubsSwift --- Package.resolved | 16 ++++++++++++++++ Package.swift | 11 ++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 Package.resolved diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..3e3af44 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "OHHTTPStubs", + "repositoryURL": "https://github.com/AliSoftware/OHHTTPStubs", + "state": { + "branch": null, + "revision": "12f19662426d0434d6c330c6974d53e2eb10ecd9", + "version": "9.1.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index 9e57fff..2865c29 100644 --- a/Package.swift +++ b/Package.swift @@ -10,17 +10,22 @@ let package = Package( products: [ .library(name: "OpenGraph", targets: ["OpenGraph"]), ], - dependencies: [], + dependencies: [ + .package(url: "https://github.com/AliSoftware/OHHTTPStubs", .upToNextMajor(from: "9.0.0")), + ], targets: [ .target( name: "OpenGraph", - dependencies: [], + dependencies: [ + .product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs") + ], path: "Sources/OpenGraph", exclude: ["Info.plist"] ), .testTarget( name: "OpenGraphTests", - dependencies: ["OpenGraph"] + dependencies: ["OpenGraph"], + path: "Tests" ), ], swiftLanguageVersions: [.v5] From 5fda2ad775167a05f653ed8c205b6e53c151c64e Mon Sep 17 00:00:00 2001 From: zunda Date: Tue, 20 Sep 2022 19:12:08 +0900 Subject: [PATCH 2/7] Adjust Dependency change's --- Tests/OpenGraphTests.swift | 39 ++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Tests/OpenGraphTests.swift b/Tests/OpenGraphTests.swift index edc06b4..06f1d18 100644 --- a/Tests/OpenGraphTests.swift +++ b/Tests/OpenGraphTests.swift @@ -1,5 +1,6 @@ import XCTest import OHHTTPStubs +import OHHTTPStubsSwift @testable import OpenGraph class OpenGraphTests: XCTestCase { @@ -12,15 +13,15 @@ class OpenGraphTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() - OHHTTPStubs.removeAllStubs() + HTTPStubs.removeAllStubs() } func setupStub(htmlFileName: String) { - OHHTTPStubs.stubRequests(passingTest: { request -> Bool in - return true - }) { request -> OHHTTPStubsResponse in + HTTPStubs.stubRequests { request in + true + } withStubResponse: { request in let path = Bundle(for: type(of: self)).path(forResource: htmlFileName, ofType: "html") - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) + return .init(fileAtPath: path!, statusCode: 200, headers: nil) } } @@ -107,12 +108,12 @@ class OpenGraphTests: XCTestCase { func testHTTPResponseError() { let responseArrived = expectation(description: "response of async request has arrived") - OHHTTPStubs.stubRequests(passingTest: { request -> Bool in - return true - }) { request -> OHHTTPStubsResponse in - OHHTTPStubsResponse(jsonObject: [:], statusCode: 404, headers: nil) + HTTPStubs.stubRequests { request in + true + } withStubResponse: { request in + return .init(jsonObject: [:], statusCode: 404, headers: nil) } - + let url = URL(string: "https://www.example.com")! var og: OpenGraph? var error: Error? @@ -142,13 +143,19 @@ class OpenGraphTests: XCTestCase { func testParseError() { let responseArrived = expectation(description: "response of async request has arrived") - - OHHTTPStubs.stubRequests(passingTest: { request -> Bool in - return true - }) { request -> OHHTTPStubsResponse in - OHHTTPStubsResponse(data: "あ".data(using: String.Encoding.shiftJIS)!, statusCode: 200, headers: nil) + + OHHTTPStubsSwift.stub { request in + return true + } response: { request in + HTTPStubsResponse() } - + + HTTPStubs.stubRequests { request in + true + } withStubResponse: { request in + .init(data: "あ".data(using: String.Encoding.shiftJIS)!, statusCode: 200, headers: nil) + } + let url = URL(string: "https://www.example.com")! var og: OpenGraph? var error: Error? From a5d3f890d47563e17dbf3634859a009dac16bbb1 Mon Sep 17 00:00:00 2001 From: zunda Date: Wed, 19 Oct 2022 18:37:26 +0900 Subject: [PATCH 3/7] Fix withTaskCancellationHandler Deprecate withTaskCancellationHandler -> withCheckedThrowingContinuation --- Sources/OpenGraph/Extension/URLSession.swift | 37 +++++++------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/Sources/OpenGraph/Extension/URLSession.swift b/Sources/OpenGraph/Extension/URLSession.swift index 0b1f52c..9b59152 100644 --- a/Sources/OpenGraph/Extension/URLSession.swift +++ b/Sources/OpenGraph/Extension/URLSession.swift @@ -29,28 +29,17 @@ public extension URLSession { /// - returns: A tuple containing the binary `Data` that was downloaded, /// as well as a `URLResponse` representing the server's response. /// - throws: Any error encountered while performing the data task. - func data(for request: URLRequest) async throws -> (Data, URLResponse) { - var dataTask: URLSessionDataTask? - let onCancel = { dataTask?.cancel() } - - return try await withTaskCancellationHandler( - handler: { - onCancel() - }, - operation: { - try await withCheckedThrowingContinuation { continuation in - dataTask = self.dataTask(with: request) { data, response, error in - guard let data = data, let response = response else { - let error = error ?? URLError(.badServerResponse) - return continuation.resume(throwing: error) - } - - continuation.resume(returning: (data, response)) - } - - dataTask?.resume() - } - } - ) - } + func data(for request: URLRequest) async throws -> (Data, URLResponse) { + try await withCheckedThrowingContinuation { continuation in + self.dataTask(with: request) { data, response, error in + if let error = error { + return continuation.resume(throwing: error) + } + guard let data = data, let response = response else { + return continuation.resume(throwing: URLError(.badServerResponse)) + } + continuation.resume(returning: (data, response)) + }.resume() + } + } } From a82f595ef87606a1d681c7487b762d57d2eb8f67 Mon Sep 17 00:00:00 2001 From: zunda Date: Wed, 19 Oct 2022 19:11:20 +0900 Subject: [PATCH 4/7] =?UTF-8?q?Resources=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/{ => Resources}/empty_ogp.html | 0 Tests/{ => Resources}/example3.com.html | 0 Tests/{ => Resources}/ogp.html | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename Tests/{ => Resources}/empty_ogp.html (100%) rename Tests/{ => Resources}/example3.com.html (100%) rename Tests/{ => Resources}/ogp.html (100%) diff --git a/Tests/empty_ogp.html b/Tests/Resources/empty_ogp.html similarity index 100% rename from Tests/empty_ogp.html rename to Tests/Resources/empty_ogp.html diff --git a/Tests/example3.com.html b/Tests/Resources/example3.com.html similarity index 100% rename from Tests/example3.com.html rename to Tests/Resources/example3.com.html diff --git a/Tests/ogp.html b/Tests/Resources/ogp.html similarity index 100% rename from Tests/ogp.html rename to Tests/Resources/ogp.html From 9f949c16c749a9d14676a981dd503e21f2560acc Mon Sep 17 00:00:00 2001 From: zunda Date: Wed, 19 Oct 2022 19:13:18 +0900 Subject: [PATCH 5/7] =?UTF-8?q?Bundle=E3=82=92SwiftPM=E3=81=A7=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/OpenGraphTests.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Tests/OpenGraphTests.swift b/Tests/OpenGraphTests.swift index 06f1d18..1bc8924 100644 --- a/Tests/OpenGraphTests.swift +++ b/Tests/OpenGraphTests.swift @@ -20,7 +20,11 @@ class OpenGraphTests: XCTestCase { HTTPStubs.stubRequests { request in true } withStubResponse: { request in - let path = Bundle(for: type(of: self)).path(forResource: htmlFileName, ofType: "html") +#if SWIFT_PACKAGE + let path = Bundle.module.path(forResource: htmlFileName, ofType: "html") +#else + let path = Bundle(for: type(of: self)).path(forResource: htmlFileName, ofType: "html") +#endif return .init(fileAtPath: path!, statusCode: 200, headers: nil) } } From f1b69197387b273250d1c3eb887d9737232aa8ce Mon Sep 17 00:00:00 2001 From: zunda Date: Wed, 19 Oct 2022 19:13:51 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88async=E3=81=AB?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/OpenGraphTests.swift | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/Tests/OpenGraphTests.swift b/Tests/OpenGraphTests.swift index 1bc8924..955f864 100644 --- a/Tests/OpenGraphTests.swift +++ b/Tests/OpenGraphTests.swift @@ -145,9 +145,7 @@ class OpenGraphTests: XCTestCase { } } - func testParseError() { - let responseArrived = expectation(description: "response of async request has arrived") - + func testParseError() async { OHHTTPStubsSwift.stub { request in return true } response: { request in @@ -161,19 +159,12 @@ class OpenGraphTests: XCTestCase { } let url = URL(string: "https://www.example.com")! - var og: OpenGraph? - var error: Error? - OpenGraph.fetch(url: url) { result in - switch result { - case .success(let _og): og = _og - case .failure(let _error): error = _error - } - responseArrived.fulfill() + + do { + _ = try await OpenGraph.fetch(url: url) } - - waitForExpectations(timeout: 10) { _ in - XCTAssert(og == nil) - XCTAssert(error! is OpenGraphParseError) + catch let error { + XCTAssert(error is OpenGraphParseError) } } From 4015ec5d88865f180c672179e450e2d13da476c0 Mon Sep 17 00:00:00 2001 From: zunda Date: Wed, 19 Oct 2022 19:14:02 +0900 Subject: [PATCH 7/7] =?UTF-8?q?Resouces=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Package.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 2865c29..bf455e8 100644 --- a/Package.swift +++ b/Package.swift @@ -25,7 +25,8 @@ let package = Package( .testTarget( name: "OpenGraphTests", dependencies: ["OpenGraph"], - path: "Tests" + path: "Tests", + resources: [.process("Resources")] ), ], swiftLanguageVersions: [.v5]