-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testing the rest api availability.
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
Tests/WPSwiftTests/RepositoryTests/APIInfoRepositoryTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |