Skip to content

Commit

Permalink
Add global options, tune tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
vzsg committed Feb 2, 2020
1 parent 20186dd commit 1485b4b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion Sources/CurlyClient/CurlyOption+Private.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Vapor

class CurlyOptionStorage: Service {
var options: [CurlyOption] = []
var options: [CurlyOption]

init(options: [CurlyOption] = []) {
self.options = options
}
}

extension Request {
Expand Down
7 changes: 5 additions & 2 deletions Sources/CurlyClient/CurlyProvider.swift
Original file line number Diff line number Diff line change
@@ -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<Void> {
Expand Down
32 changes: 31 additions & 1 deletion Tests/CurlyClientTests/CurlyClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ final class CurlyClientTests: XCTestCase {
}
}

func testSelfSignedCertificate() throws {
func testSelfSignedCertificateOld() throws {
let app = try testApplication()
let client = try app.client()

Expand All @@ -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)
]
}

0 comments on commit 1485b4b

Please sign in to comment.