Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TextPlainParameters to support sending plain text request body #317

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/APIKit2MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ APIKit provides 3 parameters types that conform to `BodyParametersType`:
- [**Added**] `class JSONBodyParameters`
- [**Added**] `class FormURLEncodedBodyParameters`
- [**Added**] `class MultipartFormDataBodyParameters`
- [**Added**] `class TextPlainParameters`

### HTTP Headers

Expand Down
1 change: 1 addition & 0 deletions Documentation/ConvenienceParametersAndActualParameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ APIKit provides 3 body parameters type listed below:
|`JSONBodyParameters` |`Any` |
|`FormURLEncodedBodyParameters` |`[String: Any]` |
|`MultipartFormDataBodyParameters`|`[MultipartFormDataBodyParameters.Part]`|
|`TextPlainParameters` |`String` |
23 changes: 23 additions & 0 deletions Sources/APIKit/BodyParameters/TextPlainParameters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

/// `TextPlainParameters` serializes plain text for HTTP body and states its content type is plain text.
public struct TextPlainParameters: BodyParameters {
/// The plain text to be serialized.
private let text: String

/// Returns `TextPlainParameters` that is initialized with plain text.
public init(text: String) {
self.text = text
}

// MARK: - BodyParameters
/// `Content-Type` to send. The value for this property will be set to `Accept` HTTP header field.
public var contentType: String {
"text/plain"
}

/// Builds `RequestBodyEntity.data` that represents plain text.
public func buildEntity() throws -> RequestBodyEntity {
.data(Data(self.text.utf8))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Foundation
import XCTest
import APIKit

class TextPlainParametersTests: XCTestCase {
func testTextPlainSuccess() throws {
let text = "Hello, World!"
let parameters = TextPlainParameters(text: text)

XCTAssertEqual(parameters.contentType, "text/plain")

guard case .data(let data) = try parameters.buildEntity() else {
XCTFail()
return
}

let decodedText = String(data: data, encoding: .utf8)
XCTAssertEqual(decodedText, text)
}

func testTextPlainEmpty() throws {
let text = ""
let parameters = TextPlainParameters(text: text)

XCTAssertEqual(parameters.contentType, "text/plain")

guard case .data(let data) = try parameters.buildEntity() else {
XCTFail()
return
}

let decodedText = String(data: data, encoding: .utf8)
XCTAssertEqual(decodedText, text)
}

func testTextPlainUnicode() throws {
let text = "Hello, World! 😄🌍"
let parameters = TextPlainParameters(text: text)

XCTAssertEqual(parameters.contentType, "text/plain")

guard case .data(let data) = try parameters.buildEntity() else {
XCTFail()
return
}

let decodedText = String(data: data, encoding: .utf8)
XCTAssertEqual(decodedText, text)
}
}