Skip to content

Commit

Permalink
Merge pull request #30 from jzucker2/develop
Browse files Browse the repository at this point in the history
Merge in ability to reset matcher
  • Loading branch information
jzucker2 committed Feb 29, 2016
2 parents 610e455 + 6dac118 commit 9b4f8ee
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 19 deletions.
2 changes: 1 addition & 1 deletion BeKindRewind.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "BeKindRewind"
s.version = "0.9.3"
s.version = "0.9.4"
s.summary = "A simple way to record and replay network requests for testing and debugging."
s.description = <<-DESC
Provides an XCTestCase subclass for easily
Expand Down
6 changes: 6 additions & 0 deletions BeKindRewind/Core/BKRRequestMatching.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@

@optional

/**
* If the matcher class stores information between recordings, then this can be implemented to reset matcher state.
* This can be expected to be called whenever the VCR calls reset
*/
- (void)reset;

/**
* Convenience callback for testing the scheme of a request for possible stubbing
*
Expand Down
11 changes: 11 additions & 0 deletions BeKindRewind/Core/Editor/BKREditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,15 @@ typedef void (^BKRCassetteEditingBlock)(BOOL updatedEnabled, BKRCassette *casset
*/
- (NSArray<BKRScene *> *)allScenes;

/**
* Resets the receiver if it contains any internal state. This should
* usually be called at the end of a session. This method is non-blocking
* and asynchronously called on the receiver's internal custom queue. This
* method sets the receiver's enabled property to NO
*
* @param completionBlock run on the receiver's queue after the reset
* actions are performed
*/
- (void)resetWithCompletionBlock:(void(^)(void))completionBlock;

@end
8 changes: 8 additions & 0 deletions BeKindRewind/Core/Editor/BKREditor.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@ - (BKRCassette *)currentCassette {
return self.currentCassette.allScenes;
}

- (void)resetWithCompletionBlock:(void (^)(void))completionBlock {
[self setEnabled:NO withCompletionHandler:^(BOOL updatedEnabled, BKRCassette *cassette) {
if (completionBlock) {
completionBlock();
}
}];
}

@end
13 changes: 13 additions & 0 deletions BeKindRewind/Core/Editor/BKRPlayingEditor.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ - (void)setEnabled:(BOOL)enabled withCompletionHandler:(BKRCassetteEditingBlock)
}];
}

- (void)resetWithCompletionBlock:(void (^)(void))completionBlock {
BKRWeakify(self);
[super resetWithCompletionBlock:^void (void){
BKRStrongify(self);
if ([self->_matcher respondsToSelector:@selector(reset)]) {
[self->_matcher reset];
}
if (completionBlock) {
completionBlock();
}
}];
}

- (void)_removeAllStubs {
[BKROHHTTPStubsWrapper removeAllStubs];
}
Expand Down
6 changes: 0 additions & 6 deletions BeKindRewind/Core/Editor/BKRRecordingEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
*/
@property (nonatomic, assign, readonly) BOOL handledRecording;

/**
* This resets the BKRRecordingEditor since it interacts with a singleton BKRRecorder. This should be called before
* releasing the instance.
*/
- (void)reset;

/**
* Add raw recordable frame representing a component of a network request to the current cassette
*
Expand Down
12 changes: 9 additions & 3 deletions BeKindRewind/Core/Editor/BKRRecordingEditor.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ - (instancetype)init {
return self;
}

- (void)reset {
// This resets the BKRRecordingEditor since it interacts with a
// singleton BKRRecorder. This should be called before
// releasing the instance.
- (void)resetWithCompletionBlock:(void (^)(void))completionBlock {
BKRWeakify(self);
dispatch_barrier_async(self.editingQueue, ^{
[super resetWithCompletionBlock:^void (void){
BKRStrongify(self);
self->_handledRecording = NO;
self->_recordingStartTime = nil;
});
if (completionBlock) {
completionBlock();
}
}];
}


Expand Down
4 changes: 3 additions & 1 deletion BeKindRewind/Core/Player/BKRPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@

/**
* Reset the player's enabled state along with before
* and after playback blocks
* and after playback blocks. If the matcher saves any state,
* that can be reset here by implemented the optional `reset` method
* in the BKRRequestMatching protocol
*/
- (void)resetWithCompletionBlock:(void (^)(void))completionBlock;

Expand Down
2 changes: 1 addition & 1 deletion BeKindRewind/Core/Player/BKRPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ - (BOOL)isEnabled {

- (void)resetWithCompletionBlock:(void (^)(void))completionBlock {
self.currentCassette = nil;
[self.editor setEnabled:NO withCompletionHandler:^(BOOL updatedEnabled, BKRCassette *cassette) {
[self.editor resetWithCompletionBlock:^{
if (completionBlock) {
completionBlock();
}
Expand Down
3 changes: 1 addition & 2 deletions BeKindRewind/Core/Recorder/BKRRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ - (void)resetWithCompletionBlock:(void (^)(void))completionBlock {
self.currentCassette = nil;
self.beginRecordingBlock = nil;
self.endRecordingBlock = nil;
[self.editor reset];
[self.editor editCassette:^(BOOL updatedEnabled, BKRCassette *cassette) {
[self.editor resetWithCompletionBlock:^{
if (completionBlock) {
completionBlock();
}
Expand Down
2 changes: 2 additions & 0 deletions Example/Tests/Helpers/XCTestCase+BKRHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ typedef void (^BKRTestBatchNetworkTimeoutCompletionHandler)(BKRTestExpectedResul
- (BKRTestExpectedResult *)HTTPBinCancelledRequestWithRecording:(BOOL)isRecording;
- (BKRTestExpectedResult *)HTTPBinGetRequestWithQueryString:(NSString *)queryString withRecording:(BOOL)isRecording;
- (BKRTestExpectedResult *)HTTPBinPostRequestWithRecording:(BOOL)isRecording;
- (BKRTestExpectedResult *)HTTPBinRedirectWithRecording:(BOOL)isRecording;
- (BKRTestExpectedResult *)HTTPBinDripDataWithRecording:(BOOL)isRecording;

#pragma mark - PN Helpers

Expand Down
27 changes: 27 additions & 0 deletions Example/Tests/Helpers/XCTestCase+BKRHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ - (NSDictionary *)_HTTPBinResponseAllHeaderFieldsWithContentLength:(NSString *)c
};
}

- (NSDictionary *)_HTTPBinChunkedResponseAllHeaderFieldsWithContentLength:(NSString *)contentLengthString {
NSMutableDictionary *mutableOriginalDictionary = [[self _HTTPBinResponseAllHeaderFieldsWithContentLength:contentLengthString] mutableCopy];
mutableOriginalDictionary[@"Content-Type"] = @"application/octet-stream";
return mutableOriginalDictionary.copy;
}

- (NSDictionary *)_PNResponseAllHeaderFieldsWithContentLength:(NSString *)contentLengthString {
return @{
@"Access-Control-Allow-Methods": @"GET",
Expand Down Expand Up @@ -868,6 +874,27 @@ - (BKRTestExpectedResult *)HTTPBinGetRequestWithQueryString:(NSString *)queryStr
return expectedResult;
}

- (BKRTestExpectedResult *)HTTPBinDripDataWithRecording:(BOOL)isRecording {
BKRTestExpectedResult *expectedResult = [BKRTestExpectedResult result];
expectedResult.isRecording = isRecording;
expectedResult.hasCurrentRequest = YES;
expectedResult.URLString = @"https://httpbin.org/drip?numbytes=30000&duration=0&code=200";
expectedResult.currentRequestAllHTTPHeaderFields = [self _expectedGETCurrentRequestAllHTTPHeaderFields];
expectedResult.responseCode = 200;
expectedResult.responseAllHeaderFields = [self _HTTPBinChunkedResponseAllHeaderFieldsWithContentLength:@"30000"];
expectedResult.expectedNumberOfFrames = 4;

return expectedResult;
}

- (BKRTestExpectedResult *)HTTPBinRedirectWithRecording:(BOOL)isRecording {
BKRTestExpectedResult *expectedResult = [BKRTestExpectedResult result];
expectedResult.isRecording = isRecording;
expectedResult.URLString = @"http://httpbin.org/redirect/6";

return expectedResult;
}

- (BKRTestExpectedResult *)HTTPBinPostRequestWithRecording:(BOOL)isRecording {
BKRTestExpectedResult *result = [BKRTestExpectedResult result];
result.isRecording = isRecording;
Expand Down
11 changes: 11 additions & 0 deletions Example/Tests/TestCases/BKRRecorderTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,15 @@ - (void)testRecordingTwoSimultaneousGETRequests {
}];
}

- (void)DISABLE_testRecordingChunkedResponseRequest {
BKRTestExpectedResult *expectedResult = [self HTTPBinDripDataWithRecording:YES];

[self BKRTest_executeHTTPBinNetworkCallsForExpectedResults:@[expectedResult] simultaneously:NO withTaskCompletionAssertions:^(BKRTestExpectedResult *result, NSURLSessionTask *task, NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"result: %@", result);
NSLog(@"task: %@", task);
} taskTimeoutHandler:^(BKRTestExpectedResult *result, NSURLSessionTask *task, NSError *error, BKRTestBatchSceneAssertionHandler batchSceneAssertions) {
batchSceneAssertions([BKRRecorder sharedInstance].allScenes);
}];
}

@end
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- BeKindRewind (0.9.3):
- BeKindRewind (0.9.4):
- OHHTTPStubs (~> 4.7.1)
- OHHTTPStubs (4.7.1):
- OHHTTPStubs/Default (= 4.7.1)
Expand All @@ -23,7 +23,7 @@ EXTERNAL SOURCES:
:path: .

SPEC CHECKSUMS:
BeKindRewind: 0f322f16427cf9305821c9a2e80e3f5d3386cc0f
BeKindRewind: 2794c487e9bc4eed434469abec60d207acdad1c9
OHHTTPStubs: f7d1604d04d37d055460c0ef9af01bb249ded0fa

COCOAPODS: 0.39.0
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ Jordan Zucker, jordan.zucker@gmail.com
BeKindRewind is available under the MIT license. See the LICENSE file for more info.

## Release criteria
* Rewrite README instructions
* proper support for redirects
* handle multi-part data
* proper header file
* tests for matcher classes
* tests for OSX, tvOS

## Future features
* swift tests (at least basic)
* swift tests (at least basic)/Swift Package Manager
* Code example for playing back in the README (not just recording)
* explain fixture write directory hack for easy recording
* Separate into subspecs
* add code coverage
* test different types of errors (timeout, invalid url) for playing/recording
Expand Down

0 comments on commit 9b4f8ee

Please # to comment.