Skip to content

Commit 916c0b3

Browse files
committed
Feat: Request 함수 테스트 메소드를 추가하였습니다. (#4)
1 parent 03aea97 commit 916c0b3

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

Client/iOS/TodoList/Network/CardHTTPRequest.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,38 @@ class CardHTTPRequest: SessionConfiguration {
4444
}
4545
}
4646

47+
func doGetRequest(parameter: [String:String]?, completionHandler: @escaping (Data?)->Void) {
48+
guard let url = URL(string: urlString) else {
49+
completionHandler(nil)
50+
return
51+
}
52+
53+
var queryItems = [URLQueryItem]()
54+
if let parameter = parameter {
55+
for param in parameter {
56+
queryItems.append(URLQueryItem(name: param.key, value: param.value))
57+
}
58+
}
59+
var urlComp = URLComponents(url: url, resolvingAgainstBaseURL: false)
60+
urlComp?.queryItems = queryItems
61+
62+
if let url = urlComp?.url {
63+
urlString = url.absoluteString
64+
getCurrentRequestHandler { request in
65+
self.session.dataTask(with: request) { data, response, error in
66+
guard let data = data else {
67+
completionHandler(nil)
68+
return
69+
}
70+
71+
completionHandler(data)
72+
}.resume()
73+
}
74+
} else {
75+
completionHandler(nil)
76+
}
77+
}
78+
4779
/// HTTP POST 요청을 보냅니다.
4880
///
4981
/// - url: Request를 보낼 URL입니다.
@@ -61,4 +93,17 @@ class CardHTTPRequest: SessionConfiguration {
6193
}.resume()
6294
}
6395
}
96+
97+
func doPostRequest(_ paramData: Data? = nil, completionHandler: @escaping (Data?)->Void) {
98+
getCurrentRequestHandler { request in
99+
self.session.uploadTask(with: request, from: paramData) { data, response, error in
100+
guard let data = data else {
101+
completionHandler(nil)
102+
return
103+
}
104+
105+
completionHandler(data)
106+
}.resume()
107+
}
108+
}
64109
}

Client/iOS/TodoList/Network/SessionConfiguration.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import Foundation
1717
class SessionConfiguration: SessionCommonAttributes {
1818

1919
private(set) var session: URLSession
20-
let config = URLSessionConfiguration.default
20+
var config = URLSessionConfiguration.default {
21+
didSet {
22+
resetSession()
23+
}
24+
}
25+
2126
var networkServiceType: NSURLRequest.NetworkServiceType
2227
{
2328
didSet {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// CardAPITests.swift
3+
// TodoListTests
4+
//
5+
// Created by 백상휘 on 2022/04/06.
6+
//
7+
8+
import XCTest
9+
@testable import TodoList
10+
11+
class CardAPITests: XCTestCase {
12+
13+
let httpRequest = CardHTTPRequest(
14+
as: "https://www.naver.com",
15+
using: nil,
16+
in: OperationQueue.main,
17+
type: .default
18+
)
19+
20+
func testConnectionInterface() throws {
21+
22+
// 현재 API가 전달되지 않아서 URLProtocol로 대신합니다.
23+
httpRequest?.config.protocolClasses = [MockURLProtocol.self]
24+
25+
let expectation = XCTestExpectation(description: "Wait")
26+
httpRequest?.doGetRequest(parameter: nil, completionHandler: { data in
27+
expectation.fulfill()
28+
guard let data = data else {
29+
XCTFail("doGetRequest Failed")
30+
return
31+
}
32+
print(data)
33+
})
34+
35+
wait(for: [expectation], timeout: 3.0)
36+
}
37+
}
38+
39+
class MockURLProtocol: URLProtocol {
40+
var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data)) = { request in
41+
let encoder = JSONEncoder()
42+
guard let data = try? encoder.encode(["objectKey":"randomKey"]) else {
43+
XCTFail("URLProtocol handler Failed")
44+
throw NSError()
45+
}
46+
return (HTTPURLResponse(), data)
47+
}
48+
49+
override func startLoading() {
50+
do {
51+
let (response, data) = try requestHandler(request)
52+
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
53+
client?.urlProtocol(self, didLoad: data)
54+
client?.urlProtocolDidFinishLoading(self)
55+
} catch {
56+
client?.urlProtocol(self, didFailWithError: error)
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)