Skip to content

Commit c734010

Browse files
Merge pull request #11 from FelixHerrmann/feature/improve-code-coverage
Feature/improve code coverage
2 parents 27bbad4 + 443cdde commit c734010

12 files changed

+161
-34
lines changed

Sources/MultipartFormData/Builder/MultipartFormDataBuilder.swift

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ public enum MultipartFormDataBuilder {
2020
return components.flatMap { $0 }
2121
}
2222

23-
public static func buildBlock(_ components: Subpart...) -> [Subpart] {
24-
return components
25-
}
26-
2723
public static func buildArray(_ components: [[Subpart]]) -> [Subpart] {
2824
return components.flatMap { $0 }
2925
}

Sources/MultipartFormData/HTTPHeaderField.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ extension HTTPHeaderField {
3939
internal var _value: String {
4040
if parameters.isEmpty {
4141
return value
42-
} else {
43-
return "\(value); \(parameters._text)"
4442
}
43+
return "\(value); \(parameters._text)"
4544
}
4645

4746
internal var _text: String {

Tests/MultipartFormDataTests/BoundaryTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@ final class BoundaryTests: XCTestCase {
4949
XCTAssertEqual(asciiString, randomBoundary._value)
5050
}
5151
}
52+
53+
func testDebugDescription() throws {
54+
let boundary = try Boundary(uncheckedBoundary: "test")
55+
56+
let expectedDescription = "test"
57+
XCTAssertEqual(boundary.debugDescription, expectedDescription)
58+
}
5259
}

Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift

