Skip to content

Commit

Permalink
Add Nocilla to unit tests target.
Browse files Browse the repository at this point in the history
  • Loading branch information
staykids committed Feb 24, 2021
1 parent b6e870b commit 6d9b2d2
Show file tree
Hide file tree
Showing 60 changed files with 1,758 additions and 3 deletions.
258 changes: 258 additions & 0 deletions Wikipedia.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion WikipediaUnitTests/Code/LSStubResponseDSL+WithJSON.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import <Nocilla/Nocilla.h>
#import "Nocilla.h"

typedef LSStubResponseDSL * (^WithJSONMethod)(id json);

Expand Down
2 changes: 1 addition & 1 deletion WikipediaUnitTests/Code/WMFNotificationTests.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import <XCTest/XCTest.h>
#import "MWKDataStore+TemporaryDataStore.h"
#import <WMF/WMF.h>
#import <Nocilla/LSNocilla.h>
#import "LSNocilla.h"
#import "MWKDataStore+TemporaryDataStore.h"
#import "WMFRandomFileUtilities.h"
#import "NSUserDefaults+WMFReset.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#import "MWKDataStore+TemporaryDataStore.h"
#import "WMFRandomFileUtilities.h"
#import "WMFHTTPHangingProtocol.h"
#import <Nocilla/Nocilla.h>
#import "Nocilla.h"
#import "UIViewController+WMFStoryboardUtilities.h"
#import "XCTestCase+WMFBundleConvenience.h"
#import "NSBundle+TestAssets.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <Foundation/Foundation.h>
#import "LSHTTPBody.h"

@interface NSData (Nocilla) <LSHTTPBody>

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import "NSData+Nocilla.h"

@implementation NSData (Nocilla)

- (NSData *)data {
return self;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#import "LSHTTPBody.h"

@interface NSString (Nocilla) <LSHTTPBody>

- (NSRegularExpression *)regex;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import "NSString+Nocilla.h"

@implementation NSString (Nocilla)

- (NSRegularExpression *)regex {
NSError *error = nil;
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:self options:0 error:&error];
if (error) {
[NSException raise:NSInvalidArgumentException format:@"Invalid regex pattern: %@\nError: %@", self, error];
}
return regex;
}

- (NSData *)data {
return [self dataUsingEncoding:NSUTF8StringEncoding];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <Foundation/Foundation.h>
#import "LSHTTPRequest.h"

@interface LSHTTPRequestDSLRepresentation : NSObject
- (id)initWithRequest:(id<LSHTTPRequest>)request;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#import "LSHTTPRequestDSLRepresentation.h"

@interface LSHTTPRequestDSLRepresentation ()
@property (nonatomic, strong) id<LSHTTPRequest> request;
@end

@implementation LSHTTPRequestDSLRepresentation
- (id)initWithRequest:(id<LSHTTPRequest>)request {
self = [super init];
if (self) {
_request = request;
}
return self;
}

- (NSString *)description {
NSMutableString *result = [NSMutableString stringWithFormat:@"stubRequest(@\"%@\", @\"%@\")", self.request.method, [self.request.url absoluteString]];
if (self.request.headers.count) {
[result appendString:@".\nwithHeaders(@{ "];
NSMutableArray *headerElements = [NSMutableArray arrayWithCapacity:self.request.headers.count];

NSArray *descriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES]];
NSArray * sortedHeaders = [[self.request.headers allKeys] sortedArrayUsingDescriptors:descriptors];

for (NSString * header in sortedHeaders) {
NSString *value = [self.request.headers objectForKey:header];
[headerElements addObject:[NSString stringWithFormat:@"@\"%@\": @\"%@\"", header, value]];
}
[result appendString:[headerElements componentsJoinedByString:@", "]];
[result appendString:@" })"];
}
if (self.request.body.length) {
NSString *escapedBody = [[NSString alloc] initWithData:self.request.body encoding:NSUTF8StringEncoding];
escapedBody = [escapedBody stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
[result appendFormat:@".\nwithBody(@\"%@\")", escapedBody];
}
return [NSString stringWithFormat:@"%@;", result];
}
@end
39 changes: 39 additions & 0 deletions WikipediaUnitTests/Third Party/Nocilla/DSL/LSStubRequestDSL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#import <Foundation/Foundation.h>
#import "NSString+Matcheable.h"
#import "NSRegularExpression+Matcheable.h"
#import "NSData+Matcheable.h"

@class LSStubRequestDSL;
@class LSStubResponseDSL;
@class LSStubRequest;

@protocol LSHTTPBody;

typedef LSStubRequestDSL *(^WithHeaderMethod)(NSString *, NSString *);
typedef LSStubRequestDSL *(^WithHeadersMethod)(NSDictionary *);
typedef LSStubRequestDSL *(^AndBodyMethod)(id<LSMatcheable>);
typedef LSStubResponseDSL *(^AndReturnMethod)(NSInteger);
typedef LSStubResponseDSL *(^AndReturnRawResponseMethod)(NSData *rawResponseData);
typedef void (^AndFailWithErrorMethod)(NSError *error);

@interface LSStubRequestDSL : NSObject
- (id)initWithRequest:(LSStubRequest *)request;

