https://github.com/417-72ki/multipartformdataparser
Testing tool for `multipart/form-data`
https://github.com/417-72ki/multipartformdataparser
ios macos swift
Last synced: about 1 year ago
JSON representation
Testing tool for `multipart/form-data`
- Host: GitHub
- URL: https://github.com/417-72ki/multipartformdataparser
- Owner: 417-72KI
- License: mit
- Created: 2019-12-13T17:08:09.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-03-23T18:14:20.000Z (over 1 year ago)
- Last Synced: 2025-03-30T16:46:31.125Z (about 1 year ago)
- Topics: ios, macos, swift
- Language: Swift
- Homepage:
- Size: 317 KB
- Stars: 14
- Watchers: 2
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MultipartFormDataParser
[](https://github.com/417-72KI/MultipartFormDataParser/actions)
[](http://cocoapods.org/pods/MultipartFormDataParser)
[](http://cocoapods.org/pods/MultipartFormDataParser)
[](https://github.com/417-72KI/MultipartFormDataParser/releases)
[](https://github.com/apple/swift-package-manager)
[](https://swiftpackageindex.com/417-72KI/MultipartFormDataParser)
[](https://swiftpackageindex.com/417-72KI/MultipartFormDataParser)
[](https://raw.githubusercontent.com/417-72KI/MultipartFormDataParser/master/LICENSE)
`MultipartFormDataParser` is a 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](https://www.ietf.org/rfc/rfc2388.txt)
Most famous networking libraries (e.g. [Alamofire](https://github.com/Alamofire/Alamofire), [APIKit](https://github.com/ishkawa/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.
```swift
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](https://github.com/AliSoftware/OHHTTPStubs), we can test a request created by networking libraries easily.
```swift
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
### Swift Package Manager (recommended)
Package.swift
```swift
dependencies: [
.package(url: "https://github.com/417-72KI/MultipartFormDataParser.git", from: "2.3.1")
]
```
### CocoaPods
Podfile
```ruby
pod 'MultipartFormDataParser'
```