Skip to content

Instantly share code, notes, and snippets.

@wanbok
Last active May 1, 2017 14:58
Show Gist options
  • Select an option

  • Save wanbok/a722f77555d976df6037 to your computer and use it in GitHub Desktop.

Select an option

Save wanbok/a722f77555d976df6037 to your computer and use it in GitHub Desktop.
//
// MockingServer.swift
// Frip
//
// Created by 최완복 on 2016. 1. 6..
// Copyright © 2016년 Wanbok. All rights reserved.
//
import UIKit
import AMYServer
import SwiftyJSON
class MockingServer: AMYServer {
override var baseURL: NSURL! { return NSURL(string: Router.baseURLString) }
func waitForServiceRequest(serviceRequest: String, withJSONDataMatchingBlock block: ((json: JSON, error: NSErrorPointer) -> KIFTestStepResult)) -> AMYRequest? {
return waitForRequestMatchingBlock { [weak self] (request, error) -> KIFTestStepResult in
self?.KIFTestCondition(
request.URL?.lastPathComponent == serviceRequest,
error: error,
errorMessage: "Could not find request for \(serviceRequest). Found request for \(self?.pendingURLRequests?.map { ($0 as? NSURLRequest)?.URL?.lastPathComponent })")
let json = JSON(request.HTTPBody ?? NSData())
return block(json: json, error: error)
}
}
}
extension AMYServer {
func KIFTestCondition(condition: Bool, error: NSErrorPointer?, errorMessage: String? = nil) -> KIFTestStepResult? {
if !condition { return nil }
error?.memory = NSError(domain: "KIFTest", code: Int(KIFTestStepResult.Wait.rawValue), userInfo: errorMessage != nil ? [NSLocalizedDescriptionKey: errorMessage ?? ""] : nil)
return .Failure
}
func KIFTestWaitCondition(condition: Bool, error: NSErrorPointer?, errorMessage: String? = nil) -> KIFTestStepResult? {
if !condition { return nil }
error?.memory = NSError(domain: "KIFTest", code: Int(KIFTestStepResult.Wait.rawValue), userInfo: errorMessage != nil ? [NSLocalizedDescriptionKey: errorMessage ?? ""] : nil)
return .Wait
}
func waitForStringValueAtKeyPath(value: AnyObject!, json: JSON, keyPath: String, error: NSErrorPointer) -> KIFTestStepResult? {
let condition = value as? String == json[keyPath].string
return KIFTestCondition(condition, error: error, errorMessage: "Waiting for \(keyPath)=\(value) in \(json)")
}
func waitForRequestMatching(mocktail mocktail: String, inBundle bundle: NSBundle?, andRespondWithValues values: [String: AnyObject]?) {
waitForRequestMatching(mocktail: mocktail, inBundle: bundle, withHTTPBodyMatchingBlock: nil, andRespondWithValues: values)
}
func waitForRequestMatching(mocktail mocktail: String, inBundle bundle: NSBundle? = NSBundle.KIFTestBundle(), withHTTPBodyMatchingBlock block: ((NSData, NSErrorPointer) -> KIFTestStepResult)?, andRespondWithValues values: [String: AnyObject]?) {
let response: _AMYMocktailResponse?
let headers: [NSObject: AnyObject]?
let body: NSData?
var errorType = "load mocktail"
do {
response = try _AMYMocktailResponse(fromTail: mocktail, bundle: bundle)
errorType = "generate headers"
headers = try response?.headersWithValues(values)
errorType = "generate body"
body = try response?.bodyWithValues(values)
let request = waitForRequestMatchingBlock { [weak self] (req, error) -> KIFTestStepResult in
return self?.KIFTestWaitCondition(response?.matchesURL(req.URL, method: req.HTTPMethod, patternLength: nil) ?? false, error: error, errorMessage: "Could not find request matching mocktail.") ??
{
block?(req.HTTPBody ?? NSData(), error)
return .Success
}()
}
request.respondWithStatusCode(response?.statusCode ?? 500, headerFields: headers)
request.sendData(body)
request.close()
} catch let error as NSError {
let errorToThrow = NSError(domain: "KIFTest", code: Int(KIFTestStepResult.Failure.rawValue), userInfo: [NSLocalizedDescriptionKey: "Failed to \(errorType): \(error.localizedDescription)", NSUnderlyingErrorKey: error])
failWithError(errorToThrow, stopTest: true)
}
}
}
extension XCTestCase {
func tester(file : String = __FILE__, _ line : Int = __LINE__) -> KIFUITestActor {
return KIFUITestActor(inFile: file, atLine: line, delegate: self)
}
func system(file : String = __FILE__, _ line : Int = __LINE__) -> KIFSystemTestActor {
return KIFSystemTestActor(inFile: file, atLine: line, delegate: self)
}
func server(file : String = __FILE__, _ line : Int = __LINE__) -> MockingServer {
return MockingServer(inFile: file, atLine: line, delegate: self)
}
}
extension KIFTestActor {
func tester(file : String = __FILE__, _ line : Int = __LINE__) -> KIFUITestActor {
return KIFUITestActor(inFile: file, atLine: line, delegate: self)
}
func system(file : String = __FILE__, _ line : Int = __LINE__) -> KIFSystemTestActor {
return KIFSystemTestActor(inFile: file, atLine: line, delegate: self)
}
func server(file : String = __FILE__, _ line : Int = __LINE__) -> MockingServer {
return MockingServer(inFile: file, atLine: line, delegate: self)
}
}
@wanbok
Copy link
Copy Markdown
Author

wanbok commented Jan 6, 2016

AMYServer for Swift

@wanbok
Copy link
Copy Markdown
Author

wanbok commented Jan 6, 2016

inspired by ExampleServer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment