Skip to content

Commit

Permalink
Replace mock server by unit tests, README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jlnr committed Oct 5, 2018
1 parent 5d8fc17 commit 5a7a649
Show file tree
Hide file tree
Showing 31 changed files with 780 additions and 1,664 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
xcuserdata
*.xcworkspace/
/Example/Pods/
/Example/MockServer/session_??????
Pods
29 changes: 15 additions & 14 deletions Classes/JLNRSessionProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,37 @@
// JLNRSessionProtocolTest
//
// Created by Julian Raschke on 11.06.14.
//
// Copyright © 2014 Raschke & Ludwig GbR. All rights reserved.
//


#import <Foundation/Foundation.h>


@protocol JLNRSession;
NS_ASSUME_NONNULL_BEGIN

@protocol JLNRSession

@interface JLNRSessionProtocol : NSURLProtocol
@required

+ (void)registerSession:(id<JLNRSession>)session;
+ (void)invalidateSession:(id<JLNRSession>)session;
- (BOOL)shouldHandleRequest:(NSURLRequest *)request;

@end
- (nullable NSURLRequest *)loginRequestBeforeRequest:(NSURLRequest *)request;

- (nullable NSURLRequest *)loginRequestAfterResponse:(NSHTTPURLResponse *)response data:(NSData *)data;

@protocol JLNRSession
- (void)applySecretToRequest:(NSMutableURLRequest *)request;

@required

- (BOOL)sessionShouldHandleRequest:(NSURLRequest *)request;
- (BOOL)storeSecretFromResponse:(NSHTTPURLResponse *)response data:(NSData *)data;

- (NSURLRequest *)sessionRequestBeforeRequest:(NSURLRequest *)request;
@end

- (NSURLRequest *)sessionRequestAfterResponse:(NSURLResponse *)response data:(NSData *)data;

- (void)applySessionToRequest:(NSMutableURLRequest *)request;
@interface JLNRSessionProtocol : NSURLProtocol

- (BOOL)storeSessionFromResponse:(NSURLResponse *)response data:(NSData *)data;
+ (void)registerSession:(id<JLNRSession>)session;
+ (void)invalidateSession:(id<JLNRSession>)session;

@end

NS_ASSUME_NONNULL_END
135 changes: 67 additions & 68 deletions Classes/JLNRSessionProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// JLNRSessionProtocolTest
//
// Created by Julian Raschke on 11.06.14.
//
// Copyright © 2014 Raschke & Ludwig GbR. All rights reserved.
//

#import "JLNRSessionProtocol.h"


#ifdef JLNR_SESSION_PROTOCOL_TEST
#ifdef DEBUG
#define JLNRLog(...) NSLog(__VA_ARGS__)
#else
#define JLNRLog(...)
Expand All @@ -19,18 +19,19 @@
static NSPointerArray *sessions;


