From 4606fff2b18a6f87a666f817de53ae53171b52c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulas=CC=A7=20Sancak?= Date: Tue, 25 Jun 2024 18:38:01 +0300 Subject: [PATCH] Add testing the rest api availability. --- .../Extensions/URL+Configuration.swift | 8 ++++ Sources/WPSwift/Models/APIInfo.swift | 15 +++++++ .../Repositories/APIInfoRepository.swift | 17 ++++++++ Tests/WPSwiftTests/Mocks/APIInfo+Mock.swift | 37 ++++++++++++++++++ .../APIInfoRepositoryTests.swift | 39 +++++++++++++++++++ Tests/WPSwiftTests/Resources/APIInfo.json | 6 +++ 6 files changed, 122 insertions(+) create mode 100644 Sources/WPSwift/Models/APIInfo.swift create mode 100644 Sources/WPSwift/Repositories/APIInfoRepository.swift create mode 100644 Tests/WPSwiftTests/Mocks/APIInfo+Mock.swift create mode 100644 Tests/WPSwiftTests/RepositoryTests/APIInfoRepositoryTests.swift create mode 100644 Tests/WPSwiftTests/Resources/APIInfo.json diff --git a/Sources/WPSwift/Extensions/URL+Configuration.swift b/Sources/WPSwift/Extensions/URL+Configuration.swift index b204523..deacf13 100644 --- a/Sources/WPSwift/Extensions/URL+Configuration.swift +++ b/Sources/WPSwift/Extensions/URL+Configuration.swift @@ -17,4 +17,12 @@ extension String { url.append(path: endpoint) return url.absoluteString } + + static func initializeRoute() throws -> String { + let configuration = try WPSwift.configuration + guard let url = URL(string: configuration.route) else { + throw NetworkError.urlMalformed + } + return url.absoluteString + } } diff --git a/Sources/WPSwift/Models/APIInfo.swift b/Sources/WPSwift/Models/APIInfo.swift new file mode 100644 index 0000000..6b49231 --- /dev/null +++ b/Sources/WPSwift/Models/APIInfo.swift @@ -0,0 +1,15 @@ +// +// APIInfo.swift +// +// +// Created by Ulaş Sancak on 25.06.2024. +// + +import Foundation + +public struct APIInfo: Decodable { + public let name: String + public let description: String? + public let url: String? + public let home: String? +} diff --git a/Sources/WPSwift/Repositories/APIInfoRepository.swift b/Sources/WPSwift/Repositories/APIInfoRepository.swift new file mode 100644 index 0000000..fab0f50 --- /dev/null +++ b/Sources/WPSwift/Repositories/APIInfoRepository.swift @@ -0,0 +1,17 @@ +// +// APIInfoRepository.swift +// +// +// Created by Ulaş Sancak on 25.06.2024. +// + +import Foundation +import Resting + +public struct APIInfoRepository { + public func getAPIInfo() async throws -> APIInfo { + let restClient: RestClient = .initialize() + let info: APIInfo = try await restClient.fetch(with: .init(urlString: .initializeRoute(), method: .get)) + return info + } +} diff --git a/Tests/WPSwiftTests/Mocks/APIInfo+Mock.swift b/Tests/WPSwiftTests/Mocks/APIInfo+Mock.swift new file mode 100644 index 0000000..d780b37 --- /dev/null +++ b/Tests/WPSwiftTests/Mocks/APIInfo+Mock.swift @@ -0,0 +1,37 @@ +// +// APIInfo+Mock.swift +// +// +// Created by Ulaş Sancak on 25.06.2023. +// + +import Foundation +@testable import WPSwift + +extension APIInfo { + static var mockData: Data { + get throws { + guard let url = Bundle.module.url(forResource: "APIInfo", withExtension: "json") else { + throw MockError.fileNotFound("APIInfo.json") + } + let data = try Data(contentsOf: url) + return data + } + } + static var mockFromData: APIInfo { + get throws { + let data = try mockData + let decoder = JSONDecoder.initialize() + let info = try decoder.decode(APIInfo.self, from: data) + return info + } + } + static var mock: APIInfo { + .init( + name: "Example", + description: "Example site", + url: "https://www.example.com", + home: "https://www.example.com" + ) + } +} diff --git a/Tests/WPSwiftTests/RepositoryTests/APIInfoRepositoryTests.swift b/Tests/WPSwiftTests/RepositoryTests/APIInfoRepositoryTests.swift new file mode 100644 index 0000000..4bebf0e --- /dev/null +++ b/Tests/WPSwiftTests/RepositoryTests/APIInfoRepositoryTests.swift @@ -0,0 +1,39 @@ +// +// APIInfoRepositoryTests.swift +// +// +// Created by Ulaş Sancak on 25.06.2024. +// + +import XCTest +@testable import WPSwift + +final class APIInfoRepositoryTests: XCTestCase { + + override func setUpWithError() throws { + WPSwift.initialize(route: "https://www.example.com/wp-json", namespace: "wp/v2") + WPSwift.sessionConfiguration.protocolClasses = [MockedURLProtocol.self] + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + WPSwift.sessionConfiguration.protocolClasses = nil + } + + func testCheckingAPI() async throws { + MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in + let response = HTTPURLResponse(url: URL(string: "https://www.example.com/wp-json")!, statusCode: 200, httpVersion: nil, headerFields: nil) + return (response, try APIInfo.mockData) + } + + let repository = APIInfoRepository() + let infoFromData = try await repository.getAPIInfo() + + let info: APIInfo = .mock + + XCTAssertEqual(info.name, infoFromData.name) + XCTAssertEqual(info.description, infoFromData.description) + XCTAssertEqual(info.url, infoFromData.url) + XCTAssertEqual(info.home, infoFromData.home) + } +} diff --git a/Tests/WPSwiftTests/Resources/APIInfo.json b/Tests/WPSwiftTests/Resources/APIInfo.json new file mode 100644 index 0000000..2e64e92 --- /dev/null +++ b/Tests/WPSwiftTests/Resources/APIInfo.json @@ -0,0 +1,6 @@ +{ + "name": "Example", + "description": "Example site", + "url": "https://www.example.com", + "home": "https://www.example.com", +}