Skip to content

Commit 1114b58

Browse files
committed
🧪 Add NetworkProvider Tests
1 parent c824e65 commit 1114b58

File tree

4 files changed

+118
-5
lines changed

4 files changed

+118
-5
lines changed

Core/Tests/CoreTests/CoreTests.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// MockEndPoint.swift
3+
//
4+
//
5+
// Created by 홍승현 on 5/7/24.
6+
//
7+
8+
import Foundation
9+
import NetworkAPIKit
10+
11+
struct MockEndPoint: EndPoint {
12+
var method: HTTPMethod
13+
var path: String
14+
var parameters: HTTPParameter
15+
var headers: [String: String]
16+
17+
init(method: HTTPMethod, path: String, parameters: HTTPParameter = .plain, headers: [String: String] = [:]) {
18+
self.method = method
19+
self.path = path
20+
self.parameters = parameters
21+
self.headers = headers
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// MockURLProtocol.swift
3+
//
4+
//
5+
// Created by 홍승현 on 5/7/24.
6+
//
7+
8+
import Foundation
9+
10+
class MockURLProtocol: URLProtocol {
11+
static var mockData: Data?
12+
static var mockError: Error?
13+
static var mockStatusCode: Int?
14+
15+
override class func canInit(with _: URLRequest) -> Bool {
16+
true
17+
}
18+
19+
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
20+
request
21+
}
22+
23+
override func startLoading() {
24+
if let error = MockURLProtocol.mockError {
25+
client?.urlProtocol(self, didFailWithError: error)
26+
} else {
27+
let response = HTTPURLResponse(url: request.url!, statusCode: MockURLProtocol.mockStatusCode ?? 200, httpVersion: nil, headerFields: nil)
28+
client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed)
29+
client?.urlProtocol(self, didLoad: MockURLProtocol.mockData ?? Data())
30+
}
31+
client?.urlProtocolDidFinishLoading(self)
32+
}
33+
34+
override func stopLoading() {}
35+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@testable import NetworkAPIKit
2+
import XCTest
3+
4+
// MARK: - NetworkProviderTests
5+
6+
final class NetworkProviderTests: XCTestCase {
7+
struct TestModel: Codable, Equatable {
8+
let id: Int
9+
let name: String
10+
}
11+
12+
var networkProvider: NetworkProvider!
13+
var mockURLSession: URLSession!
14+
15+
override func setUp() {
16+
super.setUp()
17+
let configuration = URLSessionConfiguration.ephemeral
18+
configuration.protocolClasses = [MockURLProtocol.self]
19+
mockURLSession = URLSession(configuration: configuration)
20+
networkProvider = NetworkProvider(session: mockURLSession)
21+
}
22+
23+
override func tearDown() {
24+
networkProvider = nil
25+
mockURLSession = nil
26+
super.tearDown()
27+
}
28+
29+
func testRequest_Success() async throws {
30+
// Given
31+
let expectedModel = TestModel(id: 1, name: "Test")
32+
let mockData = try JSONEncoder().encode(expectedModel)
33+
MockURLProtocol.mockData = mockData
34+
MockURLProtocol.mockStatusCode = 200
35+
36+
let endPoint = MockEndPoint(method: .get, path: "/path")
37+
38+
// When
39+
let result: TestModel = try await networkProvider.request(with: endPoint)
40+
41+
// Then
42+
XCTAssertEqual(result, expectedModel)
43+
}
44+
45+
func testRequest_Failure() async throws {
46+
// Given
47+
MockURLProtocol.mockError = NetworkError.failedResponse(statusCode: 400)
48+
49+
let endPoint = MockEndPoint(method: .get, path: "/path")
50+
51+
// When
52+
do {
53+
let _: TestModel = try await networkProvider.request(with: endPoint)
54+
XCTFail("Expected to throw an error")
55+
} catch {
56+
// Then
57+
XCTAssertEqual((error as? NetworkError)?.errorDescription, NetworkError.failedResponse(statusCode: 400).errorDescription)
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)