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..bf455e8 100644 --- a/Package.swift +++ b/Package.swift @@ -10,17 +10,23 @@ 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", + resources: [.process("Resources")] ), ], swiftLanguageVersions: [.v5] 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() + } + } } diff --git a/Tests/OpenGraphTests.swift b/Tests/OpenGraphTests.swift index edc06b4..955f864 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,19 @@ 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 - let path = Bundle(for: type(of: self)).path(forResource: htmlFileName, ofType: "html") - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) + HTTPStubs.stubRequests { request in + true + } withStubResponse: { request in +#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) } } @@ -107,12 +112,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? @@ -140,29 +145,26 @@ 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) + func testParseError() async { + 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? - 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) } } 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