TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | Apache 2 |
ReleasedLast Release | Jan 2015 |
SPMSupports SPM | ✗ |
Maintained by corinne krych.
A small library inspired by OHHTTPStubs to stub your network requests written in Swift.
This module is beta software, it currently supports Xcode 6.1.1
// 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)
// set up http stub from a stubbed file located in the test bundle
StubsManager.stubRequestsPassingTest({ (request: NSURLRequest!) -> Bool in
return true
}, withStubResponse:( { (request: NSURLRequest!) -> StubResponse in
return StubResponse(filename: "mystubbedjson.json", location:.Bundle(NSBundle(forClass: AeroGearHttpStubTests.self)), statusCode: 200, headers: ["Content-Type" : "text/json"])
}))
// or
// set up http stub from a stubbed file located in the 'Documents' directory
StubsManager.stubRequestsPassingTest({ (request: NSURLRequest!) -> Bool in
return true
}, withStubResponse:( { (request: NSURLRequest!) -> StubResponse in
return StubResponse(filename: "mystubbedjson.json", location:.Documents, statusCode: 200, headers: ["Content-Type" : "text/json"])
}))
To add the library in your project, you can either use Cocoapods or manual install in your project. See the respective sections below for instructions:
Follow these steps to add the library in your Swift project:
git submodule add https://github.com/aerogear/aerogear-ios-httpstub.git
aerogear-ios-httpstub
folder, and drag the AeroGearHttpStub.xcodeproj
into the file navigator in Xcode.AeroGearHttpStub.framework
.+
button at the top left of the panel and select “New Copy Files Phase”. Rename this new phase to “Copy Frameworks”, set the “Destination” to “Frameworks”, and add AeroGearHttpStub.framework
.If you run into any problems, please file an issue and/or ask our user mailing list. You can also join our dev mailing list.