Skip to content

Commit 01178a1

Browse files
authored
Merge pull request #209 from ishkawa/swiftpm
Support Swift Package Manager
2 parents b5b70b9 + 3ccfea4 commit 01178a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+297
-274
lines changed

.gitmodules

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
[submodule "Carthage/Checkouts/Result"]
22
path = Carthage/Checkouts/Result
33
url = https://github.com/antitypical/Result.git
4-
[submodule "Carthage/Checkouts/OHHTTPStubs"]
5-
path = Carthage/Checkouts/OHHTTPStubs
6-
url = https://github.com/AliSoftware/OHHTTPStubs.git

.travis.yml

+13-15
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ matrix:
77
- os: osx
88
language: objective-c
99
osx_image: xcode8
10-
env: JOB=Xcode8
11-
12-
install:
13-
- carthage bootstrap --use-submodules --no-build
14-
- pod repo update --silent
15-
16-
script:
17-
- if [[ "$JOB" == "Xcode7.3" ]]; then pod lib lint; fi
18-
- set -o pipefail
19-
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -configuration Release ENABLE_TESTABILITY=YES | xcpretty -c
20-
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -configuration Release -sdk iphonesimulator -destination "name=iPhone 6s" ENABLE_TESTABILITY=YES | xcpretty -c
21-
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -configuration Release -sdk appletvsimulator -destination "name=Apple TV 1080p" ENABLE_TESTABILITY=YES | xcpretty -c
22-
23-
after_success:
24-
- bash <(curl -s https://codecov.io/bash)
10+
script:
11+
- set -o pipefail
12+
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -configuration Release ENABLE_TESTABILITY=YES | xcpretty -c
13+
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -configuration Release -sdk iphonesimulator -destination "name=iPhone 6s" ENABLE_TESTABILITY=YES | xcpretty -c
14+
- xcodebuild build-for-testing test-without-building -workspace APIKit.xcworkspace -scheme APIKit -configuration Release -sdk appletvsimulator -destination "name=Apple TV 1080p" ENABLE_TESTABILITY=YES | xcpretty -c
15+
after_success:
16+
- bash <(curl -s https://codecov.io/bash)
17+
- os: osx
18+
language: generic
19+
osx_image: xcode8
20+
script:
21+
- swift build
22+
- swift test
2523

2624
notifications:
2725
email: false

APIKit.xcodeproj/project.pbxproj

+203-203
Large diffs are not rendered by default.

APIKit.xcworkspace/contents.xcworkspacedata

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cartfile.private

-1
This file was deleted.

Cartfile.resolved

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
github "AliSoftware/OHHTTPStubs" "5.2.1-swift3"
21
github "antitypical/Result" "3.0.0"

Carthage/Checkouts/OHHTTPStubs

-1
This file was deleted.

Configurations/APIKit.xcconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DYLIB_COMPATIBILITY_VERSION = 1
44
DYLIB_CURRENT_VERSION = 1
55
DYLIB_INSTALL_NAME_BASE = @rpath
66
FRAMEWORK_VERSION = A
7-
INFOPLIST_FILE = Sources/Info.plist
7+
INFOPLIST_FILE = Sources/APIKit/Info.plist
88
PRODUCT_BUNDLE_IDENTIFIER = org.ishkawa.$(PRODUCT_NAME:rfc1034identifier)
99
PRODUCT_NAME = $(PROJECT_NAME)
1010
SKIP_INSTALL = YES

Package.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PackageDescription
33
let package = Package(
44
name: "APIKit",
55
dependencies: [
6-
.Package(url: "https://github.com/antitypical/Result.git", majorVersion: 1),
7-
]
6+
.Package(url: "https://github.com/antitypical/Result.git", majorVersion: 3),
7+
],
8+
exclude: ["Sources/APIKit/BodyParameters/AbstractInputStream.m"]
89
)
File renamed without changes.

Sources/BodyParameters/MultipartFormDataBodyParameters.swift Sources/APIKit/BodyParameters/MultipartFormDataBodyParameters.swift

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import Foundation
66
import CoreServices
77
#endif
88

9+
#if SWIFT_PACKAGE
10+
class AbstractInputStream: InputStream {
11+
init() {
12+
super.init(data: Data())
13+
}
14+
}
15+
#endif
16+
917
/// `FormURLEncodedBodyParameters` serializes array of `Part` for HTTP body and states its content type is multipart/form-data.
1018
public struct MultipartFormDataBodyParameters: BodyParameters {
1119
/// `EntityType` represents wheather the entity is expressed as `Data` or `InputStream`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Tests/APIKit/BodyParametersType/MultipartFormDataParametersTests.swift Tests/APIKitTests/BodyParametersType/MultipartFormDataParametersTests.swift

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class MultipartFormDataParametersTests: XCTestCase {
7070
}
7171

7272
// MARK: Values
73+
74+
// Skip test cases that uses files until SwiftPM supports resources.
75+
#if !SWIFT_PACKAGE
7376
func testFileValue() {
7477
let fileURL = Bundle(for: type(of: self)).url(forResource: "test", withExtension: "json")!
7578
let part = try! MultipartFormDataBodyParameters.Part(fileURL: fileURL, name: "test")
@@ -100,6 +103,7 @@ class MultipartFormDataParametersTests: XCTestCase {
100103
XCTFail()
101104
}
102105
}
106+
#endif
103107

104108
func testStringValue() {
105109
let part = try! MultipartFormDataBodyParameters.Part(value: "abcdef", name: "foo")
File renamed without changes.
File renamed without changes.

Tests/APIKit/SessionAdapterType/URLSessionAdapterSubclassTests.swift Tests/APIKitTests/SessionAdapterType/URLSessionAdapterSubclassTests.swift

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Foundation
22
import XCTest
3-
import OHHTTPStubs
43
import APIKit
54

65
class URLSessionAdapterSubclassTests: XCTestCase {
@@ -25,24 +24,16 @@ class URLSessionAdapterSubclassTests: XCTestCase {
2524
super.setUp()
2625

2726
let configuration = URLSessionConfiguration.default
27+
configuration.protocolClasses = [HTTPStub.self]
28+
2829
adapter = SessionAdapter(configuration: configuration)
2930
session = Session(adapter: adapter)
3031
}
3132

32-
override func tearDown() {
33-
OHHTTPStubs.removeAllStubs()
34-
super.tearDown()
35-
}
36-
3733
func testDelegateMethodCall() {
38-
let data = try! JSONSerialization.data(withJSONObject: [:], options: [])
39-
40-
OHHTTPStubs.stubRequests(passingTest: { request in
41-
return true
42-
}, withStubResponse: { request in
43-
return OHHTTPStubsResponse(data: data, statusCode: 200, headers: nil)
44-
})
45-
34+
let data = "{}".data(using: .utf8)!
35+
HTTPStub.stubResult = .success(data)
36+
4637
let expectation = self.expectation(description: "wait for response")
4738
let request = TestRequest()
4839

Tests/APIKit/SessionAdapterType/URLSessionAdapterTests.swift Tests/APIKitTests/SessionAdapterType/URLSessionAdapterTests.swift

+9-28
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
import Foundation
22
import APIKit
33
import XCTest
4-
import OHHTTPStubs
54

65
class URLSessionAdapterTests: XCTestCase {
76
var session: Session!
87

98
override func setUp() {
109
super.setUp()
11-
10+
1211
let configuration = URLSessionConfiguration.default
12+
configuration.protocolClasses = [HTTPStub.self]
13+
1314
let adapter = URLSessionAdapter(configuration: configuration)
1415
session = Session(adapter: adapter)
1516
}
1617

17-
override func tearDown() {
18-
OHHTTPStubs.removeAllStubs()
19-
super.tearDown()
20-
}
21-
2218
// MARK: - integration tests
2319
func testSuccess() {
2420
let dictionary = ["key": "value"]
2521
let data = try! JSONSerialization.data(withJSONObject: dictionary, options: [])
26-
27-
OHHTTPStubs.stubRequests(passingTest: { request in
28-
return true
29-
}, withStubResponse: { request in
30-
return OHHTTPStubsResponse(data: data, statusCode: 200, headers: nil)
31-
})
22+
HTTPStub.stubResult = .success(data)
3223

3324
let expectation = self.expectation(description: "wait for response")
3425
let request = TestRequest()
35-
26+
3627
session.send(request) { response in
3728
switch response {
3829
case .success(let dictionary):
@@ -50,12 +41,7 @@ class URLSessionAdapterTests: XCTestCase {
5041

5142
func testConnectionError() {
5243
let error = NSError(domain: NSURLErrorDomain, code: NSURLErrorTimedOut, userInfo: nil)
53-
54-
OHHTTPStubs.stubRequests(passingTest: { request in
55-
return true
56-
}, withStubResponse: { request in
57-
return OHHTTPStubsResponse(error: error)
58-
})
44+
HTTPStub.stubResult = .failure(error)
5945

6046
let expectation = self.expectation(description: "wait for response")
6147
let request = TestRequest()
@@ -82,14 +68,9 @@ class URLSessionAdapterTests: XCTestCase {
8268
}
8369

8470
func testCancel() {
85-
let data = try! JSONSerialization.data(withJSONObject: [:], options: [])
86-
87-
OHHTTPStubs.stubRequests(passingTest: { request in
88-
return true
89-
}, withStubResponse: { request in
90-
return OHHTTPStubsResponse(data: data, statusCode: 200, headers: nil).responseTime(1.0)
91-
})
92-
71+
let data = "{}".data(using: .utf8)!
72+
HTTPStub.stubResult = .success(data)
73+
9374
let expectation = self.expectation(description: "wait for response")
9475
let request = TestRequest()
9576

Tests/APIKit/SessionCallbackQueueTests.swift Tests/APIKitTests/SessionCallbackQueueTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22
import APIKit
33
import XCTest
4-
import OHHTTPStubs
54

65
class SessionCallbackQueueTests: XCTestCase {
76
var adapter: TestSessionAdapter!

Tests/APIKit/SessionTests.swift Tests/APIKitTests/SessionTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22
import APIKit
33
import XCTest
4-
import OHHTTPStubs
54
import Result
65

76
class SessionTests: XCTestCase {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Foundation
2+
import Dispatch
3+
import Result
4+
5+
class HTTPStub: URLProtocol {
6+
static var stubResult: Result<Data, NSError> = .success(Data())
7+
8+
private var isCancelled = false
9+
10+
// MARK: URLProtocol -
11+
12+
override class func canInit(with: URLRequest) -> Bool {
13+
return true
14+
}
15+
16+
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
17+
return request
18+
}
19+
20+
override func startLoading() {
21+
let response = HTTPURLResponse(
22+
url: request.url!,
23+
statusCode: 200,
24+
httpVersion: "1.1",
25+
headerFields: nil)
26+
27+
let queue = DispatchQueue.global(qos: .default)
28+
29+
queue.asyncAfter(deadline: .now() + 0.01) {
30+
guard !self.isCancelled else {
31+
return
32+
}
33+
34+
self.client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed)
35+
36+
switch HTTPStub.stubResult {
37+
case .success(let data):
38+
self.client?.urlProtocol(self, didLoad: data)
39+
40+
case .failure(let error):
41+
self.client?.urlProtocol(self, didFailWithError: error)
42+
}
43+
44+
self.client?.urlProtocolDidFinishLoading(self)
45+
}
46+
}
47+
48+
override func stopLoading() {
49+
isCancelled = true
50+
}
51+
}
File renamed without changes.

0 commit comments

Comments
 (0)