aerogear-ios-httpstub 项目简介
aerogear-ios-httpstub 是使用 Swift 编写的,很小的网络请求处理库。灵感来源于 OHHTTPStubs。使用示例:// set up http stub
StubsManager.stubRequestsPassingTest({ (request: NSURLRequest!) -> Bool in
return true
}, withStubResponse:( { (request: NSURLRequest!) -> StubResponse in
return StubResponse(data:NSData.data(), statusCode: 200, headers: ["Content-Type" : "text/json"])
}))
// async test expectation
let registrationExpectation = expectationWithDescription("testStubWithNSURLSessionDefaultConfiguration");
let request = NSMutableURLRequest(URL: NSURL(string: "http://server.com"))
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request) {(data, response, error) in
XCTAssertNil(error, "unexpected error")
XCTAssertNotNil(data, "response should contain data")
registrationExpectation.fulfill()
}
task.resume()
waitForExpectationsWithTimeout(10, handler: nil)
StubsManager.stubRequestsPassingTest({ (request: NSURLRequest!) -> Bool in
return true
}, withStubResponse:( { (request: NSURLRequest!) -> StubResponse in
return StubResponse(data:NSData.data(), statusCode: 200, headers: ["Content-Type" : "text/json"])
}))
// async test expectation
let registrationExpectation = expectationWithDescription("testStubWithNSURLSessionDefaultConfiguration");
let request = NSMutableURLRequest(URL: NSURL(string: "http://server.com"))
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request) {(data, response, error) in
XCTAssertNil(error, "unexpected error")
XCTAssertNotNil(data, "response should contain data")
registrationExpectation.fulfill()
}
task.resume()
waitForExpectationsWithTimeout(10, handler: nil)