-
Notifications
You must be signed in to change notification settings - Fork 9
Add ability to use auth header as part of file name #38
base: master
Are you sure you want to change the base?
Changes from all commits
972672d
40bb464
3bf4877
2c78842
388c380
f20b92d
2b6f83b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| HTTP/1.1 202 Accepted | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't expect a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was mostly just to match existing files -
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. Like I said, though, doesn't really matter for how it's being used. |
||
|
|
||
| {"favorite_dog_breed": "dogfish"} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| HTTP/1.1 202 Accepted | ||
|
|
||
| {"favorite_dog_breed": "dogfish"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| HTTP/1.1 202 Accepted | ||
|
|
||
| {"favorite_dog_breed": "dogfish"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,3 +86,7 @@ In order to switch back and forth between mock and live, you can also take out t | |
| ### Using with Frameworks/Swift | ||
|
|
||
| When `VOKMockUrlProtocol` is built as a framework (usually for use with Swift), make sure to call the `setTestBundle:` class method and pass in your test bundle. Since the default behavior is to fall back to the bundle for the current class, that would look in the Framework's bundle rather than the test bundle, and nothing would work. | ||
|
|
||
| ### Using with HTTP Authorization headers | ||
|
|
||
| To verify that authorization headers are being sent properly, make sure to set `setShouldEncodeAuthHeader` to YES. This will verify that the value for the http header "Authorization" is set to an expected value. Otherwise, auth headers will not be verified. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this no longer matches the code.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true but I wanted to hold off rewriting this until we had a better idea on what the final bit would be. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,4 +20,12 @@ | |
| */ | ||
| + (void)setTestBundle:(NSBundle *)bundle; | ||
|
|
||
| /** | ||
| * Allows you to encode authorizatation headers if desired. Defaults to nil. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Description doesn't match revised method. |
||
| * | ||
| * @param headers The names of headers to encode as part of the file name as strings, | ||
| * or nil to not encode any headers. | ||
| */ | ||
| + (void)setHeadersToEncode:(NSArray<NSString *>*)headers; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be inclined to add a getter method, too. |
||
|
|
||
| @end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| #import <ILGHttpConstants/HTTPStatusCodes.h> | ||
| #import <ILGHttpConstants/HTTPMethods.h> | ||
| #import <ILGHttpConstants/HttpHeaderFields.h> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to work fine without it but just in case it breaks in the future
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most OS X filesystems are set up case-insensitive, so it'd probably be okay, except on that rare case-sensitive filesystem. |
||
| #import <VOKBenkode/VOKBenkode.h> | ||
| #import <sys/syslimits.h> | ||
|
|
||
|
|
@@ -55,6 +56,12 @@ + (void)setTestBundle:(NSBundle *)bundle | |
| testBundle = bundle; | ||
| } | ||
|
|
||
| static NSArray<NSString *> *headersToEncode = nil; | ||
| + (void)setHeadersToEncode:(NSArray<NSString *>*)headers | ||
| { | ||
| headersToEncode = headers; | ||
| } | ||
|
|
||
| + (BOOL)canInitWithRequest:(NSURLRequest *)request | ||
| { | ||
| return YES; | ||
|
|
@@ -111,6 +118,28 @@ - (NSArray *)resourceNames | |
| [resourceName appendFormat:queryFormat, self.request.URL.query]; | ||
| } | ||
|
|
||
| if (headersToEncode) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if there are no headers to encode, we still need to add a separator to distinguish the header segment of the file name. |
||
| NSDictionary *headerDict = self.request.allHTTPHeaderFields; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a potential pitfall here in that, at least according to Apple's docs, header names are case-sensitive, but my experience has been that the majority of servers treat them as case-insensitive. Basically, if the app sets I'd be tempted to iterate over
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a note when I finalize the documentation that this is case-sensitive. |
||
| NSMutableArray <NSString *>*allIncludedHeaders = [NSMutableArray array]; | ||
| for (NSString *headerName in headersToEncode) { | ||
| NSString *headerValue = headerDict[headerName]; | ||
| if (headerValue) { | ||
| NSString *headerString = [NSString stringWithFormat:@"%@=%@", headerName, headerValue]; | ||
| [allIncludedHeaders addObject:headerString]; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of building up a string, how about building up a dictionary mapping header names to values, then bencoding it to put into the resource name? Maybe that'd also allow taking the all-headers dict and filtering it rather than iterating over it? |
||
|
|
||
| NSString *fullHeaderString = [allIncludedHeaders componentsJoinedByString:@"&"]; | ||
| if (fullHeaderString.length > 0) { | ||
| NSString *encodedHeaderString = [fullHeaderString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line generating a deprecation warning? |
||
| NSData *fullHeaderData = [encodedHeaderString dataUsingEncoding:NSUTF8StringEncoding]; | ||
| NSString *hashedHeaderString = [self sha256HexOfData:fullHeaderData]; | ||
|
|
||
| [resourceNames addObject:[[resourceName stringByAppendingFormat:AppendSeparatorFormat, hashedHeaderString] mutableCopy]]; | ||
| [resourceName appendFormat:AppendSeparatorFormat, encodedHeaderString]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may already be multiple resource names at this point, so I think you have to add your potential two additions to each existing resource name—take a look below, around lines 180–188. |
||
| } | ||
| } | ||
|
|
||
| // If the request is one that can have a body... | ||
| if ([kHTTPMethodPost isEqualToString:self.request.HTTPMethod] | ||
| || [kHTTPMethodPatch isEqualToString:self.request.HTTPMethod] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| Pod::Spec.new do |s| | ||
| s.name = "VOKMockUrlProtocol" | ||
| s.version = "2.2.0" | ||
| s.version = "2.2.1" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to need to be a major version bump, since it'll break compatibility with the existing naming format. |
||
| s.summary = "A url protocol that parses and returns fake responses with mock data." | ||
| s.homepage = "https://github.com/vokal/VOKMockUrlProtocol" | ||
| s.license = { :type => "MIT", :file => "LICENSE"} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to have a convenience wrapper method like
to avoid adding these two lines in a bunch of places?