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

Add basicAuthenticationHeader function #1187

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Source/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public class Request {

// MARK: - Authentication

public static func basicAuthenticationHeader(
user user: String,
password: String)
-> Dictionary<String, String>
{
let basicAuthCredentials = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding) ?? NSData()
let base64AuthCredentials = basicAuthCredentials.base64EncodedStringWithOptions([])
return ["Authorization": "Basic \(base64AuthCredentials)"]
}

/**
Associates an HTTP Basic credential with the request.

Expand Down
31 changes: 31 additions & 0 deletions Tests/AuthenticationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ class BasicAuthenticationTestCase: AuthenticationTestCase {
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")
}

func testHiddenHTTPBasicAuthentication() {
// Given
let expectation = expectationWithDescription("\(URLString) 200")

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?

// When
let authenticationHeader = Request.basicAuthenticationHeader(user: user, password: password)
manager.request(.GET, "http://httpbin.org/hidden-basic-auth/\(user)/\(password)", headers: authenticationHeader)
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError

expectation.fulfill()
}

waitForExpectationsWithTimeout(timeout, handler: nil)

// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertEqual(response?.statusCode ?? 0, 200, "response status code should be 200")
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")
}
}

// MARK: -
Expand Down