New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to stub multiple response cookies? #43
Comments
Hi @evanspa First, this question is not related to And contrary to what you state in your question, when you want to send a header with the same name multiple times, it won't look like you say on the wire. Instead, the RFC 2616 states that the various values of the header should be appended using a comma:
And that's exactly what the iOS URL Loading System does. For example when you build an NSMutableURLRequest* req = [NSMutableURLRequest new];
static NSString* const kHeader = @"Set-Cookie";
[req addValue:@"abc=123; domain=example.com; path=/;" forHTTPHeaderField:kHeader];
[req addValue:@"def=245;" forHTTPHeaderField:kHeader];
[req addValue:@"ghi=356;" forHTTPHeaderField:kHeader];
NSDictionary* headersRFC2616 = [req allHTTPHeaderFields];
NSLog(@"headers: %@", headersRFC2616);
// headers: { "Set-Cookie" = "abc=123; domain=example.com; path=/;,def=245;,ghi=356;"; } You can even check that the generated headers are correctly interpreted as cookies using Apple's NSURL* cookieOriginURL = [NSURL URLWithString:@"http://example.com/"];
for (NSHTTPCookie* cookie in [NSHTTPCookie cookiesWithResponseHeaderFields:headersRFC2616 forURL:cookieOriginURL]) {
NSLog(@"= Cookie found in header: %@", cookie);
}
/*
= Cookie found in header: <NSHTTPCookie version:0 name:"abc" value:"123" expiresDate:(null) created:2013-11-14 20:52:16 +0000 (4.06155e+08) sessionOnly:TRUE domain:".example.com" path:"/" isSecure:FALSE>
= Cookie found in header: <NSHTTPCookie version:0 name:"def" value:"245" expiresDate:(null) created:2013-11-14 20:52:16 +0000 (4.06155e+08) sessionOnly:TRUE domain:"example.com" path:"/" isSecure:FALSE>
= Cookie found in header: <NSHTTPCookie version:0 name:"ghi" value:"356" expiresDate:(null) created:2013-11-14 20:52:16 +0000 (4.06155e+08) sessionOnly:TRUE domain:"example.com" path:"/" isSecure:FALSE>
*/ So that all said, that's exactly the same for when you want to stub your requests with You can of course do this either by directly build your NSDictionary statically, appending the strings directly in your code… or use an PS: Don't forget to finish your cookie headers with a semicolon |
Thank you AliSoftware for the detailed response! I will do that (use the comma-delimited syntax). I would say though that a separate RFC speaks to cookies (RFC6265), and does indeed specify the wire format of having multiple "Set-Cookie" headers (also on Wikipedia). I wonder if it's because the "expires" attribute value can contain a comma, making comma-delimeted cookies too cumbersome? But I digress... Thank you again for the detail answer. |
Yes, I guess the "repeated-header syntax" may exist on the wire with some HTTP servers ; depending on the library used by those other softwares, maybe some repeat the But anyway, that's not how iOS does it, as in my experiance the iOS' and OSX's URL Loading System always se the comma-separated syntax (probably because internally it uses |
I was also thrown because of my Java background - the HttpServletResponse interface treats cookies as 1st-class objects, with an API for explicitly being able to add multiple of them. I guess I was expecting/hoping for iOS/OSX to do something similar. Live and learn! |
Well, And if you need to add cookies not in the The thing here is that whatever the method you use, I guess Apple chose to internally store headers in a I think Cocoa's URL Loading System still understand/correctly interpret incoming HTTP headers that are repeated on separate lines too, so that servers generating multiple lines for each According to RFC2616, both are valid, as long as the headers can be written in both formats ("it MUST be possible for headers that can be repeated to also be concatenated using comma to separate them") with equivalent meanings. Regarding your remark with RFC6265 and the possible ambiguity with If you forget the last semicolon, as in |
Ah yes - good point regarding the semicolon 😄 |
First, thank you for this excellent library. I have a question. I don't see how to stub multiple response cookies. The OHHTTPStubsResponse class contains a dictionary for the stubbed response headers, and presumably to stub a single cookie, one would add an entry to this dictionary with the key "Set-Cookie". The problem is, an HTTP response containing several cookies will look like this on the wire:
Set-Cookie: abc=123; domain=.example.com; path=/
Set-Cookie: def=245; ...
Set-Cookie: ghi=356; ...
It's the same "Set-Cookie" header repeated for each cookie. Because NSDictionary does not allow duplicate keys, I don't see how to stub this sort of response.
Thank you,
-Paul
The text was updated successfully, but these errors were encountered: