Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing ISS-543 by appending a new body json interface. #11

Merged
merged 2 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ public extension CURLResponse {
/// Get the response body decoded from JSON into a [String:Any] dictionary.
/// Invalid/non-JSON body data will result in an empty dictionary being returned.
public var bodyJSON: [String:Any]
/// Get the response body decoded from JSON into a decodable structure
/// Invalid/non-JSON body data will throw errors.
public func bodyJSON<T: Decodable>(_ type: T.Type) throws -> T
}
```

Expand Down
4 changes: 4 additions & 0 deletions Sources/CURLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cURL
import PerfectHTTP
import PerfectCrypto
import PerfectLib
import Foundation

enum ResponseReadState {
case status, headers, body
Expand Down Expand Up @@ -300,6 +301,9 @@ public extension CURLResponse {
/// Get the response body decoded from JSON into a [String:Any] dictionary.
/// Invalid/non-JSON body data will result in an empty dictionary being returned.
public var bodyJSON: [String:Any] { do { return try bodyString.jsonDecode() as? [String:Any] ?? [:] } catch { return [:] } }
/// Get the response body decoded from JSON into a decodable structure
/// Invalid/non-JSON body data will throw errors.
public func bodyJSON<T: Decodable>(_ type: T.Type) throws -> T { return try JSONDecoder().decode(type, from: Data(bytes: bodyBytes)) }
}


Expand Down
60 changes: 18 additions & 42 deletions Tests/PerfectCURLTests/PerfectCURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,22 @@ class PerfectCURLTests: XCTestCase {
.addHeader(custom2, ""),
.removeHeader(accept),
.replaceHeader(custom, customValue))


struct Headers: Codable {
var connection = ""
var host = ""
var extra = ""
var extra2 = ""
private enum CodingKeys: String, CodingKey {
case connection = "Connection"
case host = "Host"
case extra = "X-Extra"
case extra2 = "X-Extra-2"
}
}
struct HeaderJSON: Codable {
var headers = Headers()
}
do {
let response = try request.perform()
let json = response.bodyJSON
Expand All @@ -133,6 +148,8 @@ class PerfectCURLTests: XCTestCase {
XCTAssertNil(headers[accept.standardName])
XCTAssertEqual(customValue, resCustom)
XCTAssertEqual("", resCustom2)
let headerJSON = try response.bodyJSON(HeaderJSON.self)
print(headerJSON)
} catch {
XCTAssert(false, "\(error)")
}
Expand Down Expand Up @@ -313,47 +330,6 @@ class PerfectCURLTests: XCTestCase {
}
self.waitForExpectations(timeout: 10000)
}

// func testSMTP () {
// var timestamp = time(nil)
// let now = String(cString: asctime(localtime(&timestamp))!)
// let sender = "[email protected]"
// let recipient = sender
// let u = UnsafeMutablePointer<UInt8>.allocate(capacity: MemoryLayout<uuid_t>.size)
// uuid_generate_random(u)
// let unu = UnsafeMutablePointer<Int8>.allocate(capacity: 37)
// uuid_unparse_lower(u, unu)
// let uuid = String(validatingUTF8: unu)!
// u.deallocate(capacity: MemoryLayout<uuid_t>.size)
// unu.deallocate(capacity: 37)
//
// let content = "Date: \(now)To: \(recipient)\r\nFrom: \(sender)\r\nCc:\r\nBcc:\r\n" +
// "Message-ID: <\(uuid)@perfect.org>\r\n" +
// "Subject: Hello Perfect-CURL\r\n\r\nSMTP test \(now)\r\n\r\n"
// let curl = CURL(url: "smtp://smtp.gmx.com")
// let _ = curl.setOption(CURLOPT_USERNAME, s: sender)
// let _ = curl.setOption(CURLOPT_PASSWORD, s: "abcd1234")
// let _ = curl.setOption(CURLOPT_MAIL_FROM, s: sender)
// let _ = curl.setOption(CURLOPT_MAIL_RCPT, s: recipient)
// let _ = curl.setOption(CURLOPT_VERBOSE, int: 1)
// let _ = curl.setOption(CURLOPT_UPLOAD, int: 1)
// let _ = curl.setOption(CURLOPT_INFILESIZE, int: content.utf8.count)
// var p:[Int32] = [-1, -1]
// let result = pipe(&p)
// XCTAssertEqual(result, 0)
// let fi = fdopen(p[0], "rb")
// let fo = fdopen(p[1], "wb")
// let w = fwrite(content, 1, content.utf8.count, fo)
// fclose(fo)
// XCTAssertEqual(w, content.utf8.count)
// let _ = curl.setOption(CURLOPT_READDATA, v: fi!)
// let r = curl.performFully()
// print(r.0)
// print(String(cString:r.1))
// print(String(cString:r.2))
// fclose(fi)
// XCTAssertEqual(r.0, 0)
// }

static var allTests : [(String, (PerfectCURLTests) -> () throws -> Void)] {
return [
Expand Down