Skip to content

Commit

Permalink
Allow PATCH requests to send data (#224)
Browse files Browse the repository at this point in the history
* Fix PATCH requests to allow sending data

* Add PATCH tests

* Fix typo
  • Loading branch information
tunniclm authored and rolivieri committed Oct 17, 2017
1 parent a371246 commit cb09ae6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ public class ClientRequest {
curlHelperSetOptInt(handle!, CURLOPT_INFILESIZE, count)
case "HEAD":
curlHelperSetOptBool(handle!, CURLOPT_NOBODY, CURL_TRUE)
case "PATCH":
curlHelperSetOptString(handle!, CURLOPT_CUSTOMREQUEST, methodUpperCase)
curlHelperSetOptBool(handle!, CURLOPT_UPLOAD, CURL_TRUE)
curlHelperSetOptInt(handle!, CURLOPT_INFILESIZE, count)
default:
curlHelperSetOptString(handle!, CURLOPT_CUSTOMREQUEST, methodUpperCase)
}
Expand Down
50 changes: 49 additions & 1 deletion Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ClientE2ETests: KituraNetTest {
("testKeepAlive", testKeepAlive),
("testPostRequests", testPostRequests),
("testPutRequests", testPutRequests),
("testPatchRequests", testPatchRequests),
("testSimpleHTTPClient", testSimpleHTTPClient),
("testUrlURL", testUrlURL)
]
Expand Down Expand Up @@ -161,7 +162,7 @@ class ClientE2ETests: KituraNetTest {
}
})
}

func testPutRequests() {
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("put", path: "/puttest", callback: {response in
Expand Down Expand Up @@ -207,6 +208,53 @@ class ClientE2ETests: KituraNetTest {
request.write(from: "A few characters")
}
})
}

func testPatchRequests() {
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("patch", path: "/patchtest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
var data = Data()
let count = try response?.readAllData(into: &data)
XCTAssertEqual(count, 12, "Result should have been 12 bytes, was \(String(describing: count)) bytes")
let patchValue = String(data: data as Data, encoding: .utf8)
if let patchValue = patchValue {
XCTAssertEqual(patchValue, "Read 0 bytes")
}
else {
XCTFail("patchValue's value wasn't an UTF8 string")
}
}
catch {
XCTFail("Failed reading the body of the response")
}
expectation.fulfill()
})
},
{ expectation in
self.performRequest("patch", path: "/patchtest", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
var data = Data()
let count = try response?.readAllData(into: &data)
XCTAssertEqual(count, 13, "Result should have been 13 bytes, was \(String(describing: count)) bytes")
let patchValue = String(data: data as Data, encoding: .utf8)
if let patchValue = patchValue {
XCTAssertEqual(patchValue, "Read 16 bytes")
}
else {
XCTFail("patchValue's value wasn't an UTF8 string")
}
}
catch {
XCTFail("Failed reading the body of the response")
}
expectation.fulfill()
}) {request in
request.write(from: "A few characters")
}
})
}

func testErrorRequests() {
Expand Down

0 comments on commit cb09ae6

Please sign in to comment.