diff --git a/README.md b/README.md index dd91f50..fb0d70b 100644 --- a/README.md +++ b/README.md @@ -60,10 +60,33 @@ Your Vapor app now uses curl directly instead of URLSession. From 0.3.0, Curly exposes a few useful options from cURL that are otherwise not available via the Client interface, or even URLSession. To use them, you _must_ register Curly via the `CurlyProvider` as seen in step 2. With that in place, you can call `Request.addCurlyOption` in either the `beforeSend` closure when using the convenience functions of Client, or on the Request instance itself when using `Client.send()` and a self-built Request object. -See the tests for examples of both methods. +The two approaches are functionally equivalent. + +```swift +try client.get("https://self-signed.badssl.com/", beforeSend: { req in + req.addCurlyOption(.sslVerifyPeer(false)) +}) +``` + +```swift +var http = HTTPRequest(method: .GET, url: "https://self-signed.badssl.com/") +http.headers.replaceOrAdd(name: "X-Test-Header", value: "Foo") + +let req = Request(http: http, using: app) +req.addCurlyOption(.sslVerifyPeer(false)) + +try client.send(req) +``` > **Warning**: Calling `Request.addCurlyOption` without the Provider will result in a fatal error in debug builds, and a warning print in release builds. +Starting with 0.7.0, options can be applied to all requests made via Curly using the new `globalOptions` optional parameter of CurlyProvider. + +```swift +try services.register(CurlyProvider(globalOptions: [ + .sslCAFilePath("my-selfsigned-certs.crt") +])) +``` #### Available options diff --git a/Sources/CurlyClient/CurlyOption+Private.swift b/Sources/CurlyClient/CurlyOption+Private.swift index ac4858a..0f1a08c 100644 --- a/Sources/CurlyClient/CurlyOption+Private.swift +++ b/Sources/CurlyClient/CurlyOption+Private.swift @@ -1,7 +1,11 @@ import Vapor class CurlyOptionStorage: Service { - var options: [CurlyOption] = [] + var options: [CurlyOption] + + init(options: [CurlyOption] = []) { + self.options = options + } } extension Request { diff --git a/Sources/CurlyClient/CurlyProvider.swift b/Sources/CurlyClient/CurlyProvider.swift index 3ed60b8..f24030a 100644 --- a/Sources/CurlyClient/CurlyProvider.swift +++ b/Sources/CurlyClient/CurlyProvider.swift @@ -1,12 +1,15 @@ import Vapor public class CurlyProvider: Provider { - public init() { + private let globalOptions: [CurlyOption] + + public init(globalOptions: [CurlyOption] = []) { + self.globalOptions = globalOptions } public func register(_ services: inout Services) throws { services.register(CurlyClient.self) - services.register { _ in CurlyOptionStorage() } + services.register { [globalOptions] _ in CurlyOptionStorage(options: globalOptions) } } public func didBoot(_ container: Container) throws -> EventLoopFuture { diff --git a/Tests/CurlyClientTests/CurlyClientTests.swift b/Tests/CurlyClientTests/CurlyClientTests.swift index 8209e17..49a1dad 100644 --- a/Tests/CurlyClientTests/CurlyClientTests.swift +++ b/Tests/CurlyClientTests/CurlyClientTests.swift @@ -105,7 +105,7 @@ final class CurlyClientTests: XCTestCase { } } - func testSelfSignedCertificate() throws { + func testSelfSignedCertificateOld() throws { let app = try testApplication() let client = try app.client() @@ -119,12 +119,42 @@ final class CurlyClientTests: XCTestCase { XCTAssertEqual(200, insecure.http.status.code) } + func testSelfSignedCertificate() throws { + let app = try testApplication() + let client = try app.client() + + XCTAssertThrowsError(try client.get("https://self-signed.badssl.com/").wait()) + + let insecure = try client.get("https://self-signed.badssl.com/", beforeSend: { req in + req.addCurlyOption(.sslVerifyPeer(false)) + }).wait() + + XCTAssertEqual(200, insecure.http.status.code) + } + + func testGlobalOptions() throws { + var services = Services.default() + var config = Config.default() + + try services.register(CurlyProvider(globalOptions: [.sslVerifyPeer(false)])) + config.prefer(CurlyClient.self, for: Client.self) + + let app = try Application(config: config, services: services) + let client = try app.client() + + let insecure = try client.get("https://self-signed.badssl.com/").wait() + + XCTAssertEqual(200, insecure.http.status.code) + } + static var allTests = [ ("testHttpBinPost", testHttpBinPost), ("testHttpBinGet", testHttpBinGet), ("testConvenience", testConvenience), ("testCookieJar", testCookieJar), ("testTimeoutError", testTimeoutError), + ("testSelfSignedCertificateOld", testSelfSignedCertificateOld), ("testSelfSignedCertificate", testSelfSignedCertificate), + ("testGlobalOptions", testGlobalOptions) ] }