Skip to content

Commit

Permalink
Merge pull request #31 from Ast3r10n/release/0.2
Browse files Browse the repository at this point in the history
Release 0.2
  • Loading branch information
Ast3r10n authored Jul 2, 2021
2 parents 74dd6e2 + 9d5125c commit 5a24c85
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 133 deletions.
22 changes: 5 additions & 17 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
name: Swift
name: Test

on: [pull_request]

jobs:
build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: swift build -v
test:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: swift test -v --enable-code-coverage

codecov:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Generate coverage report
run: xcodebuild -scheme SwiftQuests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11 Pro' -enableCodeCoverage YES build test
run: |
swift test --enable-code-coverage
xcrun llvm-cov export -format="lcov" .build/debug/SwiftQuestsPackageTests.xctest/Contents/MacOS/SwiftQuestsPackageTests -instr-profile .build/debug/codecov/default.profdata > info.lcov
- name: Codecov
uses: codecov/[email protected]
with:
token: 44439f4d-d012-4000-a0c1-28647d07b545
# file:
30 changes: 15 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// swift-tools-version:5.1
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "SwiftQuests",
products: [
.library(
name: "SwiftQuests",
targets: ["SwiftQuests"]),
],
targets: [
.target(
name: "SwiftQuests",
dependencies: []),
.testTarget(
name: "SwiftQuestsTests",
dependencies: ["SwiftQuests"]),
]
name: "SwiftQuests",
products: [
.library(
name: "SwiftQuests",
targets: ["SwiftQuests"]),
],
targets: [
.target(
name: "SwiftQuests",
dependencies: []),
.testTarget(
name: "SwiftQuestsTests",
dependencies: ["SwiftQuests"]),
]
)
76 changes: 76 additions & 0 deletions Sources/SwiftQuests/NetworkError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// NetworkError.swift
//
//
// Created by Andrea Sacerdoti on 17/12/20.
//

import Foundation

/// A specialized error for network purposes.
public enum NetworkError: LocalizedError {

/// A generic network error.
case generic

/// An error identifying the 400 HTTP status code.
case badRequest

/// An error identifying the 401 HTTP status code.
case unauthorized

/// An error identifying the 403 HTTP status code.
case forbidden

/// An error identifying the 404 HTTP status code.
case notFound

/// An error identifying the 500 HTTP status code.
case internalServerError

/// An error including a description provided by the backend.
case withDescription(String)

public var errorDescription: String? {
switch self {
case .generic:
return NSLocalizedString("Generic error.", comment: "")
case .badRequest:
return NSLocalizedString("Bad request.", comment: "")
case .unauthorized:
return NSLocalizedString("Unauthorized.", comment: "")
case .forbidden:
return NSLocalizedString("Access denied.", comment: "")
case .notFound:
return NSLocalizedString("Resource not found.", comment: "")
case .internalServerError:
return NSLocalizedString("Internal server error.", comment: "")
case .withDescription(let description):
return NSLocalizedString(description, comment: "")
}
}

/// Returns a NetworkError identifying a given HTTP error.
/// - Parameter statusCode: The given HTTP status code.
/// - Returns: A NetworkError identified by the given status code.
public static func identifying(statusCode: Int) -> NetworkError {
switch statusCode {
case 400:
return NetworkError.badRequest
case 401:
return NetworkError.unauthorized
case 403:
return NetworkError.forbidden
case 404:
return NetworkError.notFound
case 500:
return NetworkError.internalServerError
default:
return NetworkError.generic
}
}
}