typedef NS_ENUM(NSInteger, JLNRSessionRequestState) {
JLNRSessionRequestStateBeforeRequest = 0,
JLNRSessionRequestStateFirstChance,
JLNRSessionRequestStateAfterResponse,
JLNRSessionRequestStateSecondChance,
JLNRSessionRequestStateFinished,
// Not prefixed, since it is only used internally.
typedef NS_ENUM(NSInteger, RequestState) {
RequestStateBeforeRequest = 0,
RequestStateFirstChance,
RequestStateAfterResponse,
RequestStateSecondChance,
RequestStateFinished,
};


@interface JLNRSessionProtocol () <NSURLConnectionDataDelegate>

@property (nonatomic) JLNRSessionRequestState state;
@property (nonatomic) RequestState state;
@property (nonatomic) NSURLConnection *connection;
@property (nonatomic) id<JLNRSession> session;

Expand All @@ -55,43 +56,44 @@ + (void)initialize
+ (void)registerSession:(id<JLNRSession>)session
{
@synchronized(sessions) {
NSUInteger lastSessionCount = [sessions count];

[sessions addPointer:(__bridge void *)(session)];

if (lastSessionCount == 0 && [sessions count] > 0) {
[sessions compact];
[sessions addPointer:(__bridge void *)session];
if ([sessions count] == 1) {
[NSURLProtocol registerClass:self.class];
JLNRLog(@"Registered JLNRSessionProtocol");
}
}
}

+ (void)invalidateSession:(id<JLNRSession>)session
{
@synchronized(sessions) {
NSUInteger lastSessionCount = [sessions count];

for (NSInteger i = 0; i < lastSessionCount; ++i) {
[sessions compact];
NSUInteger previousSessionCount = sessions.count;
for (NSInteger i = 0; i < previousSessionCount; ++i) {
if ([sessions pointerAtIndex:i] == (__bridge void *)session) {
[sessions replacePointerAtIndex:i withPointer:NULL];
break;
}
}
[sessions compact];

if (lastSessionCount > 0 && [sessions count] == 0) {
if (previousSessionCount > 0 && sessions.count == 0) {
[NSURLProtocol unregisterClass:self.class];
JLNRLog(@"Unregistered JLNRSessionProtocol");
}
}
}

+ (id<JLNRSession>)firstSessionInterestedInRequest:(NSURLRequest *)request
+ (nullable id<JLNRSession>)firstSessionInterestedInRequest:(NSURLRequest *)request
{
NSArray *currentSessions = nil;
NSArray *currentSessions;
@synchronized(sessions) {
[sessions compact];
currentSessions = [sessions allObjects];
}

for (id<JLNRSession> session in currentSessions) {
if ([session sessionShouldHandleRequest:request]) {
if ([session shouldHandleRequest:request]) {
return session;
}
}
Expand All @@ -103,15 +105,10 @@ + (void)invalidateSession:(id<JLNRSession>)session

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
BOOL isInternalRequest =
[NSURLProtocol propertyForKey:NSStringFromClass(self.class)
inRequest:request] != nil;

if (isInternalRequest) {
return NO;
}

return [self firstSessionInterestedInRequest:request] != nil;
id internalRequestMarker = [NSURLProtocol propertyForKey:NSStringFromClass(self.class)
inRequest:request];
// Do not pass internally created requests on to our registered sessions.
return internalRequestMarker == nil && [self firstSessionInterestedInRequest:request] != nil;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
Expand All @@ -138,7 +135,7 @@ - (void)startLoading

- (void)stopLoading
{
if (self.state != JLNRSessionRequestStateFinished) {
if (self.state != RequestStateFinished) {
JLNRLog(@"Stopping request in state %@: %@", @(self.state), self.request);
}

Expand All @@ -152,49 +149,57 @@ - (void)sendNextRequestOrFinish
{
NSMutableURLRequest *nextRequest = nil;

if (self.state == JLNRSessionRequestStateBeforeRequest) {
nextRequest = [[self.session sessionRequestBeforeRequest:self.request] mutableCopy];
JLNRLog(@"Advancing one step for request %@, state = %@", self.request.URL, @(self.state));

if (self.state == RequestStateBeforeRequest) {
nextRequest = [[self.session loginRequestBeforeRequest:self.request] mutableCopy];

if (nextRequest == nil) {
self.state = JLNRSessionRequestStateFirstChance;
}
else {
JLNRLog(@"Opening session before request: %@", self.request);
self.state = RequestStateFirstChance;
JLNRLog(@"No need to log in before request: %@", self.request.URL);
} else {
JLNRLog(@"Logging in before request: %@", self.request.URL);
}
}

if (self.state == JLNRSessionRequestStateFirstChance ||
self.state == JLNRSessionRequestStateSecondChance) {
if (self.state == RequestStateFirstChance ||
self.state == RequestStateSecondChance) {

JLNRLog(@"Will try to send request %@", self.request.URL);
nextRequest = [self.request mutableCopy];

[self.session applySessionToRequest:nextRequest];
[self.session applySecretToRequest:nextRequest];
}

if (self.state == JLNRSessionRequestStateAfterResponse) {

if (self.state == RequestStateAfterResponse) {
nextRequest =
[[self.session sessionRequestAfterResponse:self.currentResponse
data:self.currentData] mutableCopy];
[[self.session loginRequestAfterResponse:(NSHTTPURLResponse *)self.currentResponse
data:self.currentData] mutableCopy];

if (nextRequest == nil) {
self.state = JLNRSessionRequestStateFinished;
self.state = RequestStateFinished;
JLNRLog(@"No need to log in after request: %@", self.request.URL);
} else {
JLNRLog(@"Logging in after request: %@", self.request.URL);
}
}

if (self.state == JLNRSessionRequestStateFinished) {
if (self.state == RequestStateFinished) {
JLNRLog(@"Finished - passing response to outer request");
[self finish];
}
else {
} else {
// If we get here, we need to send a login request.
NSAssert(nextRequest, @"we must have a request to send at this point");

JLNRLog(@"Opening connection for %@", nextRequest.URL);

// Mark this request as internal to JLNRSessionProtocol so we can ignore it later.
[NSURLProtocol setProperty:@YES
forKey:NSStringFromClass(self.class)
inRequest:nextRequest];

self.currentResponse = nil;
self.currentData = nil;
self.connection = [NSURLConnection connectionWithRequest:nextRequest
delegate:self];

self.connection = [NSURLConnection connectionWithRequest:nextRequest delegate:self];
[self.connection start];
}
}
Expand All @@ -215,14 +220,14 @@ - (void)finish

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
JLNRLog(@"connection: %@ didReceiveResponse: %@", connection, response);
NSParameterAssert([response isKindOfClass:[NSHTTPURLResponse class]]);
JLNRLog(@"connection: %@ didReceiveResponse: statusCode=%@",
connection, @(((NSHTTPURLResponse *)response).statusCode));

self.currentResponse = response;
self.currentData = [NSMutableData data];

if (self.state == JLNRSessionRequestStateFirstChance ||
self.state == JLNRSessionRequestStateSecondChance) {

if (self.state == RequestStateFirstChance || self.state == RequestStateSecondChance) {
self.originalResponse = response;
}
}
Expand All @@ -236,16 +241,11 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
JLNRLog(@"connectionDidFinishLoading: %@", connection);

if (self.state == JLNRSessionRequestStateFirstChance ||
self.state == JLNRSessionRequestStateSecondChance) {

if (self.state == RequestStateFirstChance || self.state == RequestStateSecondChance) {
self.originalData = self.currentData;
}
else if (self.state == JLNRSessionRequestStateBeforeRequest ||
self.state == JLNRSessionRequestStateAfterResponse) {

if (! [self.session storeSessionFromResponse:self.currentResponse
data:self.currentData]) {
} else if (self.state == RequestStateBeforeRequest || self.state == RequestStateAfterResponse) {
if (! [self.session storeSecretFromResponse:(NSHTTPURLResponse *)self.currentResponse
data:self.currentData]) {

NSError *error = [NSError errorWithDomain:NSStringFromClass(self.class)
code:0
Expand All @@ -264,8 +264,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
[self sendNextRequestOrFinish];
}

- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
JLNRLog(@"connection:didFailWithError: %@", connection);

Expand Down
Loading

0 comments on commit 5a7a649

Please sign in to comment.