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

Enable open Package.swift without Error #65

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 9 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
37 changes: 13 additions & 24 deletions Sources/OpenGraph/Extension/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
64 changes: 33 additions & 31 deletions Tests/OpenGraphTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import XCTest
import OHHTTPStubs
import OHHTTPStubsSwift
@testable import OpenGraph

class OpenGraphTests: XCTestCase {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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)
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.