// MARK: -
extension NetworkError: Equatable {
}
12 changes: 9 additions & 3 deletions Sources/SwiftQuests/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ open class Request: AbstractRequest {
/// - Parameters:
/// - completionHandler: An handler called upon completion.
/// - result: The response result.
/// - error: The task error.
/// - Throws: An error if either the `urlRequest` property was not properly initialised, or the `completionHandler`
/// throws.
open func perform(_ completionHandler: @escaping (_ result: Result<Response, Error>) throws -> Void) {
Expand All @@ -134,6 +133,13 @@ open class Request: AbstractRequest {
return
}

if let statusCode = (response as? HTTPURLResponse)?.statusCode,
!(200..<300 ~= statusCode) {

try? completionHandler(.failure(NetworkError.identifying(statusCode: statusCode)))
return
}

try? completionHandler(.success((data, response)))
}

Expand All @@ -153,15 +159,15 @@ open class Request: AbstractRequest {
/// - object: An object type to decode from the response data.
/// - completionHandler: An handler called upon completion.
/// - result: The response result.
/// - error: The task error.
open func perform<T: Decodable>(decoding object: T.Type,
_ completionHandler: @escaping (
_ result: Result<(T, URLResponse?), Error>) throws -> Void) {

perform { result in
switch result {
case .success(let response):
guard let data = response.data else {
guard let data = response.data,
!data.isEmpty else {
try completionHandler(.failure(NSError(domain: "",
code: 0,
userInfo: [NSLocalizedDescriptionKey: "Data returned nil."])))
Expand Down
7 changes: 0 additions & 7 deletions Tests/LinuxMain.swift

This file was deleted.

42 changes: 42 additions & 0 deletions Tests/SwiftQuestsTests/Mocks/URLProtocolMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// URLProtocolMock.swift
//
//
// Created by Andrea Sacerdoti on 05/02/2020.
//

import Foundation

class URLProtocolMock: URLProtocol {
static var response: (data: Data?, response: URLResponse?, error: Error?)?

override class func canInit(with request: URLRequest) -> Bool {
true
}

override class func canonicalRequest(for request: URLRequest) -> URLRequest {
request
}

override func startLoading() {
if let (data, response, error) = URLProtocolMock.response {

if let response = response {
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
}

if let data = data {
client?.urlProtocol(self, didLoad: data)
}

if let error = error {
client?.urlProtocol(self, didFailWithError: error)
}
}

client?.urlProtocolDidFinishLoading(self)
}

override func stopLoading() {
}
}
17 changes: 0 additions & 17 deletions Tests/SwiftQuestsTests/Mocks/URLSessionCodableMock.swift

This file was deleted.

20 changes: 0 additions & 20 deletions Tests/SwiftQuestsTests/Mocks/URLSessionDataTaskMock.swift

This file was deleted.

21 changes: 0 additions & 21 deletions Tests/SwiftQuestsTests/Mocks/URLSessionMock.swift

This file was deleted.

31 changes: 31 additions & 0 deletions Tests/SwiftQuestsTests/NetworkErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// NetworkErrorTests.swift
//
//
// Created by Andrea Sacerdoti on 02/07/21.
//

import XCTest
@testable import SwiftQuests

final class NetworkErrorTests: XCTestCase {

func testErrorDescription() {
XCTAssertNotNil(NetworkError.generic.errorDescription)
XCTAssertNotNil(NetworkError.badRequest.errorDescription)
XCTAssertNotNil(NetworkError.unauthorized.errorDescription)
XCTAssertNotNil(NetworkError.forbidden.errorDescription)
XCTAssertNotNil(NetworkError.notFound.errorDescription)
XCTAssertNotNil(NetworkError.internalServerError.errorDescription)
XCTAssertEqual(NetworkError.withDescription("Test Description").localizedDescription, "Test Description")
}

func testIdentifying() {
XCTAssertEqual(NetworkError.identifying(statusCode: 400), .badRequest)
XCTAssertEqual(NetworkError.identifying(statusCode: 401), .unauthorized)
XCTAssertEqual(NetworkError.identifying(statusCode: 403), .forbidden)
XCTAssertEqual(NetworkError.identifying(statusCode: 404), .notFound)
XCTAssertEqual(NetworkError.identifying(statusCode: 405), .generic)
XCTAssertEqual(NetworkError.identifying(statusCode: 500), .internalServerError)
}
}
Loading

0 comments on commit 5a24c85

Please sign in to comment.