Skip to content
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

Only set cookies in debugPrintable when relevant #516

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions Source/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,14 @@ extension Request: DebugPrintable {
// Temporarily disabled on OS X due to build failure for CocoaPods
// See https://github.com/CocoaPods/swift/issues/24
#if !os(OSX)
if let cookieStorage = session.configuration.HTTPCookieStorage,
cookies = cookieStorage.cookiesForURL(URL!) as? [NSHTTPCookie]
where !cookies.isEmpty
{
let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value ?? String());" }
components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"")
if session.configuration.HTTPShouldSetCookies == true {
if let cookieStorage = session.configuration.HTTPCookieStorage,
cookies = cookieStorage.cookiesForURL(URL!) as? [NSHTTPCookie]
where !cookies.isEmpty
{
let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value ?? String());" }
components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"")
}
}
#endif

Expand Down
32 changes: 32 additions & 0 deletions Tests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
manager.startRequestsImmediately = false
return manager
}()

let managerDisallowingCookies: Alamofire.Manager = {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPShouldSetCookies = false

let manager = Alamofire.Manager(configuration: config)
manager.startRequestsImmediately = false

return manager
}()

// MARK: Tests

Expand Down Expand Up @@ -268,6 +278,28 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
XCTAssertEqual(components[5..<6], ["-b"], "command should contain -b flag")
#endif
}

func testPOSTRequestWithCookiesDisabledDebugDescription() {
// Given
let URLString = "http://httpbin.org/post"

let properties = [
NSHTTPCookieDomain: "httpbin.org",
NSHTTPCookiePath: "/post",
NSHTTPCookieName: "foo",
NSHTTPCookieValue: "bar",
]
let cookie = NSHTTPCookie(properties: properties)!
managerDisallowingCookies.session.configuration.HTTPCookieStorage?.setCookie(cookie)

// When
let request = managerDisallowingCookies.request(.POST, URLString)
let components = cURLCommandComponents(request)

// Then
let cookieComponents = components.filter { $0 == "-b" }
XCTAssertEqual(cookieComponents.count, 0, "Cookie cURL argument should not exist if cookies are disabled in the session config")
}

// MARK: Test Helper Methods

Expand Down