Simplify AFNetworking usage.

This commit is contained in:
Matthew Chen 2021-10-14 11:31:08 -03:00
parent e84d765d1e
commit 1b21755455
3 changed files with 64 additions and 61 deletions

View File

@ -3,6 +3,7 @@
//
import XCTest
import CoreServices
@testable import SignalMessaging
class SignalAttachmentTest: SignalBaseTest {

View File

@ -406,9 +406,11 @@ public class OWSURLSession: NSObject {
// Ensure certain invariants for all requests.
private func prepareRequest(request: URLRequest) -> URLRequest {
var request = request
request.httpShouldHandleCookies = httpShouldHandleCookies.get()
var request = OWSHttpHeaders.fillInMissingDefaultHeaders(request: request)
request = OWSHttpHeaders.fillInMissingDefaultHeaders(request: request)
if signalService.isCensorshipCircumventionActive,
let frontingURL = self.frontingURL,
@ -440,22 +442,22 @@ public class OWSURLSession: NSObject {
var baseUrl: URL? = self.baseUrl
if signalService.isCensorshipCircumventionActive {
if let censorshipCircumventionPrefix = customCensorshipCircumventionPrefix?.nilIfEmpty {
// When we are domain fronting, we target a fronting host and add a path prefix.
let ccBaseUrl: URL = signalService.domainFrontBaseURL.appendingPathComponent(censorshipCircumventionPrefix)
baseUrl = ccBaseUrl
if !Self.isValidUrlForCensorshipCircumvention(urlString,
frontingURL: ccBaseUrl) {
Logger.warn("Unfronted URL: \(urlString), ccBaseUrl: \(ccBaseUrl)")
}
} else if let frontingURL = self.frontingURL {
// Only requests to Signal services require CC.
// If frontingHost is nil, this instance of OWSURLSession does not perform CC.
if !Self.isValidUrlForCensorshipCircumvention(urlString,
frontingURL: frontingURL) {
Logger.warn("Unfronted URL: \(urlString), frontingURL: \(frontingURL)")
}
}
if let censorshipCircumventionPrefix = customCensorshipCircumventionPrefix?.nilIfEmpty {
// When we are domain fronting, we target a fronting host and add a path prefix.
let ccBaseUrl: URL = signalService.domainFrontBaseURL.appendingPathComponent(censorshipCircumventionPrefix)
baseUrl = ccBaseUrl
if !Self.isValidUrlForCensorshipCircumvention(urlString,
frontingURL: ccBaseUrl) {
Logger.warn("Unfronted URL: \(urlString), ccBaseUrl: \(ccBaseUrl)")
}
} else if let frontingURL = self.frontingURL {
// Only requests to Signal services require CC.
// If frontingHost is nil, this instance of OWSURLSession does not perform CC.
if !Self.isValidUrlForCensorshipCircumvention(urlString,
frontingURL: frontingURL) {
Logger.warn("Unfronted URL: \(urlString), frontingURL: \(frontingURL)")
}
}
} else if let customHost = customHost?.nilIfEmpty {
// For some requests (CDS, KBS, remote attestation) we target a "custom host".
guard let customBaseUrl = URL(string: customHost) else {
@ -492,7 +494,7 @@ public class OWSURLSession: NSObject {
return false
}
if let scheme = url.scheme?.nilIfEmpty,
let host = url.host?.nilIfEmpty {
let host = url.host?.nilIfEmpty {
// Absolute URLs which already have the correct prefix are valid.
return (scheme.lowercased() == "https" &&
host.lowercased() == frontingHost)

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2020 Open Whisper Systems. All rights reserved.
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
//
import Foundation
@ -11,79 +11,79 @@ class OWSURLSessionTest: SSKBaseTestSwift {
func test_buildUrl() {
// No base url
XCTAssertEqual(URL(string: "https://e.f.com")!,
OWSURLSession.buildUrl(urlString: "https://e.f.com",
baseUrl: nil)!)
OWSURLSession.joinUrl(urlString: "https://e.f.com",
baseUrl: nil)!)
XCTAssertEqual(URL(string: "https://e.f.com/")!,
OWSURLSession.buildUrl(urlString: "https://e.f.com/",
baseUrl: nil)!)
OWSURLSession.joinUrl(urlString: "https://e.f.com/",
baseUrl: nil)!)
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "https://e.f.com/a/b/c",
baseUrl: nil)!)
OWSURLSession.joinUrl(urlString: "https://e.f.com/a/b/c",
baseUrl: nil)!)
// * baseUrl with just host, no trailing /.
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com")))
OWSURLSession.joinUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com")))
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com")))
OWSURLSession.joinUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com")))
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com")))
// * baseUrl with host & trailing /.
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com/")))
OWSURLSession.joinUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com/")))
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com/")))
OWSURLSession.joinUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com/")))
XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/")))
// * baseUrl with host and path, no trailing /.
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com/x")))
OWSURLSession.joinUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com/x")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com/x")))
OWSURLSession.joinUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com/x")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x")))
// * baseUrl with host and path & trailing /.
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com/x/")))
OWSURLSession.joinUrl(urlString: "a/b/c",
baseUrl: URL(string: "https://e.f.com/x/")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/")))
OWSURLSession.joinUrl(urlString: "/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/")))
// Querystring
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c?i=j&k=l")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l",
baseUrl: URL(string: "https://e.f.com/x/")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l",
baseUrl: URL(string: "https://e.f.com/x/")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c?i=j&k=l")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l",
baseUrl: URL(string: "https://e.f.com/x/?m=m")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l",
baseUrl: URL(string: "https://e.f.com/x/?m=m")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/?m=m")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/?m=m")))
// Fragment
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c#ooo")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c#ooo",
baseUrl: URL(string: "https://e.f.com/x/")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c#ooo",
baseUrl: URL(string: "https://e.f.com/x/")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c#ooo")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c#ooo",
baseUrl: URL(string: "https://e.f.com/x/#ppp")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c#ooo",
baseUrl: URL(string: "https://e.f.com/x/#ppp")))
XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!,
OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/#ppp")))
OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c",
baseUrl: URL(string: "https://e.f.com/x/#ppp")))
}
}