Skip to content

Commit

Permalink
[PAS-418] Fix issue with server unreachable error url
Browse files Browse the repository at this point in the history
  • Loading branch information
sq9rt-mq committed Oct 15, 2021
1 parent 5adb7be commit f96eda2
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ extension ServerNotReachableController: UIController {
cancellables: Cancellables
) -> Self {
var urlComponents: URLComponents? = context.flatMap { URLComponents(url: $0, resolvingAgainstBaseURL: true) }
urlComponents?.user = nil
urlComponents?.path = ""
urlComponents?.query = nil
urlComponents?.fragment = nil

return Self(
serverURL: (urlComponents?.string).map(URLString.init(rawValue:))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,74 @@ final class NetworkRequestTests: XCTestCase {
XCTAssertEqual(completionError?.identifier, .httpError)
}

func test_request_withHTTPErrorCannotConnect_failsWithServerNotReachableError() {
let url: URL = .init(string: "https://passbolt.com")!
networking.execute = { _, _ -> AnyPublisher<HTTPResponse, HTTPError> in
Fail<HTTPResponse, HTTPError>(error: .cannotConnect(url))
.eraseToAnyPublisher()
}

request = prepareRequest()
var completionError: TheError? = nil

request
.make(using: .sample)
.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
XCTFail("Unexpected behaviour")

case let .failure(error):
completionError = error
}
},
receiveValue: { _ in
XCTFail("Unexpected behaviour")
}
)
.store(in: cancellables)

sessionSubject.send(NetworkSessionVariable(domain: "https://passbolt.com"))

XCTAssertEqual(completionError?.identifier, .serverNotReachable)
XCTAssertEqual(completionError?.url, url)
}

func test_request_withHTTPErrorTimeout_failsWithServerNotReachableError() {
let url: URL = .init(string: "https://passbolt.com")!
networking.execute = { _, _ -> AnyPublisher<HTTPResponse, HTTPError> in
Fail<HTTPResponse, HTTPError>(error: .timeout(url))
.eraseToAnyPublisher()
}

request = prepareRequest()
var completionError: TheError? = nil

request
.make(using: .sample)
.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
XCTFail("Unexpected behaviour")

case let .failure(error):
completionError = error
}
},
receiveValue: { _ in
XCTFail("Unexpected behaviour")
}
)
.store(in: cancellables)

sessionSubject.send(NetworkSessionVariable(domain: "https://passbolt.com"))

XCTAssertEqual(completionError?.identifier, .serverNotReachable)
XCTAssertEqual(completionError?.url, url)
}

func test_requestBodyAndResponseBody_withBodyMirroring_areEqual() {
networking.execute = { request, _ -> AnyPublisher<HTTPResponse, HTTPError> in
Just(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Passbolt - Open source password manager for teams
// Copyright (c) 2021 Passbolt SA
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
// Public License (AGPL) as published by the Free Software Foundation version 3.
//
// The name "Passbolt" is a registered trademark of Passbolt SA, and Passbolt SA hereby declines to grant a trademark
// license to "Passbolt" pursuant to the GNU Affero General Public License version 3 Section 7(e), without a separate
// agreement with Passbolt SA.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with this program. If not,
// see GNU Affero General Public License v3 (http://www.gnu.org/licenses/agpl-3.0.html).
//
// @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
// @license https://opensource.org/licenses/AGPL-3.0 AGPL License
// @link https://www.passbolt.com Passbolt (tm)
// @since v1.0
//

import Features
import TestExtensions
import UIComponents
import XCTest

@testable import SharedUIComponents

// swift-format-ignore: AlwaysUseLowerCamelCase, NeverUseImplicitlyUnwrappedOptionals
final class ServerNotReachableControllerTests: TestCase {

func test_serverURL_hasCorrectValue_whenProvided_withValidURL() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: "https://passbolt.com:443")
)

XCTAssertEqual(controller.serverURL, "https://passbolt.com:443")
}

func test_serverURL_hasCorrectValue_whenProvided_withValidURL_containingAllUnnecessaryComponents() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: "https://user@passbolt.com:443/path?query=1#fragment")
)

XCTAssertEqual(controller.serverURL, "https://passbolt.com:443")
}

func test_serverURL_doesNotContainUser_whenProvided_withValidURL_containingUser() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: "https://user@passbolt.com:443")
)

XCTAssertEqual(controller.serverURL, "https://passbolt.com:443")
}

func test_serverURL_doesNotContainPath_whenProvided_withValidURL_containingPath() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: "https://passbolt.com:443/path")
)

XCTAssertEqual(controller.serverURL, "https://passbolt.com:443")
}

func test_serverURL_doesNotContainQuery_whenProvided_withValidURL_containingQuery() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: "https://passbolt.com:443?query=1")
)

XCTAssertEqual(controller.serverURL, "https://passbolt.com:443")
}

func test_serverURL_doesNotContainFragment_whenProvided_withValidURL_containingQuery() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: "https://passbolt.com:443#fragment")
)

XCTAssertEqual(controller.serverURL, "https://passbolt.com:443")
}

func test_serverURL_isEmpty_whenProvided_withInvalidURL() {
let controller: ServerNotReachableController = testInstance(
context: .init(string: ":)//passboltcom/?fragment")
)

XCTAssertTrue(controller.serverURL?.rawValue.isEmpty ?? false)
}
}

0 comments on commit f96eda2

Please sign in to comment.