99//
1010
1111#import < Foundation/Foundation.h>
12- #import " MocktailResponse .h"
12+ #import " _AMYMocktailResponse .h"
1313#import < GRMustache.h>
1414
15- NSString * const MocktailFileExtension = @" tail" ;
15+ NSString * const _AMYMocktailFileExtension = @" tail" ;
1616
17- @interface MocktailResponse ()
17+ @interface _AMYMocktailResponse ()
1818@property (nonatomic , strong ) NSRegularExpression *methodRegex;
1919@property (nonatomic , strong ) NSRegularExpression *absoluteURLRegex;
2020@property (nonatomic , strong ) NSURL *fileURL;
@@ -23,7 +23,7 @@ @interface MocktailResponse ()
2323@property (nonatomic , assign ) NSUInteger bodyOffset;
2424@end
2525
26- @implementation MocktailResponse
26+ @implementation _AMYMocktailResponse
2727
2828+ (instancetype )responseFromFileAtURL : (NSURL *)url ;
2929{
@@ -45,12 +45,27 @@ + (instancetype)responseFromFileAtURL:(NSURL *)url;
4545 NSLog (@" Invalid amount of lines: %u " , (unsigned )[lines count ]);
4646 return nil ;
4747 }
48-
49- MocktailResponse *response = [[self alloc ] init ];
48+
49+ _AMYMocktailResponse *response = [[self alloc ] init ];
5050 response.methodRegex = [NSRegularExpression regularExpressionWithPattern: lines[0 ] options: NSRegularExpressionCaseInsensitive error: nil ];
5151 response.absoluteURLRegex = [NSRegularExpression regularExpressionWithPattern: lines[1 ] options: NSRegularExpressionCaseInsensitive error: nil ];
5252 response.statusCode = [lines[2 ] integerValue ];
53- response.headers = @{@" Content-Type" :lines[3 ]};
53+ NSMutableDictionary *headers = @{@" Content-Type" :lines[3 ]}.mutableCopy ;
54+
55+ // From line 5 to '\n\n', expect HTTP response headers.
56+ NSRegularExpression *headerPattern = [NSRegularExpression regularExpressionWithPattern: @" ^([^:]+):\\ s+(.*)" options: 0 error: NULL ];
57+ for (NSUInteger line = 4 ; line < lines.count ; line ++) {
58+ NSString *headerLine = lines[line];
59+ NSTextCheckingResult *match = [headerPattern firstMatchInString: headerLine options: 0 range: NSMakeRange (0 , headerLine.length)];
60+
61+ if (match) {
62+ NSString *key = [headerLine substringWithRange: [match rangeAtIndex: 1 ]];
63+ NSString *value = [headerLine substringWithRange: [match rangeAtIndex: 2 ]];
64+ headers[key] = value;
65+ }
66+ }
67+
68+ response.headers = headers.copy ;
5469 response.fileURL = url;
5570 response.bodyOffset = [headerMatter dataUsingEncoding: originalEncoding].length + 2 ;
5671 return response;
@@ -72,6 +87,16 @@ - (BOOL)matchesURL:(NSURL *)URL method:(NSString *)method patternLength:(NSUInte
7287 return NO ;
7388}
7489
90+ - (NSDictionary *)headersWithValues : (NSDictionary *)values
91+ {
92+ NSMutableDictionary *headers = [NSMutableDictionary dictionary ];
93+ [self .headers enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
94+ NSString *transformedObj = [GRMustacheTemplate renderObject: values fromString: obj error: NULL ];
95+ headers[key] = transformedObj ?: obj;
96+ }];
97+ return headers.copy ;
98+ }
99+
75100- (NSData *)bodyWithValues : (NSDictionary *)values ;
76101{
77102 NSData *body = [NSData dataWithContentsOfURL: self .fileURL];
0 commit comments