Skip to content

Commit

Permalink
Add testing the rest api availability.
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed Jun 25, 2024
1 parent bde7ea0 commit 4606fff
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/WPSwift/Extensions/URL+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
15 changes: 15 additions & 0 deletions Sources/WPSwift/Models/APIInfo.swift
Original file line number Diff line number Diff line change
@@ -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?
}
17 changes: 17 additions & 0 deletions Sources/WPSwift/Repositories/APIInfoRepository.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
37 changes: 37 additions & 0 deletions Tests/WPSwiftTests/Mocks/APIInfo+Mock.swift
Original file line number Diff line number Diff line change
@@ -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"
)
}
}
39 changes: 39 additions & 0 deletions Tests/WPSwiftTests/RepositoryTests/APIInfoRepositoryTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions Tests/WPSwiftTests/Resources/APIInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Example",
"description": "Example site",
"url": "https://www.example.com",
"home": "https://www.example.com",
}

0 comments on commit 4606fff

Please sign in to comment.