+18-9
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,34 @@ final class BodyDataBuilderTests: XCTestCase {
2929

3030
func testAllBuildMethods() {
3131
let data = _buildData {
32-
33-
// buildArray
32+
// buildArray(_:)
3433
for index in 0...2 {
3534
Data(index.description.utf8)
3635
}
3736

38-
// buildOptional
39-
if true {
37+
// buildOptional(_:)
38+
if Bool(truncating: 1) {
4039
Data("true".utf8)
4140
}
41+
if Bool(truncating: 0) {
42+
Data("false".utf8)
43+
}
44+
45+
// buildEither(first:)
46+
if Bool(truncating: 1) {
47+
Data("first".utf8)
48+
} else {
49+
Data("second".utf8)
50+
}
4251

43-
// buildEither
44-
if .random() {
45-
Data("random".utf8)
52+
// buildEither(second:)
53+
if Bool(truncating: 0) {
54+
Data("first".utf8)
4655
} else {
47-
Data("random".utf8)
56+
Data("second".utf8)
4857
}
4958
}
50-
XCTAssertEqual(data, Data("012truerandom".utf8))
59+
XCTAssertEqual(data, Data("012truefirstsecond".utf8))
5160
}
5261
}
5362

Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ final class HTTPHeaderBuilderTests: XCTestCase {
3434

3535
func testAllBuildMethods() {
3636
let buildResult = _buildHeader {
37-
38-
// buildEither
39-
if .random() {
37+
// buildEither(first:)
38+
if Bool(truncating: 1) {
4039
ContentDisposition(name: "a")
4140
} else {
4241
ContentDisposition(name: "a")
4342
}
4443
}
4544
XCTAssertEqual(buildResult._contentDisposition, ContentDisposition(name: "a"))
45+
46+
let buildResult2 = _buildHeader {
47+
// buildEither(second:)
48+
if Bool(truncating: 0) {
49+
ContentDisposition(name: "b")
50+
} else {
51+
ContentDisposition(name: "b")
52+
}
53+
}
54+
XCTAssertEqual(buildResult2._contentDisposition, ContentDisposition(name: "b"))
4655
}
4756
}
4857

Tests/MultipartFormDataTests/Builder/MultipartFormDataBuilderTests.swift

+37-11
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ final class MultipartFormDataBuilderTests: XCTestCase {
5050
XCTAssertEqual(subparts, expectedSubparts)
5151
}
5252

53+
// swiftlint:disable function_body_length
54+
// swiftlint:disable closure_body_length
5355
func testAllBuildMethods() throws {
5456
let subparts = try _buildSubparts {
55-
56-
// buildArray
57+
// buildArray(_:)
5758
for index in 0...2 {
5859
try Subpart {
5960
try ContentDisposition(uncheckedName: index.description)
@@ -62,27 +63,49 @@ final class MultipartFormDataBuilderTests: XCTestCase {
6263
}
6364
}
6465

65-
// buildOptional
66-
if true {
66+
// buildOptional(_:)
67+
if Bool(truncating: 1) {
6768
Subpart {
6869
ContentDisposition(name: "true")
6970
} body: {
7071
Data("true".utf8)
7172
}
7273
}
74+
if Bool(truncating: 0) {
75+
Subpart {
76+
ContentDisposition(name: "false")
77+
} body: {
78+
Data("false".utf8)
79+
}
80+
}
81+
82+
// buildEither(first:)
83+
if Bool(truncating: 1) {
84+
Subpart {
85+
ContentDisposition(name: "first")
86+
} body: {
87+
Data("first".utf8)
88+
}
89+
} else {
90+
Subpart {
91+
ContentDisposition(name: "second")
92+
} body: {
93+
Data("second".utf8)
94+
}
95+
}
7396

74-
// buildEither
75-
if .random() {
97+
// buildEither(second:)
98+
if Bool(truncating: 0) {
7699
Subpart {
77-
ContentDisposition(name: "random")
100+
ContentDisposition(name: "first")
78101
} body: {
79-
Data("random".utf8)
102+
Data("first".utf8)
80103
}
81104
} else {
82105
Subpart {
83-
ContentDisposition(name: "random")
106+
ContentDisposition(name: "second")
84107
} body: {
85-
Data("random".utf8)
108+
Data("second".utf8)
86109
}
87110
}
88111
}
@@ -91,10 +114,13 @@ final class MultipartFormDataBuilderTests: XCTestCase {
91114
Subpart(contentDisposition: ContentDisposition(name: "1"), body: Data("1".utf8)),
92115
Subpart(contentDisposition: ContentDisposition(name: "2"), body: Data("2".utf8)),
93116
Subpart(contentDisposition: ContentDisposition(name: "true"), body: Data("true".utf8)),
94-
Subpart(contentDisposition: ContentDisposition(name: "random"), body: Data("random".utf8)),
117+
Subpart(contentDisposition: ContentDisposition(name: "first"), body: Data("first".utf8)),
118+
Subpart(contentDisposition: ContentDisposition(name: "second"), body: Data("second".utf8)),
95119
]
96120
XCTAssertEqual(subparts, expectedSubparts)
97121
}
122+
// swiftlint:enable function_body_length
123+
// swiftlint:enable closure_body_length
98124
}
99125

100126
extension MultipartFormDataBuilderTests {

Tests/MultipartFormDataTests/ContentDispositionTests.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ final class ContentDispositionTests: XCTestCase {
1717
// does not work on Linux, can still encoding there
1818
let nonPercentEncodableString = try XCTUnwrap(String(bytes: [0xD8, 0x00] as [UInt8], encoding: .utf16BigEndian))
1919
if nonPercentEncodableString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) == nil {
20-
XCTAssertThrowsError(
21-
try ContentDisposition(uncheckedName: nonPercentEncodableString, uncheckedFilename: nonPercentEncodableString)
22-
)
20+
XCTAssertThrowsError(try ContentDisposition(uncheckedName: nonPercentEncodableString, uncheckedFilename: nil))
21+
XCTAssertThrowsError(try ContentDisposition(uncheckedName: "", uncheckedFilename: nonPercentEncodableString))
2322
}
2423
}
2524

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// HTTPHeaderFieldTests.swift
3+
// swift-multipart-formdata
4+
//
5+
// Created by Felix Herrmann on 05.03.23.
6+
//
7+
8+
import XCTest
9+
@testable import MultipartFormData
10+
11+
final class HTTPHeaderFieldTests: XCTestCase {
12+
13+
func testDebugDescription() {
14+
let parameter = HTTPHeaderParameter("name", value: "value")
15+
let testHeaderField = TestHeaderField(value: "value", parameters: [parameter])
16+
17+
let expectedDescription = "Test: value; name=\"value\""
18+
XCTAssertEqual(testHeaderField.debugDescription, expectedDescription)
19+
}
20+
}
21+
22+
extension HTTPHeaderFieldTests {
23+
private struct TestHeaderField: HTTPHeaderField {
24+
static let name: String = "Test"
25+
26+
var value: String
27+
28+
var parameters: [HTTPHeaderParameter]
29+
30+
init(value: String, parameters: [HTTPHeaderParameter]) {
31+
self.value = value
32+
self.parameters = parameters
33+
}
34+
}
35+
}

Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ final class HTTPHeaderParameterTests: XCTestCase {
2323
]
2424
XCTAssertEqual(parameters._text, "test=\"a\"; test=\"a\"; test=\"a\"")
2525
}
26+
27+
func testDebugDescription() {
28+
let parameter = HTTPHeaderParameter("test", value: "a")
29+
30+
let expectedDescription = "test=\"a\""
31+
XCTAssertEqual(parameter.debugDescription, expectedDescription)
32+
}
2633
}

Tests/MultipartFormDataTests/MediaTypeTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ final class MediaTypeTests: XCTestCase {
1414
let mediaType = MediaType(type: "type", subtype: "subtype")
1515
XCTAssertEqual(mediaType._text, "type/subtype")
1616
}
17+
18+
func testDebugDescription() {
19+
let mediaType = MediaType(type: "type", subtype: "subtype")
20+
21+
let expectedDescription = "type/subtype"
22+
XCTAssertEqual(mediaType.debugDescription, expectedDescription)
23+
}
1724
}

Tests/MultipartFormDataTests/MultipartFormDataTests.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class MultipartFormDataTests: XCTestCase {
5050
"Content-Type: application/octet-stream",
5151
"",
5252
"",
53-
"--test--\r\n"
53+
"--test--\r\n",
5454
].joined(separator: "\r\n").utf8)
5555
XCTAssertEqual(multipartFormData.httpBody, expectedBody)
5656
}
@@ -91,8 +91,25 @@ final class MultipartFormDataTests: XCTestCase {
9191
"Content-Type: application/octet-stream",
9292
"",
9393
"",
94-
"--test--\r\n"
94+
"--test--\r\n",
9595
].joined(separator: "\r\n")
9696
XCTAssertEqual(multipartFormData.debugDescription, expectedDescription)
9797
}
98+
99+
func testBuilderInit() throws {
100+
let boundary = try Boundary(uncheckedBoundary: "test")
101+
let jsonData = try JSONSerialization.data(withJSONObject: ["a": 1])
102+
let multipartFormData = MultipartFormData(boundary: boundary) {
103+
Subpart {
104+
ContentDisposition(name: "json")
105+
ContentType(mediaType: .applicationJson)
106+
} body: {
107+
jsonData
108+
}
109+
}
110+
111+
XCTAssertEqual(multipartFormData.boundary, boundary)
112+
XCTAssertEqual(multipartFormData.body.first?.contentType?.mediaType, .applicationJson)
113+
XCTAssertEqual(multipartFormData.body.first?.body, jsonData)
114+
}
98115
}

Tests/MultipartFormDataTests/SubpartTests.swift

+16
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,20 @@ final class SubpartTests: XCTestCase {
2424
].joined(separator: "\r\n").utf8)
2525
XCTAssertEqual(subpart._data, expectedData)
2626
}
27+
28+
func testDebugDescription() {
29+
let subpart = Subpart(
30+
contentDisposition: ContentDisposition(name: "a"),
31+
contentType: ContentType(mediaType: .textPlain),
32+
body: Data("a".utf8)
33+
)
34+
35+
let expectedDescription = [
36+
"Content-Disposition: form-data; name=\"a\"",
37+
"Content-Type: text/plain",
38+
"",
39+
"a",
40+
].joined(separator: "\r\n")
41+
XCTAssertEqual(subpart.debugDescription, expectedDescription)
42+
}
2743
}

0 commit comments

Comments
 (0)