diff --git a/AMYServer Tests/AMYServerTests.m b/AMYServer Tests/AMYServerTests.m index dd6f736..ca9118e 100644 --- a/AMYServer Tests/AMYServerTests.m +++ b/AMYServer Tests/AMYServerTests.m @@ -46,29 +46,62 @@ - (void)testSuccess [tester tapViewWithAccessibilityLabel:@"Close"]; } + +- (void)testThatRequestsCloseWithAnErrorStatus +{ + [exampleServer closeAllRequests]; + [tester waitForViewWithAccessibilityLabel:@"500 status code"]; + [tester tapViewWithAccessibilityLabel:@"Close"]; +} + +- (void)testThatClosingAllRequestsClosesAllRequests +{ + [exampleServer closeAllRequests]; + KIFExpectFailure([[exampleServer usingTimeout:0.5] waitForLoginWithUsername:@"brian" password:@"$ecret" andRespondWithSuccess:YES message:@"Welcome, Brian" token:@"12345"]); + [tester tapViewWithAccessibilityLabel:@"Close"]; +} + +- (void)testThatStoppingTheServerClosesAllRequests +{ + [exampleServer stop]; + [exampleServer start]; + KIFExpectFailure([[exampleServer usingTimeout:0.5] waitForLoginWithUsername:@"brian" password:@"$ecret" andRespondWithSuccess:YES message:@"Welcome, Brian" token:@"12345"]); + [tester tapViewWithAccessibilityLabel:@"Close"]; +} + - (void)testMissingMocktail { KIFExpectFailure([exampleServer waitForRequestMatchingMocktail:@"logon" andRespondWithValues:@{}]); + [exampleServer closeAllRequests]; + [tester tapViewWithAccessibilityLabel:@"Close"]; } - (void)testMocktailWithBadMustache { KIFExpectFailure([exampleServer waitForRequestMatchingMocktail:@"bad-mustache" andRespondWithValues:@{@"message": @"hello"}]); + [exampleServer closeAllRequests]; + [tester tapViewWithAccessibilityLabel:@"Close"]; } - (void)testMocktailWithNotEnoughLines { KIFExpectFailure([exampleServer waitForRequestMatchingMocktail:@"not-enough-lines" andRespondWithValues:@{@"message": @"hello"}]); + [exampleServer closeAllRequests]; + [tester tapViewWithAccessibilityLabel:@"Close"]; } - (void)testMocktailWithBadJSON { KIFExpectFailure([exampleServer waitForRequestMatchingMocktail:@"bad-json" andRespondWithValues:@{@"message": @"hello"}]); + [exampleServer closeAllRequests]; + [tester tapViewWithAccessibilityLabel:@"Close"]; } - (void)testMocktailWithInvalidHeader { KIFExpectFailure([exampleServer waitForRequestMatchingMocktail:@"invalid-header" andRespondWithValues:@{@"message": @"hello"}]); + [exampleServer closeAllRequests]; + [tester tapViewWithAccessibilityLabel:@"Close"]; } - (void)testBasicMocktail @@ -107,7 +140,8 @@ - (void)testMocktailWithFailingBodyConditions KIFTestWaitCondition([json[@"username"] isEqualToString:@"joe"], error, @"Could not find username"); return KIFTestStepResultSuccess; } andRespondWithValues:@{}]); + [exampleServer closeAllRequests]; + [tester tapViewWithAccessibilityLabel:@"Close"]; } - @end diff --git a/AMYServer/AMYServer.h b/AMYServer/AMYServer.h index 2e7edff..640e5d9 100644 --- a/AMYServer/AMYServer.h +++ b/AMYServer/AMYServer.h @@ -20,5 +20,6 @@ - (void)start; - (void)stop; +- (void)closeAllRequests; @end diff --git a/AMYServer/AMYServer.m b/AMYServer/AMYServer.m index b1b84bc..c7064be 100644 --- a/AMYServer/AMYServer.m +++ b/AMYServer/AMYServer.m @@ -33,9 +33,15 @@ - (void)start - (void)stop { + [self closeAllRequests]; [_AMYURLProtocol stopMonitoringURL:self.baseURL]; } +- (void)closeAllRequests +{ + [_AMYURLProtocol closeAllRequestsWithBaseURL:self.baseURL]; +} + - (AMYRequest *)waitForRequestMatchingBlock:(KIFTestStepResult (^)(NSURLRequest *, NSError **))block { __block AMYRequest *request = nil; @@ -67,6 +73,16 @@ - (void)waitForRequestMatchingMocktail:(NSString *)mocktail withHTTPBodyMatching [self failWithError:[NSError KIFErrorWithUnderlyingError:error format:@"Failed to load mocktail: %@", error.localizedDescription] stopTest:YES]; } + NSDictionary *headers = [response headersWithValues:values error:&error]; + if (error) { + [self failWithError:[NSError KIFErrorWithUnderlyingError:error format:@"Failed to generate headers: %@", error.localizedDescription] stopTest:YES]; + } + + NSData *body = [response bodyWithValues:values error:&error]; + if (error) { + [self failWithError:[NSError KIFErrorWithUnderlyingError:error format:@"Failed to generate body: %@", error.localizedDescription] stopTest:YES]; + } + AMYRequest *request = [self waitForRequestMatchingBlock:^KIFTestStepResult(NSURLRequest *request, NSError *__autoreleasing *error) { KIFTestWaitCondition([response matchesURL:request.URL method:request.HTTPMethod patternLength:NULL], error, @"Could not find request matching mocktail."); @@ -78,16 +94,6 @@ - (void)waitForRequestMatchingMocktail:(NSString *)mocktail withHTTPBodyMatching return KIFTestStepResultSuccess; }]; - NSDictionary *headers = [response headersWithValues:values error:&error]; - if (error) { - [self failWithError:[NSError KIFErrorWithUnderlyingError:error format:@"Failed to generate headers: %@", error.localizedDescription] stopTest:YES]; - } - - NSData *body = [response bodyWithValues:values error:&error]; - if (error) { - [self failWithError:[NSError KIFErrorWithUnderlyingError:error format:@"Failed to generate body: %@", error.localizedDescription] stopTest:YES]; - } - [request respondWithStatusCode:response.statusCode headerFields:headers]; [request sendData:body]; [request close]; diff --git a/AMYServer/_AMYURLProtocol.h b/AMYServer/_AMYURLProtocol.h index db95105..4324aa2 100644 --- a/AMYServer/_AMYURLProtocol.h +++ b/AMYServer/_AMYURLProtocol.h @@ -19,6 +19,7 @@ + (AMYRequest *)findAndRemoveRequestMatchingBaseURL:(NSURL *)baseURL block:(KIFTestStepResult (^)(NSURLRequest *URLRequest, NSError **error))block error:(NSError **)error; + (void)startMonitoringURL:(NSURL *)URL; + (void)stopMonitoringURL:(NSURL *)URL; ++ (void)closeAllRequestsWithBaseURL:(NSURL *)URL; + (NSArray *)pendingURLRequestsMatchingBaseURL:(NSURL *)baseURL; @end diff --git a/AMYServer/_AMYURLProtocol.m b/AMYServer/_AMYURLProtocol.m index c7fbaac..d4ba5da 100644 --- a/AMYServer/_AMYURLProtocol.m +++ b/AMYServer/_AMYURLProtocol.m @@ -70,11 +70,16 @@ + (void)addRequestForProtocol:(_AMYURLProtocol *)protocol [[self pendingRequests] addObject:protocol]; } ++ (AMYRequest *)findAndRemoveAnyRequestMatchingBaseURL:(NSURL *)baseURL +{ + return [self findAndRemoveRequestMatchingBaseURL:baseURL block:nil error:NULL]; +} + + (AMYRequest *)findAndRemoveRequestMatchingBaseURL:(NSURL *)baseURL block:(KIFTestStepResult (^)(NSURLRequest *URLRequest, NSError **error))block error:(NSError **)error { AMYAssertMainThread(); for (_AMYURLProtocol *protocol in [self pendingRequests]) { - if ([baseURL isEqual:[self baseURLForRequest:protocol.request]] && protocol.canRespond && block(protocol.request, error) == KIFTestStepResultSuccess) { + if ([baseURL isEqual:[self baseURLForRequest:protocol.request]] && protocol.canRespond && (!block || block(protocol.request, error) == KIFTestStepResultSuccess)) { AMYRequest *request = [[AMYRequest alloc] initWithProtocol:protocol]; [[self pendingRequests] removeObject:protocol]; @@ -119,6 +124,16 @@ + (void)stopMonitoringURL:(NSURL *)URL } } ++ (void)closeAllRequestsWithBaseURL:(NSURL *)URL +{ + AMYAssertMainThread(); + AMYRequest *request; + while ((request = [self findAndRemoveAnyRequestMatchingBaseURL:URL])) { + [request respondWithStatusCode:500 headerFields:nil]; + [request close]; + } +} + + (BOOL)canInitWithRequest:(NSURLRequest *)request { return [self baseURLForRequest:request] != nil; diff --git a/Test Host/ViewController.m b/Test Host/ViewController.m index 9cec5e2..27587be 100644 --- a/Test Host/ViewController.m +++ b/Test Host/ViewController.m @@ -44,7 +44,11 @@ - (IBAction)logIn { [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { - NSLog(@"Headers: %@", [(NSHTTPURLResponse *)response allHeaderFields]); + NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; + if (HTTPResponse.statusCode != 200) { + [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%d status code", HTTPResponse.statusCode] message:nil delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil] show]; + return; + } NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];