Skip to content

Commit

Permalink
remove content-type header setting in interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Wu committed Aug 31, 2023
1 parent c891933 commit 45aeb9e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ struct AuthTokenURLRequestInterceptor: URLRequestInterceptor {

mutableRequest.setValue(amzDate,
forHTTPHeaderField: URLRequestConstants.Header.xAmzDate)
mutableRequest.setValue(URLRequestConstants.ContentType.applicationJson,
forHTTPHeaderField: URLRequestConstants.Header.contentType)
mutableRequest.setValue(userAgent,
forHTTPHeaderField: URLRequestConstants.Header.userAgent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct IAMURLRequestInterceptor: URLRequestInterceptor {
throw APIError.unknown("Could not get host from mutable request", "")
}

request.setValue(URLRequestConstants.ContentType.applicationJson, forHTTPHeaderField: URLRequestConstants.Header.contentType)
request.setValue(host, forHTTPHeaderField: "host")
request.setValue(userAgent, forHTTPHeaderField: URLRequestConstants.Header.userAgent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ final public class AWSRESTOperation: AmplifyOperation<
}
}

// The headers from the request object should override any of the same header modifications done by the intercepters above
finalRequest = RESTOperationRequestUtils.applyCustomizeRequestHeaders(request.headers, on: finalRequest)

if isCancelled {
finish()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,19 @@ final class RESTOperationRequestUtils {
headers: [String: String]?,
requestPayload: Data?) -> URLRequest {

let baseHeaders = ["content-type": "application/json"]
var baseRequest = URLRequest(url: url)
baseRequest = applyCustomizeRequestHeaders(
baseHeaders.merging(headers ?? [:], uniquingKeysWith: { _, new in new }),
on: baseRequest
)
var requestHeaders = ["content-type": "application/json"]
if let headers = headers {
for (key, value) in headers {
requestHeaders[key.lowercased()] = value
}
}
baseRequest.allHTTPHeaderFields = requestHeaders
baseRequest.httpMethod = operationType.rawValue
baseRequest.httpBody = requestPayload
return baseRequest
}

static func applyCustomizeRequestHeaders(_ headers: [String: String]?, on request: URLRequest) -> URLRequest {
guard let headers = headers,
let mutableRequest = (request as NSURLRequest).mutableCopy() as? NSMutableURLRequest
else {
return request
}

for (key, value) in headers {
mutableRequest.setValue(value, forHTTPHeaderField: key)
}

return mutableRequest as URLRequest
}

private static let permittedQueryParamCharacters = CharacterSet.alphanumerics
.union(.init(charactersIn: "/_-.~"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class AuthTokenURLRequestInterceptorTests: XCTestCase {
func testAuthTokenInterceptor() async throws {
let mockTokenProvider = MockTokenProvider()
let interceptor = AuthTokenURLRequestInterceptor(authTokenProvider: mockTokenProvider)
let request = URLRequest(url: URL(string: "http://anapiendpoint.ca")!)
let request = RESTOperationRequestUtils.constructURLRequest(
with: URL(string: "http://anapiendpoint.ca")!,
operationType: .get,
headers: nil,
requestPayload: nil
)

guard let headers = try await interceptor.intercept(request).allHTTPHeaderFields else {
XCTFail("Failed retrieving headers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,6 @@ class RESTRequestUtilsTests: XCTestCase {
)
)
}

func testApplyCustomizeRequestHeaders_withCutomeHeaders_successfullyOverride() {
var request = URLRequest(url: URL(string: "https://aws.amazon.com")!)
request.allHTTPHeaderFields = ["Content-Type": "application/json"]
let headers = ["Content-Type": "text/plain"]
let requestWithHeaders = RESTOperationRequestUtils.applyCustomizeRequestHeaders(headers, on: request)
XCTAssertNotNil(requestWithHeaders.allHTTPHeaderFields)
for (key, value) in headers {
XCTAssertEqual(requestWithHeaders.allHTTPHeaderFields![key], value)
}
}

}

extension RESTRequestUtilsTests {
Expand Down

0 comments on commit 45aeb9e

Please sign in to comment.