Skip to content

Commit

Permalink
Keep original headers of Shalon URL requests
Browse files Browse the repository at this point in the history
  • Loading branch information
blochberger committed Oct 1, 2018
1 parent c806786 commit 358201c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion PrivacyKit/ShalonURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ public class ShalonURLProtocol : URLProtocol {
httpBody = Data()
}

shalon.issue(request: Http.Request(withMethod: method, andUrl: shalonParameters.requestUrl, andBody: httpBody)!) {
let actualRequest = Http.Request(
withMethod: method,
andUrl: shalonParameters.requestUrl,
andHeaders: request.allHTTPHeaderFields ?? [:],
andBody: httpBody
)!

shalon.issue(request: actualRequest) {
optionalResponse, optionalError in

assert((optionalResponse != nil) != (optionalError != nil))
Expand Down
44 changes: 44 additions & 0 deletions PrivacyKitTests/ShalonTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,48 @@ class ShalonTest: XCTestCase {
XCTAssertEqual(retrievedData, data)
}
}

func testShalonHeaders() {
let url = URL(string: "httpss://shalon1.jondonym.net:443/httpbin.org/headers")!

let sessionConfiguration = URLSessionConfiguration.ephemeral
sessionConfiguration.protocolClasses?.append(ShalonURLProtocol.self)
let session = URLSession(configuration: sessionConfiguration)
var request = URLRequest(url: url)
request.set(method: .get)
request.add(value: "1", for: .badProvider)

let responseExpectation = expectation(description: "responseExpectation")

var response: URLResponse? = nil
var responseBody : Data? = nil

let task = session.dataTask(with: request) {
(potentialData, potentialResponse, potentialError) in

response = potentialResponse
responseBody = potentialData

responseExpectation.fulfill()
}
task.resume()

waitForExpectations(timeout: 25/*seconds*/) {
optionalExpectationError in

XCTAssertNil(optionalExpectationError, "Expectation handled erroneously")
XCTAssertNotNil(response)
XCTAssertNotNil(responseBody)

let json = try! JSONSerialization.jsonObject(with: responseBody!) as! [String: Any]
let retrievedHeaders = json["headers"] as! [String: String]
var headers: [String: String] = [:]
for (key, value) in retrievedHeaders {
headers[key.lowercased()] = value
}

XCTAssertEqual(headers[Http.Header.badProvider.rawValue.lowercased()], "1")
}
}

}

0 comments on commit 358201c

Please sign in to comment.