Skip to content

Commit

Permalink
Remove HTTPURLResponse subclass
Browse files Browse the repository at this point in the history
Fixes type inference issue, that caused problem with non-200 status code responses.
venmo#75
  • Loading branch information
mr-v committed Dec 7, 2017
1 parent bedabc3 commit 003a3d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 57 deletions.
4 changes: 2 additions & 2 deletions DVR.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
3647AFBD1B335E4A00EF10D4 /* Session.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFB81B335E4A00EF10D4 /* Session.swift */; };
3647AFBE1B335E4A00EF10D4 /* SessionDataTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFB91B335E4A00EF10D4 /* SessionDataTask.swift */; };
3647AFC01B33602A00EF10D4 /* URLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFBF1B33602A00EF10D4 /* URLResponse.swift */; };
3647AFC21B3363C400EF10D4 /* HTTPURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFC11B3363C400EF10D4 /* HTTPURLResponse.swift */; };
3647AFCD1B33689000EF10D4 /* SessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFCB1B33689000EF10D4 /* SessionTests.swift */; };
3690A0851B33AA3C00731222 /* DVR.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3690A07B1B33AA3B00731222 /* DVR.framework */; };
3690A0921B33AA9400731222 /* DVR.h in Headers */ = {isa = PBXBuildFile; fileRef = 3647AF9E1B335D5500EF10D4 /* DVR.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -58,6 +57,7 @@
3690A0A21B33AA9E00731222 /* SessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFCB1B33689000EF10D4 /* SessionTests.swift */; };
36BDDB891B6716AB00878665 /* json-example.json in Resources */ = {isa = PBXBuildFile; fileRef = 36BDDB881B6716AB00878665 /* json-example.json */; };
36BDDB8A1B6716AB00878665 /* json-example.json in Resources */ = {isa = PBXBuildFile; fileRef = 36BDDB881B6716AB00878665 /* json-example.json */; };
96459AB21FD9795400F1FEF0 /* HTTPURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3647AFC11B3363C400EF10D4 /* HTTPURLResponse.swift */; };
B19D62651CB1860400E16D11 /* SessionUploadTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = B19D62641CB1860400E16D11 /* SessionUploadTask.swift */; };
B19D62681CB19AA000E16D11 /* testfile.txt in Resources */ = {isa = PBXBuildFile; fileRef = B19D62661CB18EDB00E16D11 /* testfile.txt */; };
B19D62691CB19AA200E16D11 /* testfile.txt in Resources */ = {isa = PBXBuildFile; fileRef = B19D62661CB18EDB00E16D11 /* testfile.txt */; };
Expand Down Expand Up @@ -543,10 +543,10 @@
3647AFBE1B335E4A00EF10D4 /* SessionDataTask.swift in Sources */,
3647AFBD1B335E4A00EF10D4 /* Session.swift in Sources */,
360F5F731B5C907A001AADD1 /* SessionDownloadTask.swift in Sources */,
96459AB21FD9795400F1FEF0 /* HTTPURLResponse.swift in Sources */,
3647AFBC1B335E4A00EF10D4 /* URLRequest.swift in Sources */,
3647AFBB1B335E4A00EF10D4 /* Interaction.swift in Sources */,
3647AFBA1B335E4A00EF10D4 /* Cassette.swift in Sources */,
3647AFC21B3363C400EF10D4 /* HTTPURLResponse.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
59 changes: 8 additions & 51 deletions DVR/HTTPURLResponse.swift
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
import Foundation

// There isn't a mutable HTTPURLResponse, so we have to make our own.
class HTTPURLResponse: Foundation.HTTPURLResponse {

// MARK: - Properties

private var _url: URL?
override var url: URL? {
get {
return _url ?? super.url
}

set {
_url = newValue
}
}

private var _statusCode: Int?
override var statusCode: Int {
get {
return _statusCode ?? super.statusCode
}

set {
_statusCode = newValue
}
}

private var _allHeaderFields: [AnyHashable: Any]?
override var allHeaderFields: [AnyHashable: Any] {
get {
return _allHeaderFields ?? super.allHeaderFields
}

set {
_allHeaderFields = newValue
}
}
}


extension HTTPURLResponse {
convenience init(dictionary: [String: Any]) {
let url = URL(string: dictionary["url"] as! String)!

self.init(url: url, mimeType: nil, expectedContentLength: 0, textEncodingName: nil)
convenience init?(dictionary: [String: Any]) {
guard let urlString = dictionary["url"] as? String,
let url = URL(string: urlString),
let statusCode = dictionary["status"] as? Int
else { return nil }

if let headers = dictionary["headers"] as? [String: String] {
allHeaderFields = headers
}

if let status = dictionary["status"] as? Int {
statusCode = status
}
let headers = dictionary["headers"] as? [String: String]
self.init(url: url, statusCode: statusCode, httpVersion: nil, headerFields: headers)
}
}

10 changes: 6 additions & 4 deletions DVR/Interaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ extension Interaction {

init?(dictionary: [String: Any]) {
guard let request = dictionary["request"] as? [String: Any],
let response = dictionary["response"] as? [String: Any],
let recordedAt = dictionary["recorded_at"] as? TimeInterval else { return nil }
let responseDict = dictionary["response"] as? [String: Any],
let response = HTTPURLResponse(dictionary: responseDict),
let recordedAt = dictionary["recorded_at"] as? TimeInterval
else { return nil }

self.request = NSMutableURLRequest(dictionary: request) as URLRequest
self.response = HTTPURLResponse(dictionary: response)
self.response = response
self.recordedAt = Date(timeIntervalSince1970: recordedAt)
self.responseData = Interaction.dencodeBody(response["body"], headers: response["headers"] as? [String: String])
self.responseData = Interaction.dencodeBody(responseDict["body"], headers: responseDict["headers"] as? [String: String])
}
}

0 comments on commit 003a3d5

Please sign in to comment.