How do I make fun of Alamofire's answer?

I have Swift 2 code using Alamofire that requests a remote url that it expects to return JSON. I have done enough interactive testing to be sure that it behaves correctly enough. Now I want to test it so that I can rely on this code and then move on to other things.

I am new to Swift, although I have been programming OO Perl for decades, so maybe something that I missed.

This is how I set the verification code so far (non-essential things such as how I delete the username and password to avoid yak shaving). The calling code indicates the username and password and expects one of the two callbacks to be called whenever a result is received. (I have not reached the point of errors, so please do not pay attention to this topic.)

struct checkUsernameHandler { var onSuccess: () -> () var onFailure: () -> () } struct checkUsernameOutput { var authenticated: Bool? } func checkUsername(user: User, handler: checkUsernameHandler) { Alamofire.request(.PUT, NSURL(string: baseURL + "/user/verify?key=" + apiKey)!, parameters: [ "username": user.username!, "password": user.password!, ], encoding: .JSON ).responseJSON { response in let checkusernameOutput = self.analyseCheckUsernameResponse(response) if let authenticated = checkusernameOutput.authenticated { if authenticated { handler.onSuccess() } else { handler.onFailure() } } } } func analyseCheckUsernameResponse (response: Response<AnyObject, NSError>) -> checkUsernameOutput { var output = checkUsernameOutput() if (response.result.isSuccess) { if let JSON = response.result.value as? NSDictionary { // ... } } else { print("Didn't get proper JSON or something: \(response.result.error)") } return output } 

What I'm looking for is a way to take raw JSON and somehow pass it to Alamofire so that I can create a response object that has the appropriate stuff, including errors if it doesn't receive anything (timeout?) Or The data returned was invalid (for example, HTML 404 or 500 pages).

At the moment, I'm quite happy with the checkUsername function, so I'm not interested in someone saying "use OHHTTPStubs". I am looking at testing only the code that I doubt it is the code I just wrote.

EDIT . Fearing something else, it seems that the inside of Alamofire is sufficiently connected with the fact that in order to create a fake answer I need to do almost all the work related to setting up a network connection one way or another. Therefore, perhaps the answer: "Is it too much trouble to do this"?

+6
source share
1 answer

I had success by overriding the startPoad method for URLProtocol. You can find an example of this in the Alamofire test directory under URLProtocolTests.swift.

Intercepting this call, you can capture the incoming request, then parse the URL to match your chosen response layout. The following is an example of how I handle the response:

 override func startLoading() { //Seperate module I used to handle the request let data:NSData = mocks.find(request) as! NSData let client = self.client let response = HTTPURLResponse(url: (request.url)!, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: cannedHeaders) client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: URLCache.StoragePolicy.notAllowed) cannedResponse = data client?.urlProtocol(self, didLoad: cannedResponse as! Data) client?.urlProtocolDidFinishLoading(self) activeTask?.resume() } 

I posted a complete example of how to do this on github: https://github.com/ksteigerwald/MockAlamofire

+4
source

All Articles