diff --git a/Sources/MultipartFormData/Builder/MultipartFormDataBuilder.swift b/Sources/MultipartFormData/Builder/MultipartFormDataBuilder.swift index 0257d27..973c5f7 100644 --- a/Sources/MultipartFormData/Builder/MultipartFormDataBuilder.swift +++ b/Sources/MultipartFormData/Builder/MultipartFormDataBuilder.swift @@ -20,10 +20,6 @@ public enum MultipartFormDataBuilder { return components.flatMap { $0 } } - public static func buildBlock(_ components: Subpart...) -> [Subpart] { - return components - } - public static func buildArray(_ components: [[Subpart]]) -> [Subpart] { return components.flatMap { $0 } } diff --git a/Sources/MultipartFormData/HTTPHeaderField.swift b/Sources/MultipartFormData/HTTPHeaderField.swift index bd53df0..7018a56 100644 --- a/Sources/MultipartFormData/HTTPHeaderField.swift +++ b/Sources/MultipartFormData/HTTPHeaderField.swift @@ -39,9 +39,8 @@ extension HTTPHeaderField { internal var _value: String { if parameters.isEmpty { return value - } else { - return "\(value); \(parameters._text)" } + return "\(value); \(parameters._text)" } internal var _text: String { diff --git a/Tests/MultipartFormDataTests/BoundaryTests.swift b/Tests/MultipartFormDataTests/BoundaryTests.swift index d04a28c..1ee0f05 100644 --- a/Tests/MultipartFormDataTests/BoundaryTests.swift +++ b/Tests/MultipartFormDataTests/BoundaryTests.swift @@ -49,4 +49,11 @@ final class BoundaryTests: XCTestCase { XCTAssertEqual(asciiString, randomBoundary._value) } } + + func testDebugDescription() throws { + let boundary = try Boundary(uncheckedBoundary: "test") + + let expectedDescription = "test" + XCTAssertEqual(boundary.debugDescription, expectedDescription) + } } diff --git a/Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift b/Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift index 78808be..e3c71a3 100644 --- a/Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift +++ b/Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift @@ -29,25 +29,34 @@ final class BodyDataBuilderTests: XCTestCase { func testAllBuildMethods() { let data = _buildData { - - // buildArray + // buildArray(_:) for index in 0...2 { Data(index.description.utf8) } - // buildOptional - if true { + // buildOptional(_:) + if Bool(truncating: 1) { Data("true".utf8) } + if Bool(truncating: 0) { + Data("false".utf8) + } + + // buildEither(first:) + if Bool(truncating: 1) { + Data("first".utf8) + } else { + Data("second".utf8) + } - // buildEither - if .random() { - Data("random".utf8) + // buildEither(second:) + if Bool(truncating: 0) { + Data("first".utf8) } else { - Data("random".utf8) + Data("second".utf8) } } - XCTAssertEqual(data, Data("012truerandom".utf8)) + XCTAssertEqual(data, Data("012truefirstsecond".utf8)) } } diff --git a/Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift b/Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift index 87640a4..7b229db 100644 --- a/Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift +++ b/Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift @@ -34,15 +34,24 @@ final class HTTPHeaderBuilderTests: XCTestCase { func testAllBuildMethods() { let buildResult = _buildHeader { - - // buildEither - if .random() { + // buildEither(first:) + if Bool(truncating: 1) { ContentDisposition(name: "a") } else { ContentDisposition(name: "a") } } XCTAssertEqual(buildResult._contentDisposition, ContentDisposition(name: "a")) + + let buildResult2 = _buildHeader { + // buildEither(second:) + if Bool(truncating: 0) { + ContentDisposition(name: "b") + } else { + ContentDisposition(name: "b") + } + } + XCTAssertEqual(buildResult2._contentDisposition, ContentDisposition(name: "b")) } } diff --git a/Tests/MultipartFormDataTests/Builder/MultipartFormDataBuilderTests.swift b/Tests/MultipartFormDataTests/Builder/MultipartFormDataBuilderTests.swift index 4deee05..66efb8e 100644 --- a/Tests/MultipartFormDataTests/Builder/MultipartFormDataBuilderTests.swift +++ b/Tests/MultipartFormDataTests/Builder/MultipartFormDataBuilderTests.swift @@ -50,10 +50,11 @@ final class MultipartFormDataBuilderTests: XCTestCase { XCTAssertEqual(subparts, expectedSubparts) } + // swiftlint:disable function_body_length + // swiftlint:disable closure_body_length func testAllBuildMethods() throws { let subparts = try _buildSubparts { - - // buildArray + // buildArray(_:) for index in 0...2 { try Subpart { try ContentDisposition(uncheckedName: index.description) @@ -62,27 +63,49 @@ final class MultipartFormDataBuilderTests: XCTestCase { } } - // buildOptional - if true { + // buildOptional(_:) + if Bool(truncating: 1) { Subpart { ContentDisposition(name: "true") } body: { Data("true".utf8) } } + if Bool(truncating: 0) { + Subpart { + ContentDisposition(name: "false") + } body: { + Data("false".utf8) + } + } + + // buildEither(first:) + if Bool(truncating: 1) { + Subpart { + ContentDisposition(name: "first") + } body: { + Data("first".utf8) + } + } else { + Subpart { + ContentDisposition(name: "second") + } body: { + Data("second".utf8) + } + } - // buildEither - if .random() { + // buildEither(second:) + if Bool(truncating: 0) { Subpart { - ContentDisposition(name: "random") + ContentDisposition(name: "first") } body: { - Data("random".utf8) + Data("first".utf8) } } else { Subpart { - ContentDisposition(name: "random") + ContentDisposition(name: "second") } body: { - Data("random".utf8) + Data("second".utf8) } } } @@ -91,10 +114,13 @@ final class MultipartFormDataBuilderTests: XCTestCase { Subpart(contentDisposition: ContentDisposition(name: "1"), body: Data("1".utf8)), Subpart(contentDisposition: ContentDisposition(name: "2"), body: Data("2".utf8)), Subpart(contentDisposition: ContentDisposition(name: "true"), body: Data("true".utf8)), - Subpart(contentDisposition: ContentDisposition(name: "random"), body: Data("random".utf8)), + Subpart(contentDisposition: ContentDisposition(name: "first"), body: Data("first".utf8)), + Subpart(contentDisposition: ContentDisposition(name: "second"), body: Data("second".utf8)), ] XCTAssertEqual(subparts, expectedSubparts) } + // swiftlint:enable function_body_length + // swiftlint:enable closure_body_length } extension MultipartFormDataBuilderTests { diff --git a/Tests/MultipartFormDataTests/ContentDispositionTests.swift b/Tests/MultipartFormDataTests/ContentDispositionTests.swift index f5a1097..a10e182 100644 --- a/Tests/MultipartFormDataTests/ContentDispositionTests.swift +++ b/Tests/MultipartFormDataTests/ContentDispositionTests.swift @@ -17,9 +17,8 @@ final class ContentDispositionTests: XCTestCase { // does not work on Linux, can still encoding there let nonPercentEncodableString = try XCTUnwrap(String(bytes: [0xD8, 0x00] as [UInt8], encoding: .utf16BigEndian)) if nonPercentEncodableString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) == nil { - XCTAssertThrowsError( - try ContentDisposition(uncheckedName: nonPercentEncodableString, uncheckedFilename: nonPercentEncodableString) - ) + XCTAssertThrowsError(try ContentDisposition(uncheckedName: nonPercentEncodableString, uncheckedFilename: nil)) + XCTAssertThrowsError(try ContentDisposition(uncheckedName: "", uncheckedFilename: nonPercentEncodableString)) } } diff --git a/Tests/MultipartFormDataTests/HTTPHeaderFieldTests.swift b/Tests/MultipartFormDataTests/HTTPHeaderFieldTests.swift new file mode 100644 index 0000000..b67d6cf --- /dev/null +++ b/Tests/MultipartFormDataTests/HTTPHeaderFieldTests.swift @@ -0,0 +1,35 @@ +// +// HTTPHeaderFieldTests.swift +// swift-multipart-formdata +// +// Created by Felix Herrmann on 05.03.23. +// + +import XCTest +@testable import MultipartFormData + +final class HTTPHeaderFieldTests: XCTestCase { + + func testDebugDescription() { + let parameter = HTTPHeaderParameter("name", value: "value") + let testHeaderField = TestHeaderField(value: "value", parameters: [parameter]) + + let expectedDescription = "Test: value; name=\"value\"" + XCTAssertEqual(testHeaderField.debugDescription, expectedDescription) + } +} + +extension HTTPHeaderFieldTests { + private struct TestHeaderField: HTTPHeaderField { + static let name: String = "Test" + + var value: String + + var parameters: [HTTPHeaderParameter] + + init(value: String, parameters: [HTTPHeaderParameter]) { + self.value = value + self.parameters = parameters + } + } +} diff --git a/Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift b/Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift index c8a3449..b1b9420 100644 --- a/Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift +++ b/Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift @@ -23,4 +23,11 @@ final class HTTPHeaderParameterTests: XCTestCase { ] XCTAssertEqual(parameters._text, "test=\"a\"; test=\"a\"; test=\"a\"") } + + func testDebugDescription() { + let parameter = HTTPHeaderParameter("test", value: "a") + + let expectedDescription = "test=\"a\"" + XCTAssertEqual(parameter.debugDescription, expectedDescription) + } } diff --git a/Tests/MultipartFormDataTests/MediaTypeTests.swift b/Tests/MultipartFormDataTests/MediaTypeTests.swift index 159a633..dc4e40d 100644 --- a/Tests/MultipartFormDataTests/MediaTypeTests.swift +++ b/Tests/MultipartFormDataTests/MediaTypeTests.swift @@ -14,4 +14,11 @@ final class MediaTypeTests: XCTestCase { let mediaType = MediaType(type: "type", subtype: "subtype") XCTAssertEqual(mediaType._text, "type/subtype") } + + func testDebugDescription() { + let mediaType = MediaType(type: "type", subtype: "subtype") + + let expectedDescription = "type/subtype" + XCTAssertEqual(mediaType.debugDescription, expectedDescription) + } } diff --git a/Tests/MultipartFormDataTests/MultipartFormDataTests.swift b/Tests/MultipartFormDataTests/MultipartFormDataTests.swift index cf715f3..2dbd111 100644 --- a/Tests/MultipartFormDataTests/MultipartFormDataTests.swift +++ b/Tests/MultipartFormDataTests/MultipartFormDataTests.swift @@ -50,7 +50,7 @@ final class MultipartFormDataTests: XCTestCase { "Content-Type: application/octet-stream", "", "", - "--test--\r\n" + "--test--\r\n", ].joined(separator: "\r\n").utf8) XCTAssertEqual(multipartFormData.httpBody, expectedBody) } @@ -91,8 +91,25 @@ final class MultipartFormDataTests: XCTestCase { "Content-Type: application/octet-stream", "", "", - "--test--\r\n" + "--test--\r\n", ].joined(separator: "\r\n") XCTAssertEqual(multipartFormData.debugDescription, expectedDescription) } + + func testBuilderInit() throws { + let boundary = try Boundary(uncheckedBoundary: "test") + let jsonData = try JSONSerialization.data(withJSONObject: ["a": 1]) + let multipartFormData = MultipartFormData(boundary: boundary) { + Subpart { + ContentDisposition(name: "json") + ContentType(mediaType: .applicationJson) + } body: { + jsonData + } + } + + XCTAssertEqual(multipartFormData.boundary, boundary) + XCTAssertEqual(multipartFormData.body.first?.contentType?.mediaType, .applicationJson) + XCTAssertEqual(multipartFormData.body.first?.body, jsonData) + } } diff --git a/Tests/MultipartFormDataTests/SubpartTests.swift b/Tests/MultipartFormDataTests/SubpartTests.swift index eb8037d..e7df384 100644 --- a/Tests/MultipartFormDataTests/SubpartTests.swift +++ b/Tests/MultipartFormDataTests/SubpartTests.swift @@ -24,4 +24,20 @@ final class SubpartTests: XCTestCase { ].joined(separator: "\r\n").utf8) XCTAssertEqual(subpart._data, expectedData) } + + func testDebugDescription() { + let subpart = Subpart( + contentDisposition: ContentDisposition(name: "a"), + contentType: ContentType(mediaType: .textPlain), + body: Data("a".utf8) + ) + + let expectedDescription = [ + "Content-Disposition: form-data; name=\"a\"", + "Content-Type: text/plain", + "", + "a", + ].joined(separator: "\r\n") + XCTAssertEqual(subpart.debugDescription, expectedDescription) + } }