Skip to content

Commit

Permalink
Merge pull request #11 from FelixHerrmann/feature/improve-code-coverage
Browse files Browse the repository at this point in the history
Feature/improve code coverage
  • Loading branch information
FelixHerrmann authored Mar 5, 2023
2 parents 27bbad4 + 443cdde commit c734010
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/MultipartFormData/HTTPHeaderField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions Tests/MultipartFormDataTests/BoundaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
27 changes: 18 additions & 9 deletions Tests/MultipartFormDataTests/Builder/BodyDataBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
15 changes: 12 additions & 3 deletions Tests/MultipartFormDataTests/Builder/HTTPHeaderBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions Tests/MultipartFormDataTests/ContentDispositionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
35 changes: 35 additions & 0 deletions Tests/MultipartFormDataTests/HTTPHeaderFieldTests.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
7 changes: 7 additions & 0 deletions Tests/MultipartFormDataTests/HTTPHeaderParameterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions Tests/MultipartFormDataTests/MediaTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
21 changes: 19 additions & 2 deletions Tests/MultipartFormDataTests/MultipartFormDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
16 changes: 16 additions & 0 deletions Tests/MultipartFormDataTests/SubpartTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit c734010

Please sign in to comment.