Swiftpack.co - 417-72KI/MultipartFormDataParser as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
417-72KI/MultipartFormDataParser
Testing tool for `multipart/form-data`
.package(url: "https://github.com/417-72KI/MultipartFormDataParser.git", from: "1.4.0")

MultipartFormDataParser

Actions Status GitHub release Swift Package Manager Carthage Compatible GitHub license

Testing tool for multipart/form-data request in Swift.

When to upload some files via API, we must use multipart/form-data for request. multipart/form-data is defined as RFC-2388

Most famous networking libraries (e.g. Alamofire, APIKit) can implement easily. However, to test if the created request is as expected is difficult and bothering.

This library provides a parser for multipart/form-data request to test it briefly.

let request: URLRequest = ...
do {
    let data = try MultipartFormData.parse(from: request)
    let genbaNeko = try XCTUnwrap(data.element(forName: "genbaNeko"))
    let message = try XCTUnwrap(data.element(forName: "message"))
    XCTAssertNotNil(Image(data: genbaNeko.data))
    XCTAssertEqual(genbaNeko.mimeType, "image/jpeg")
    XCTAssertEqual(message.string, "Hello world!")
} catch {
    XCTFail(error.localizedDescription)
}

Using OHHTTPStubs, we can test a request created by networking libraries easily.

let expectedGenbaNeko: Data = ...

let condition = isHost("localhost") && isPath("/upload")
stub(condition: condition) { request in
    let errorResponse = { (message: String) -> HTTPStubsResponse in
        .init(
            jsonObject: ["status": 403, "error": message],
            statusCode: 403, 
            headers: ["Content-Type": "application/json"]
        )
    }
    do {
        let data = try MultipartFormData.parse(from: request)
        guard let genbaNeko = data.element(forName: "genbaNeko"),
              genbaNeko.data == expectedGenbaNeko else { return errorResponse("Unexpected genbaNeko") }
        guard let message = data.element(forName: "message"),
              message.string == "Hello world!" else { return errorResponse("Unexpected message: \(message)") }
    } catch {
        return .init(error: error)
    }
    return .init(
        jsonObject: ["status": 200],
        statusCode: 200,
        headers: ["Content-Type": "application/json"]
    )
}

Installation

Carthage

Adding to Cartfile.private is recommended.

github "417-72KI/MultipartFormDataParser"

Swift Package Manager

Package.swift

dependencies: [
    .package(url: "https://github.com/417-72KI/MultipartFormDataParser.git", from: "1.4.0")
]

GitHub

link
Stars: 12
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.4.0
1 week ago

Xcode 13 support

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics