-
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.
- Loading branch information
Showing
4 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
// | ||
// MockEndPoint.swift | ||
// | ||
// | ||
// Created by 홍승현 on 5/7/24. | ||
// | ||
|
||
import Foundation | ||
import NetworkAPIKit | ||
|
||
struct MockEndPoint: EndPoint { | ||
var method: HTTPMethod | ||
var path: String | ||
var parameters: HTTPParameter | ||
var headers: [String: String] | ||
|
||
init(method: HTTPMethod, path: String, parameters: HTTPParameter = .plain, headers: [String: String] = [:]) { | ||
self.method = method | ||
self.path = path | ||
self.parameters = parameters | ||
self.headers = headers | ||
} | ||
} |
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,35 @@ | ||
// | ||
// MockURLProtocol.swift | ||
// | ||
// | ||
// Created by 홍승현 on 5/7/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class MockURLProtocol: URLProtocol { | ||
static var mockData: Data? | ||
static var mockError: Error? | ||
static var mockStatusCode: Int? | ||
|
||
override class func canInit(with _: URLRequest) -> Bool { | ||
true | ||
} | ||
|
||
override class func canonicalRequest(for request: URLRequest) -> URLRequest { | ||
request | ||
} | ||
|
||
override func startLoading() { | ||
if let error = MockURLProtocol.mockError { | ||
client?.urlProtocol(self, didFailWithError: error) | ||
} else { | ||
let response = HTTPURLResponse(url: request.url!, statusCode: MockURLProtocol.mockStatusCode ?? 200, httpVersion: nil, headerFields: nil) | ||
client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed) | ||
client?.urlProtocol(self, didLoad: MockURLProtocol.mockData ?? Data()) | ||
} | ||
client?.urlProtocolDidFinishLoading(self) | ||
} | ||
|
||
override func stopLoading() {} | ||
} |
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,60 @@ | ||
@testable import NetworkAPIKit | ||
import XCTest | ||
|
||
// MARK: - NetworkProviderTests | ||
|
||
final class NetworkProviderTests: XCTestCase { | ||
struct TestModel: Codable, Equatable { | ||
let id: Int | ||
let name: String | ||
} | ||
|
||
var networkProvider: NetworkProvider! | ||
var mockURLSession: URLSession! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
let configuration = URLSessionConfiguration.ephemeral | ||
configuration.protocolClasses = [MockURLProtocol.self] | ||
mockURLSession = URLSession(configuration: configuration) | ||
networkProvider = NetworkProvider(session: mockURLSession) | ||
} | ||
|
||
override func tearDown() { | ||
networkProvider = nil | ||
mockURLSession = nil | ||
super.tearDown() | ||
} | ||
|
||
func testRequest_Success() async throws { | ||
// Given | ||
let expectedModel = TestModel(id: 1, name: "Test") | ||
let mockData = try JSONEncoder().encode(expectedModel) | ||
MockURLProtocol.mockData = mockData | ||
MockURLProtocol.mockStatusCode = 200 | ||
|
||
let endPoint = MockEndPoint(method: .get, path: "/path") | ||
|
||
// When | ||
let result: TestModel = try await networkProvider.request(with: endPoint) | ||
|
||
// Then | ||
XCTAssertEqual(result, expectedModel) | ||
} | ||
|
||
func testRequest_Failure() async throws { | ||
// Given | ||
MockURLProtocol.mockError = NetworkError.failedResponse(statusCode: 400) | ||
|
||
let endPoint = MockEndPoint(method: .get, path: "/path") | ||
|
||
// When | ||
do { | ||
let _: TestModel = try await networkProvider.request(with: endPoint) | ||
XCTFail("Expected to throw an error") | ||
} catch { | ||
// Then | ||
XCTAssertEqual((error as? NetworkError)?.errorDescription, NetworkError.failedResponse(statusCode: 400).errorDescription) | ||
} | ||
} | ||
} |