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

Make SwiftHTTP be consumable as a Swift package #307

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Temporary Items
### Swift ###
# Xcode
#
.build
build/
*.pbxuser
!default.pbxuser
Expand Down
22 changes: 21 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

//
// Package.Swift
// SwiftHTTP
Expand All @@ -21,5 +24,22 @@
import PackageDescription

let package = Package(
name: "SwiftHTTP"
name: "SwiftHTTP",
products: [
.library(
name: "SwiftHTTP",
targets: ["SwiftHTTP"]),
],
targets: [
.target(
name: "SwiftHTTP",
dependencies: [],
path: "Source",
exclude: ["Info.plist"]),
.testTarget(
name: "SwiftHTTPTests",
dependencies: ["SwiftHTTP"],
path: "Tests",
exclude: ["Info.plist"]),
]
)
55 changes: 7 additions & 48 deletions Tests/SwiftHTTPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class SwiftHTTPTests: XCTestCase {
super.tearDown()
}

func testGetRequest() {
func testGetRequest() throws {
let expectation = self.expectation(description: "testGetRequest")

do {
let opt = try HTTP.GET("https://google.com", parameters: nil)
opt.start { response in
let opt = try XCTUnwrap(HTTP.GET("https://google.com", parameters: nil))
opt.run { response in
if response.error != nil {
XCTAssert(false, "Failure")
}
Expand All @@ -39,12 +39,12 @@ class SwiftHTTPTests: XCTestCase {
waitForExpectations(timeout: 30, handler: nil)
}

func testGetProgress() {
func testGetProgress() throws {
let expectation1 = expectation(description: "testGetProgressFinished")
let expectation2 = expectation(description: "testGetProgressIncremented")

do {
let opt = try HTTP.GET("http://photojournal.jpl.nasa.gov/tiff/PIA19330.tif", parameters: nil)
let opt = try XCTUnwrap(HTTP.GET("http://photojournal.jpl.nasa.gov/tiff/PIA19330.tif", parameters: nil))
var alreadyCheckedProgressIncremented: Bool = false
opt.progress = { progress in
if progress > 0 && !alreadyCheckedProgressIncremented {
Expand All @@ -53,7 +53,7 @@ class SwiftHTTPTests: XCTestCase {
expectation2.fulfill()
}
}
opt.start { response in
opt.run { response in
if response.error != nil {
XCTAssert(false, "Failure")
}
Expand All @@ -66,46 +66,5 @@ class SwiftHTTPTests: XCTestCase {

waitForExpectations(timeout: 30, handler: nil)
}

func testOperationDependencies() {
let expectation1 = expectation(description: "testOperationDependencies1")
let expectation2 = expectation(description: "testOperationDependencies2")

var operation1Finished = false

let urlString1 = "http://photojournal.jpl.nasa.gov/tiff/PIA19330.tif" // (4.32 MB)
let urlString2 = "http://photojournal.jpl.nasa.gov/jpeg/PIA19330.jpg" // (0.14 MB)

let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 2

do {
let opt1 = try HTTP.GET(urlString1, parameters: nil)
opt1.onFinish = { response in
if let err = response.error {
XCTFail("request1 failed: \(err.localizedDescription)")
}
operation1Finished = true
expectation1.fulfill()
}

let opt2 = try HTTP.GET(urlString2, parameters: nil)
opt2.onFinish = { response in
if let err = response.error {
XCTFail("request2 failed: \(err.localizedDescription)")
}
XCTAssert(operation1Finished, "Operation 1 did not finish first")
expectation2.fulfill()
}

opt2.addDependency(opt1)
operationQueue.addOperation(opt1)
operationQueue.addOperation(opt2)

} catch {
XCTAssert(false, "Failure")
}

waitForExpectations(timeout: 30, handler: nil)
}

}