Add PromiseKit dependency
- Added PromiseKit dependency
This commit is contained in:
48
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Tests/TestNSURLSession.m
vendored
Normal file
48
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Tests/TestNSURLSession.m
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
@import PMKOMGHTTPURLRQ;
|
||||
@import PMKFoundation;
|
||||
@import OHHTTPStubs;
|
||||
@import PromiseKit;
|
||||
@import XCTest;
|
||||
|
||||
|
||||
@implementation NSURLSessionTests: XCTestCase
|
||||
|
||||
- (void)tearDown {
|
||||
[OHHTTPStubs removeAllStubs];
|
||||
}
|
||||
|
||||
- (void)test1 {
|
||||
id stubData = [NSData dataWithBytes:"[a: 3]" length:1];
|
||||
|
||||
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *rq){
|
||||
return [rq.URL.host isEqualToString:@"example.com"];
|
||||
} withStubResponse:^(NSURLRequest *request){
|
||||
return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:@{@"Content-Type": @"application/json"}];
|
||||
}];
|
||||
|
||||
id ex = [self expectationWithDescription:@""];
|
||||
|
||||
[[NSURLSession sharedSession] GET:[NSURL URLWithString:@"http://example.com"]].catch(^(NSError *err){
|
||||
XCTAssertEqualObjects(err.domain, NSCocoaErrorDomain); //TODO this is why we should replace this domain
|
||||
XCTAssertEqual(err.code, 3840);
|
||||
XCTAssertEqualObjects(err.userInfo[PMKURLErrorFailingDataKey], stubData);
|
||||
XCTAssertNotNil(err.userInfo[PMKURLErrorFailingURLResponseKey]);
|
||||
[ex fulfill];
|
||||
});
|
||||
|
||||
[self waitForExpectationsWithTimeout:1 handler:nil];
|
||||
}
|
||||
|
||||
- (void)test2 {
|
||||
id ex = [self expectationWithDescription:@""];
|
||||
|
||||
[[NSURLSession sharedSession] GET:nil].catch(^(NSError *err){
|
||||
XCTAssertEqualObjects(err.domain, NSURLErrorDomain);
|
||||
XCTAssertEqual(err.code, NSURLErrorUnsupportedURL);
|
||||
[ex fulfill];
|
||||
});
|
||||
|
||||
[self waitForExpectationsWithTimeout:1 handler:nil];
|
||||
}
|
||||
|
||||
@end
|
||||
74
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Tests/TestNSURLSession.swift
vendored
Normal file
74
Carthage/Checkouts/PromiseKit/Extensions/OMGHTTPURLRQ/Tests/TestNSURLSession.swift
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
import PMKOMGHTTPURLRQ
|
||||
import OHHTTPStubs
|
||||
import PromiseKit
|
||||
import XCTest
|
||||
|
||||
class NSURLSessionTests: XCTestCase {
|
||||
func test1() {
|
||||
let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]]
|
||||
|
||||
OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
|
||||
return OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil)
|
||||
}
|
||||
|
||||
let ex = expectation(description: "")
|
||||
URLSession.shared.GET("http://example.com").compactMap {
|
||||
try JSONSerialization.jsonObject(with: $0.data)
|
||||
}.done {
|
||||
XCTAssertEqual(json, $0 as? NSDictionary)
|
||||
ex.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 1)
|
||||
}
|
||||
|
||||
func test2() {
|
||||
|
||||
// test that Promise<Data> chains thens
|
||||
// this test because I don’t trust the Swift compiler
|
||||
|
||||
let dummy = ("fred" as NSString).data(using: String.Encoding.utf8.rawValue)!
|
||||
|
||||
OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
|
||||
return OHHTTPStubsResponse(data: dummy, statusCode: 200, headers: [:])
|
||||
}
|
||||
|
||||
let ex = expectation(description: "")
|
||||
|
||||
after(seconds: 0.1).then {
|
||||
URLSession.shared.GET("http://example.com")
|
||||
}.done {
|
||||
XCTAssertEqual($0.data, dummy)
|
||||
ex.fulfill()
|
||||
}
|
||||
|
||||
waitForExpectations(timeout: 1)
|
||||
}
|
||||
|
||||
func testSyntax() {
|
||||
let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]]
|
||||
|
||||
OHHTTPStubs.stubRequests(passingTest: {
|
||||
$0.url!.host == "example.com"
|
||||
}, withStubResponse: { _ in
|
||||
OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil)
|
||||
})
|
||||
|
||||
let p = URLSession.shared.GET("http://example.com", query: [
|
||||
"1": 1,
|
||||
"2": 2
|
||||
])
|
||||
|
||||
let ex = expectation(description: "")
|
||||
p.compactMap {
|
||||
try JSONSerialization.jsonObject(with: $0.data)
|
||||
}.done {
|
||||
XCTAssertEqual(json, $0 as? NSDictionary)
|
||||
ex.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 1)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
OHHTTPStubs.removeAllStubs()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user