Skip to content

Commit 86a09d1

Browse files
committed
[add] documentation and unit testing
1 parent b69e0fa commit 86a09d1

File tree

2 files changed

+131
-5
lines changed

2 files changed

+131
-5
lines changed

Sources/MicrocmsSDK/MicrocmsSDK.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ public struct MicrocmsClient {
8989
return task
9090
}
9191

92+
/// make write request for microCMS .
93+
///
94+
/// - Parameters:
95+
/// - method: HTTP method.
96+
/// - endpoint: endpoint of contents.
97+
/// - contentId: contentId. It's needed if you want to fetch a element of list.
98+
/// - params: some parameters for body.
99+
/// - Returns: URLRequest made with given parameters.
92100
public func makeWriteRequest(
93101
method: HTTPMethod,
94102
endpoint: String,
@@ -162,6 +170,13 @@ public struct MicrocmsClient {
162170
return task
163171
}
164172

173+
/// post content for microCMS .
174+
///
175+
/// - Parameters:
176+
/// - endpoint: endpoint of contents.
177+
/// - params: some parameters for body.
178+
/// - Returns: URLSessionTask you requested. Basically, you don't need to use it, but it helps you to manage state or cancel request.
179+
@discardableResult
165180
public func post(
166181
endpoint: String,
167182
params: [String: Any]?,
@@ -173,6 +188,14 @@ public struct MicrocmsClient {
173188
completion: completion)
174189
}
175190

191+
/// put content for microCMS .
192+
///
193+
/// - Parameters:
194+
/// - endpoint: endpoint of contents.
195+
/// - contentId: contentId. you can specify contentId for new content.
196+
/// - params: some parameters for body.
197+
/// - Returns: URLSessionTask you requested. Basically, you don't need to use it, but it helps you to manage state or cancel request.
198+
@discardableResult
176199
public func put(
177200
endpoint: String,
178201
contentId: String,
@@ -185,6 +208,14 @@ public struct MicrocmsClient {
185208
completion: completion)
186209
}
187210

211+
/// patch content for microCMS .
212+
///
213+
/// - Parameters:
214+
/// - endpoint: endpoint of contents.
215+
/// - contentId: contentId of target.
216+
/// - params: some parameters for body.
217+
/// - Returns: URLSessionTask you requested. Basically, you don't need to use it, but it helps you to manage state or cancel request.
218+
@discardableResult
188219
public func patch(
189220
endpoint: String,
190221
contentId: String,
@@ -197,6 +228,13 @@ public struct MicrocmsClient {
197228
completion: completion)
198229
}
199230

231+
/// delete content for microCMS .
232+
///
233+
/// - Parameters:
234+
/// - endpoint: endpoint of contents.
235+
/// - contentId: contentId of target.
236+
/// - Returns: URLSessionTask you requested. Basically, you don't need to use it, but it helps you to manage state or cancel request.
237+
@discardableResult
200238
public func delete(
201239
endpoint: String,
202240
contentId: String,

Tests/MicrocmsSDKTests/MicrocmsSDKTests.swift

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import XCTest
22
@testable import MicrocmsSDK
33

44
final class MicrocmsSDKTests: XCTestCase {
5+
func testExample() {
6+
XCTAssertEqual("microcms", "microcms")
7+
}
8+
59
func testBaseUrl() {
610
let client = MicrocmsClient(
711
serviceDomain: "test-service",
812
apiKey: "test-api-key")
913
XCTAssertEqual(client.baseUrl, "https://test-service.microcms.io/api/v1")
1014
}
1115

12-
func testMakeRequest_list() {
16+
func testMakeGetRequest_list() {
1317
let client = MicrocmsClient(
1418
serviceDomain: "test-service",
1519
apiKey: "test-api-key")
@@ -21,7 +25,7 @@ final class MicrocmsSDKTests: XCTestCase {
2125
"https://test-service.microcms.io/api/v1/endpoint")
2226
}
2327

24-
func testMakeRequest_detail() {
28+
func testMakeGetRequest_detail() {
2529
let client = MicrocmsClient(
2630
serviceDomain: "test-service",
2731
apiKey: "test-api-key")
@@ -33,7 +37,7 @@ final class MicrocmsSDKTests: XCTestCase {
3337
"https://test-service.microcms.io/api/v1/endpoint/contentId")
3438
}
3539

36-
func testMakeRequest_params() {
40+
func testMakeGetRequest_params() {
3741
let client = MicrocmsClient(
3842
serviceDomain: "test-service",
3943
apiKey: "test-api-key")
@@ -62,7 +66,7 @@ final class MicrocmsSDKTests: XCTestCase {
6266
XCTAssertTrue(request?.url?.query?.contains("filters=createdAt%5Bgreater_than%5D2019-11") == true)
6367
}
6468

65-
func testMakeRequest_headers() {
69+
func testMakeGetRequest_headers() {
6670
let client = MicrocmsClient(
6771
serviceDomain: "test-service",
6872
apiKey: "test-api-key")
@@ -83,8 +87,92 @@ final class MicrocmsSDKTests: XCTestCase {
8387
params: nil) { _ in }
8488
XCTAssertNotNil(task)
8589
}
90+
91+
func testMakeWriteRequest_post() {
92+
let client = MicrocmsClient(
93+
serviceDomain: "test-service",
94+
apiKey: "test-api-key")
95+
let params = ["title": "test-title"]
96+
let request = client.makeWriteRequest(method: .POST,
97+
endpoint: "test-endpoint",
98+
contentId: nil,
99+
params: params)
100+
XCTAssertEqual(request?.url?.absoluteString, "https://test-service.microcms.io/api/v1/test-endpoint")
101+
XCTAssertEqual(request?.httpMethod, "POST")
102+
XCTAssertEqual(request?.allHTTPHeaderFields, ["X-MICROCMS-API-KEY": "test-api-key",
103+
"Content-Type": "application/json"])
104+
105+
let expectedData = try! JSONSerialization.data(
106+
withJSONObject: params,
107+
options: .prettyPrinted)
108+
XCTAssertEqual(request?.httpBody, expectedData)
109+
}
110+
111+
func testMakeWriteRequest_put() {
112+
let client = MicrocmsClient(
113+
serviceDomain: "test-service",
114+
apiKey: "test-api-key")
115+
let params = ["title": "test-title"]
116+
let request = client.makeWriteRequest(method: .PUT,
117+
endpoint: "test-endpoint",
118+
contentId: "test-contentId",
119+
params: params)
120+
XCTAssertEqual(request?.url?.absoluteString, "https://test-service.microcms.io/api/v1/test-endpoint/test-contentId")
121+
XCTAssertEqual(request?.httpMethod, "PUT")
122+
XCTAssertEqual(request?.allHTTPHeaderFields, ["X-MICROCMS-API-KEY": "test-api-key",
123+
"Content-Type": "application/json"])
124+
125+
let expectedData = try! JSONSerialization.data(
126+
withJSONObject: params,
127+
options: .prettyPrinted)
128+
XCTAssertEqual(request?.httpBody, expectedData)
129+
}
130+
131+
func testMakeWriteRequest_patch() {
132+
let client = MicrocmsClient(
133+
serviceDomain: "test-service",
134+
apiKey: "test-api-key")
135+
let params = ["title": "test-title"]
136+
let request = client.makeWriteRequest(method: .PATCH,
137+
endpoint: "test-endpoint",
138+
contentId: "test-contentId",
139+
params: params)
140+
XCTAssertEqual(request?.url?.absoluteString, "https://test-service.microcms.io/api/v1/test-endpoint/test-contentId")
141+
XCTAssertEqual(request?.httpMethod, "PATCH")
142+
XCTAssertEqual(request?.allHTTPHeaderFields, ["X-MICROCMS-API-KEY": "test-api-key",
143+
"Content-Type": "application/json"])
144+
145+
let expectedData = try! JSONSerialization.data(
146+
withJSONObject: params,
147+
options: .prettyPrinted)
148+
XCTAssertEqual(request?.httpBody, expectedData)
149+
}
150+
151+
func testMakeWriteRequest_delete() {
152+
let client = MicrocmsClient(
153+
serviceDomain: "test-service",
154+
apiKey: "test-api-key")
155+
let request = client.makeWriteRequest(method: .DELETE,
156+
endpoint: "test-endpoint",
157+
contentId: "test-contentId",
158+
params: nil)
159+
XCTAssertEqual(request?.url?.absoluteString, "https://test-service.microcms.io/api/v1/test-endpoint/test-contentId")
160+
XCTAssertEqual(request?.httpMethod, "DELETE")
161+
XCTAssertEqual(request?.allHTTPHeaderFields, ["X-MICROCMS-API-KEY": "test-api-key",
162+
"Content-Type": "application/json"])
163+
}
86164

87165
static var allTests = [
88-
("testExample", testBaseUrl),
166+
("testExample",
167+
testBaseUrl,
168+
testMakeGetRequest_list,
169+
testMakeGetRequest_detail,
170+
testMakeGetRequest_params,
171+
testMakeGetRequest_headers,
172+
testGet,
173+
testMakeWriteRequest_post,
174+
testMakeWriteRequest_put,
175+
testMakeWriteRequest_patch,
176+
testMakeWriteRequest_delete),
89177
]
90178
}

0 commit comments

Comments
 (0)