Skip to content

Commit

Permalink
🧪 Add NetworkProvider Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteHyun committed May 7, 2024
1 parent c824e65 commit 1114b58
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 5 deletions.
5 changes: 0 additions & 5 deletions Core/Tests/CoreTests/CoreTests.swift

This file was deleted.

23 changes: 23 additions & 0 deletions Core/Tests/CoreTests/Mock/MockEndPoint.swift
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
}
}
35 changes: 35 additions & 0 deletions Core/Tests/CoreTests/Mock/MockURLProtocol.swift
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() {}
}
60 changes: 60 additions & 0 deletions Core/Tests/CoreTests/NetworkProviderTests.swift
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)
}
}
}

0 comments on commit 1114b58

Please sign in to comment.