@property (nonatomic, strong, readonly) WithHeaderMethod withHeader;
@property (nonatomic, strong, readonly) WithHeadersMethod withHeaders;
@property (nonatomic, strong, readonly) AndBodyMethod withBody;
@property (nonatomic, strong, readonly) AndReturnMethod andReturn;
@property (nonatomic, strong, readonly) AndReturnRawResponseMethod andReturnRawResponse;
@property (nonatomic, strong, readonly) AndFailWithErrorMethod andFailWithError;

@end

#ifdef __cplusplus
extern "C" {
#endif

LSStubRequestDSL * stubRequest(NSString *method, id<LSMatcheable> url);

#ifdef __cplusplus
}
#endif
72 changes: 72 additions & 0 deletions WikipediaUnitTests/Third Party/Nocilla/DSL/LSStubRequestDSL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#import "LSStubRequestDSL.h"
#import "LSStubResponseDSL.h"
#import "LSStubRequest.h"
#import "LSNocilla.h"

@interface LSStubRequestDSL ()
@property (nonatomic, strong) LSStubRequest *request;
@end

@implementation LSStubRequestDSL

- (id)initWithRequest:(LSStubRequest *)request {
self = [super init];
if (self) {
_request = request;
}
return self;
}
- (WithHeadersMethod)withHeaders {
return ^(NSDictionary *headers) {
for (NSString *header in headers) {
NSString *value = [headers objectForKey:header];
[self.request setHeader:header value:value];
}
return self;
};
}

- (WithHeaderMethod)withHeader {
return ^(NSString * header, NSString * value) {
[self.request setHeader:header value:value];
return self;
};
}

- (AndBodyMethod)withBody {
return ^(id<LSMatcheable> body) {
self.request.body = body.matcher;
return self;
};
}

- (AndReturnMethod)andReturn {
return ^(NSInteger statusCode) {
self.request.response = [[LSStubResponse alloc] initWithStatusCode:statusCode];
LSStubResponseDSL *responseDSL = [[LSStubResponseDSL alloc] initWithResponse:self.request.response];
return responseDSL;
};
}

- (AndReturnRawResponseMethod)andReturnRawResponse {
return ^(NSData *rawResponseData) {
self.request.response = [[LSStubResponse alloc] initWithRawResponse:rawResponseData];
LSStubResponseDSL *responseDSL = [[LSStubResponseDSL alloc] initWithResponse:self.request.response];
return responseDSL;
};
}

- (AndFailWithErrorMethod)andFailWithError {
return ^(NSError *error) {
self.request.response = [[LSStubResponse alloc] initWithError:error];
};
}

@end

LSStubRequestDSL * stubRequest(NSString *method, id<LSMatcheable> url) {
LSStubRequest *request = [[LSStubRequest alloc] initWithMethod:method urlMatcher:url.matcher];
LSStubRequestDSL *dsl = [[LSStubRequestDSL alloc] initWithRequest:request];
[[LSNocilla sharedInstance] addStubbedRequest:request];
return dsl;
}
19 changes: 19 additions & 0 deletions WikipediaUnitTests/Third Party/Nocilla/DSL/LSStubResponseDSL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#import <Foundation/Foundation.h>

@class LSStubResponse;
@class LSStubResponseDSL;

@protocol LSHTTPBody;

typedef LSStubResponseDSL *(^ResponseWithBodyMethod)(id<LSHTTPBody>);
typedef LSStubResponseDSL *(^ResponseWithHeaderMethod)(NSString *, NSString *);
typedef LSStubResponseDSL *(^ResponseWithHeadersMethod)(NSDictionary *);

@interface LSStubResponseDSL : NSObject
- (id)initWithResponse:(LSStubResponse *)response;

@property (nonatomic, strong, readonly) ResponseWithHeaderMethod withHeader;
@property (nonatomic, strong, readonly) ResponseWithHeadersMethod withHeaders;
@property (nonatomic, strong, readonly) ResponseWithBodyMethod withBody;

@end
40 changes: 40 additions & 0 deletions WikipediaUnitTests/Third Party/Nocilla/DSL/LSStubResponseDSL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#import "LSStubResponseDSL.h"
#import "LSStubResponse.h"
#import "LSHTTPBody.h"

@interface LSStubResponseDSL ()
@property (nonatomic, strong) LSStubResponse *response;
@end

@implementation LSStubResponseDSL
- (id)initWithResponse:(LSStubResponse *)response {
self = [super init];
if (self) {
_response = response;
}
return self;
}
- (ResponseWithHeaderMethod)withHeader {
return ^(NSString * header, NSString * value) {
[self.response setHeader:header value:value];
return self;
};
}

- (ResponseWithHeadersMethod)withHeaders; {
return ^(NSDictionary *headers) {
for (NSString *header in headers) {
NSString *value = [headers objectForKey:header];
[self.response setHeader:header value:value];
}
return self;
};
}

- (ResponseWithBodyMethod)withBody {
return ^(id<LSHTTPBody> body) {
self.response.body = [body data];
return self;
};
}
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#import "LSHTTPRequest.h"

@interface LSHTTPRequestDiff : NSObject
@property (nonatomic, assign, readonly, getter = isEmpty) BOOL empty;

- (id)initWithRequest:(id<LSHTTPRequest>)oneRequest andRequest:(id<LSHTTPRequest>)anotherRequest;
@end
Loading

0 comments on commit 6d9b2d2

Please sign in to comment.