Skip to content

Commit

Permalink
Fixed WebDavProviderTests: the fetchItemMetadata Request, which is no…
Browse files Browse the repository at this point in the history
…w additionally required for the uploadFile, was not mocked.

The additional fetchItemMetadata in uploadFile was moved behind the recover block, because it is intended for the PUT request.
  • Loading branch information
phil1995 committed Oct 13, 2020
1 parent 76e19f6 commit 6cac92e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Sources/CryptomatorCloudAccess/WebDAV/WebDAVProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public class WebDAVProvider: CloudProvider {
default:
return Promise(error)
}
}.then { _, _ in
return self.fetchItemMetadata(at: cloudPath)
}.recover { error -> Promise<CloudItemMetadata> in
}.recover { error -> Promise<(HTTPURLResponse, Data?)> in
switch error {
case URLSessionError.httpError(_, statusCode: 401):
return Promise(CloudProviderError.unauthorized)
Expand All @@ -179,6 +177,8 @@ public class WebDAVProvider: CloudProvider {
default:
return Promise(error)
}
}.then { _, _ in
return self.fetchItemMetadata(at: cloudPath)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class MockURLProtocol: URLProtocol {
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
if let data = data {
client?.urlProtocol(self, didLoad: data)
} else {
print("no data")
}
client?.urlProtocolDidFinishLoading(self)
} catch {
Expand Down
22 changes: 20 additions & 2 deletions Tests/CryptomatorCloudAccessTests/WebDAV/WebDAVProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,24 @@ class WebDAVProviderTests: XCTestCase {
return (propfindResponse, nil)
})

let putData = try getTestData(forResource: "item-metadata", withExtension: "xml")
let putResponse = HTTPURLResponse(url: responseURL, statusCode: 201, httpVersion: "HTTP/1.1", headerFields: nil)!
let putData = Data()
MockURLProtocol.requestHandler.append({ request in
guard let url = request.url, url.path == responseURL.path else {
throw MockURLProtocolError.unexpectedRequest
}
return (putResponse, putData)
})

let propfindResponseAfterUpload = HTTPURLResponse(url: responseURL, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)!
let propfindDataAfterUpload = try getTestData(forResource: "item-metadata", withExtension: "xml")
MockURLProtocol.requestHandler.append({ request in
guard let url = request.url, url.path == responseURL.path else {
throw MockURLProtocolError.unexpectedRequest
}
return (propfindResponseAfterUpload, propfindDataAfterUpload)
})

provider.uploadFile(from: localURL, to: CloudPath("/Documents/About.txt"), replaceExisting: false).then { metadata in
XCTAssertEqual(.zero, self.client.propfindRequests["Documents/About.txt"])
XCTAssertTrue(self.client.putRequests.contains("Documents/About.txt"))
Expand Down Expand Up @@ -362,7 +371,7 @@ class WebDAVProviderTests: XCTestCase {
return (propfindResponse, propfindData)
})

let putData = try getTestData(forResource: "item-metadata", withExtension: "xml")
let putData = Data()
let putResponse = HTTPURLResponse(url: responseURL, statusCode: 201, httpVersion: "HTTP/1.1", headerFields: nil)!
MockURLProtocol.requestHandler.append({ request in
guard let url = request.url, url.path == responseURL.path else {
Expand All @@ -371,6 +380,15 @@ class WebDAVProviderTests: XCTestCase {
return (putResponse, putData)
})

let propfindResponseAfterUpload = HTTPURLResponse(url: responseURL, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)!
let propfindDataAfterUpload = try getTestData(forResource: "item-metadata", withExtension: "xml")
MockURLProtocol.requestHandler.append({ request in
guard let url = request.url, url.path == responseURL.path else {
throw MockURLProtocolError.unexpectedRequest
}
return (propfindResponseAfterUpload, propfindDataAfterUpload)
})

provider.uploadFile(from: localURL, to: CloudPath("/Documents/About.txt"), replaceExisting: true).then { metadata in
XCTAssertEqual(.zero, self.client.propfindRequests["Documents/About.txt"])
XCTAssertTrue(self.client.putRequests.contains("Documents/About.txt"))
Expand Down

0 comments on commit 6cac92e

Please sign in to comment.