Skip to content

Commit

Permalink
Merge pull request #1 from b1narykid/retab
Browse files Browse the repository at this point in the history
retab sources.
  • Loading branch information
kjessup authored Jun 16, 2016
2 parents 338b879 + 3ec5d11 commit 3cbcbe7
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 158 deletions.
14 changes: 7 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import PackageDescription

let package = Package(
name: "PerfectCURL",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/Perfect-libcurl.git", versions: Version(0,0,0)..<Version(10,0,0)),
.Package(url: "https://github.com/PerfectlySoft/Perfect-Thread.git", versions: Version(0,0,0)..<Version(10,0,0))
],
exclude: []
name: "PerfectCURL",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/Perfect-libcurl.git", versions: Version(0,0,0)..<Version(10,0,0)),
.Package(url: "https://github.com/PerfectlySoft/Perfect-Thread.git", versions: Version(0,0,0)..<Version(10,0,0))
],
exclude: []
)

21 changes: 10 additions & 11 deletions Sources/cURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ public class CURL {
}
}

private class ResponseAccumulator {
var header = [UInt8]()
var body = [UInt8]()
}
private class ResponseAccumulator {
var header = [UInt8]()
var body = [UInt8]()
}

/// Perform the CURL request in a non-blocking manner. The closure will be called with the resulting code, header and body data.
public func perform(closure: (Int, [UInt8], [UInt8]) -> ()) {

let accum = ResponseAccumulator()

self.multi = curl_multi_init()
Expand All @@ -177,7 +176,7 @@ public class CURL {
performInner(accumulator: accum, closure: closure)
}

private func performInner(accumulator: ResponseAccumulator, closure: (Int, [UInt8], [UInt8]) -> ()) {
private func performInner(accumulator: ResponseAccumulator, closure: (Int, [UInt8], [UInt8]) -> ()) {
let perf = self.perform()
if let h = perf.2 {
let _ = accumulator.header.append(contentsOf: h)
Expand All @@ -197,7 +196,6 @@ public class CURL {
/// Performs the request, blocking the current thread until it completes.
/// - returns: A tuple consisting of: Int - the result code, [UInt8] - the header bytes if any, [UInt8] - the body bytes if any
public func performFully() -> (Int, [UInt8], [UInt8]) {

let code = curl_easy_perform(self.curl!)
defer {
if self.headerBytes.count > 0 {
Expand Down Expand Up @@ -283,7 +281,7 @@ public class CURL {

/// Returns the String value for the given CURLINFO.
public func getInfo(_ info: CURLINFO) -> (String, CURLcode) {
let i = UnsafeMutablePointer<UnsafePointer<Int8>?>(allocatingCapacity: 1)
let i = UnsafeMutablePointer<UnsafePointer<Int8>?>(allocatingCapacity: 1)
defer { i.deinitialize(count: 1); i.deallocateCapacity(1) }
let code = curl_easy_getinfo_cstr(self.curl!, info, i)
return (code != CURLE_OK ? "" : String(validatingUTF8: i.pointee!)!, code)
Expand All @@ -300,8 +298,8 @@ public class CURL {
}

/// Sets the pointer option value.
/// Note that the pointer value is not copied or otherwise manipulated or saved.
/// It is up to the caller to ensure the pointer value has a lifetime which corresponds to its usage.
/// Note that the pointer value is not copied or otherwise manipulated or saved.
/// It is up to the caller to ensure the pointer value has a lifetime which corresponds to its usage.
public func setOption(_ option: CURLoption, v: UnsafeMutablePointer<Void>) -> CURLcode {
return curl_easy_setopt_void(self.curl!, option, v)
}
Expand Down Expand Up @@ -360,3 +358,4 @@ public class CURL {
self.close()
}
}

2 changes: 1 addition & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import XCTest
@testable import Perfect-CURLTestSuite

XCTMain([
testCase(Perfect-CURLTests.allTests),
testCase(Perfect-CURLTests.allTests),
])
274 changes: 135 additions & 139 deletions Tests/PerfectCURL/PerfectCURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,144 +22,140 @@ import XCTest
import cURL

class PerfectCURLTests: XCTestCase {

func testCURL() {

let url = "https://www.treefrog.ca"
let curl = CURL(url: url)

let _ = curl.setOption(CURLOPT_SSL_VERIFYPEER, int: 0)

XCTAssert(curl.url == url)

var header = [UInt8]()
var body = [UInt8]()

var perf = curl.perform()
while perf.0 {
if let h = perf.2 {
header.append(contentsOf: h)
}
if let b = perf.3 {
body.append(contentsOf: b)
}
perf = curl.perform()
}
if let h = perf.2 {
header.append(contentsOf: h)
}
if let b = perf.3 {
body.append(contentsOf: b)
}
let perf1 = perf.1
XCTAssert(perf1 == 0, "\(perf)")

let response = curl.responseCode
XCTAssert(response == 200, "\(response)")


XCTAssert(header.count > 0)
XCTAssert(body.count > 0)
}

func testCURLAsync() {

let url = "https://www.treefrog.ca"
let curl = CURL(url: url)

let _ = curl.setOption(CURLOPT_SSL_VERIFYPEER, int: 0)

XCTAssert(curl.url == url)

let clientExpectation = self.expectation(withDescription: "client")

curl.perform {
code, header, body in

XCTAssert(0 == code, "Request error code \(code)")

let response = curl.responseCode
XCTAssert(response == 200, "\(response)")
XCTAssert(header.count > 0)
XCTAssert(body.count > 0)

clientExpectation.fulfill()
}

self.waitForExpectations(withTimeout: 10000) {
_ in

}
}

func testCURLHeader() {
let url = "https://httpbin.org/headers"
let header = ("Accept", "application/json")

let curl = CURL(url: url)
let _ = curl.setOption(CURLOPT_HTTPHEADER, s: "\(header.0): \(header.1)" )
let response = curl.performFully()
XCTAssert(response.0 == 0)

// let body = UTF8Encoding.encode(bytes: response.2)
// do {
// guard
// let jsonMap = try body.jsonDecode() as? [String: Any],
// let headers = jsonMap["headers"] as? [String: Any],
// let accept = headers[header.0] as? String
// else {
// XCTAssert(false)
// return
// }
// XCTAssertEqual(accept, header.1)
// } catch let e {
// XCTAssert(false, "Exception: \(e)")
// }
}

func testCURLPost() {
let url = "https://httpbin.org/post"
let postParamString = "key1=value1&key2=value2"
let byteArray = [UInt8](postParamString.utf8)

do {

let curl = CURL(url: url)

let _ = curl.setOption(CURLOPT_POST, int: 1)
let _ = curl.setOption(CURLOPT_POSTFIELDS, v: UnsafeMutablePointer<UInt8>(byteArray))
let _ = curl.setOption(CURLOPT_POSTFIELDSIZE, int: byteArray.count)

let response = curl.performFully()
XCTAssert(response.0 == 0)

// let body = UTF8Encoding.encode(bytes: response.2)
// do {
// guard
// let jsonMap = try body.jsonDecode() as? [String: Any],
// let form = jsonMap["form"] as? [String: Any],
// let value1 = form["key1"] as? String,
// let value2 = form["key2"] as? String
// else {
// XCTAssert(false)
// return
// }
// XCTAssertEqual(value1, "value1")
// XCTAssertEqual(value2, "value2")
// } catch let e {
// XCTAssert(false, "Exception: \(e)")
// }
}
}

static var allTests : [(String, (PerfectCURLTests) -> () throws -> Void)] {
return [
("testCURLPost", testCURLPost),
("testCURLHeader", testCURLHeader),
("testCURLAsync", testCURLAsync),
("testCURL", testCURL)
]
}
}

func testCURL() {
let url = "https://www.treefrog.ca"
let curl = CURL(url: url)

let _ = curl.setOption(CURLOPT_SSL_VERIFYPEER, int: 0)

XCTAssert(curl.url == url)

var header = [UInt8]()
var body = [UInt8]()

var perf = curl.perform()
while perf.0 {
if let h = perf.2 {
header.append(contentsOf: h)
}
if let b = perf.3 {
body.append(contentsOf: b)
}
perf = curl.perform()
}
if let h = perf.2 {
header.append(contentsOf: h)
}
if let b = perf.3 {
body.append(contentsOf: b)
}
let perf1 = perf.1
XCTAssert(perf1 == 0, "\(perf)")

let response = curl.responseCode
XCTAssert(response == 200, "\(response)")

XCTAssert(header.count > 0)
XCTAssert(body.count > 0)
}

func testCURLAsync() {
let url = "https://www.treefrog.ca"
let curl = CURL(url: url)

let _ = curl.setOption(CURLOPT_SSL_VERIFYPEER, int: 0)

XCTAssert(curl.url == url)

let clientExpectation = self.expectation(withDescription: "client")

curl.perform {
code, header, body in

XCTAssert(0 == code, "Request error code \(code)")

let response = curl.responseCode
XCTAssert(response == 200, "\(response)")
XCTAssert(header.count > 0)
XCTAssert(body.count > 0)

clientExpectation.fulfill()
}

self.waitForExpectations(withTimeout: 10000) {
_ in

}
}

func testCURLHeader() {
let url = "https://httpbin.org/headers"
let header = ("Accept", "application/json")

let curl = CURL(url: url)
let _ = curl.setOption(CURLOPT_HTTPHEADER, s: "\(header.0): \(header.1)" )
let response = curl.performFully()
XCTAssert(response.0 == 0)

// let body = UTF8Encoding.encode(bytes: response.2)
// do {
// guard
// let jsonMap = try body.jsonDecode() as? [String: Any],
// let headers = jsonMap["headers"] as? [String: Any],
// let accept = headers[header.0] as? String
// else {
// XCTAssert(false)
// return
// }
// XCTAssertEqual(accept, header.1)
// } catch let e {
// XCTAssert(false, "Exception: \(e)")
// }
}

func testCURLPost() {
let url = "https://httpbin.org/post"
let postParamString = "key1=value1&key2=value2"
let byteArray = [UInt8](postParamString.utf8)

do {

let curl = CURL(url: url)

let _ = curl.setOption(CURLOPT_POST, int: 1)
let _ = curl.setOption(CURLOPT_POSTFIELDS, v: UnsafeMutablePointer<UInt8>(byteArray))
let _ = curl.setOption(CURLOPT_POSTFIELDSIZE, int: byteArray.count)

let response = curl.performFully()
XCTAssert(response.0 == 0)

// let body = UTF8Encoding.encode(bytes: response.2)
// do {
// guard
// let jsonMap = try body.jsonDecode() as? [String: Any],
// let form = jsonMap["form"] as? [String: Any],
// let value1 = form["key1"] as? String,
// let value2 = form["key2"] as? String
// else {
// XCTAssert(false)
// return
// }
// XCTAssertEqual(value1, "value1")
// XCTAssertEqual(value2, "value2")
// } catch let e {
// XCTAssert(false, "Exception: \(e)")
// }
}
}

static var allTests : [(String, (PerfectCURLTests) -> () throws -> Void)] {
return [
("testCURLPost", testCURLPost),
("testCURLHeader", testCURLHeader),
("testCURLAsync", testCURLAsync),
("testCURL", testCURL)
]
}
}

0 comments on commit 3cbcbe7

Please sign in to comment.