Skip to content

Commit 3c6c63c

Browse files
committed
added tests
1 parent 76d701a commit 3c6c63c

2 files changed

Lines changed: 129 additions & 3 deletions

File tree

Sources/SimpleHTTP/DataCoder/FormURL/FormURLEncoder.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import Foundation
22

3-
struct FormURLEncoder: ContentDataEncoder {
4-
static let contentType: HTTPContentType = .formURLEncoded
3+
public struct FormURLEncoder: ContentDataEncoder {
4+
public static let contentType: HTTPContentType = .formURLEncoded
55

6-
func encode<T: Encodable>(_ value: T) throws -> Data {
6+
public init() {
7+
8+
}
9+
10+
public func encode<T: Encodable>(_ value: T) throws -> Data {
711
let encoder = FormKeyValueEncoder()
812
try value.encode(to: encoder)
913

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import Testing
2+
import Foundation
3+
import SimpleHTTP
4+
5+
struct FormURLEncoderTests {
6+
let encoder = FormURLEncoder()
7+
8+
struct Encode {
9+
let encoder = FormURLEncoder()
10+
11+
@Test("single string field returns key=value pair")
12+
func singleStringField_returnsKeyValuePair() throws {
13+
let input = SingleField(name: "John")
14+
15+
let data = try encoder.encode(input)
16+
17+
#expect(String(data: data, encoding: .utf8) == "name=John")
18+
}
19+
20+
@Test("multiple fields returns ampersand-separated pairs")
21+
func multipleFields_returnsAmpersandSeparatedPairs() throws {
22+
let input = MultipleFields(name: "John", age: 30)
23+
24+
let data = try encoder.encode(input)
25+
26+
#expect(String(data: data, encoding: .utf8) == "name=John&age=30")
27+
}
28+
29+
@Test("boolean field returns true or false")
30+
func booleanField_returnsTrueOrFalse() throws {
31+
let input = BoolField(active: true)
32+
33+
let data = try encoder.encode(input)
34+
35+
#expect(String(data: data, encoding: .utf8) == "active=true")
36+
}
37+
38+
@Test("double field returns decimal value")
39+
func doubleField_returnsDecimalValue() throws {
40+
let input = DoubleField(score: 9.5)
41+
42+
let data = try encoder.encode(input)
43+
44+
#expect(String(data: data, encoding: .utf8) == "score=9.5")
45+
}
46+
47+
@Test("spaces in value are percent-encoded")
48+
func spacesInValue_arePercentEncoded() throws {
49+
let input = SingleField(name: "John Doe")
50+
51+
let data = try encoder.encode(input)
52+
let result = String(data: data, encoding: .utf8)
53+
54+
#expect(result == "name=John%20Doe")
55+
}
56+
57+
@Test("special characters in key are percent-encoded")
58+
func specialCharactersInKey_arePercentEncoded() throws {
59+
let input = SpecialKeyField(value: "hello")
60+
61+
let data = try encoder.encode(input)
62+
let result = String(data: data, encoding: .utf8)
63+
64+
#expect(result?.contains("my%20key=hello") == true)
65+
}
66+
67+
@Test("nil optional field is omitted")
68+
func nilOptionalField_isOmitted() throws {
69+
let input = OptionalField(name: "John", nickname: nil)
70+
71+
let data = try encoder.encode(input)
72+
73+
#expect(String(data: data, encoding: .utf8) == "name=John")
74+
}
75+
76+
@Test("present optional field is included")
77+
func presentOptionalField_isIncluded() throws {
78+
let input = OptionalField(name: "John", nickname: "JD")
79+
80+
let data = try encoder.encode(input)
81+
82+
#expect(String(data: data, encoding: .utf8) == "name=John&nickname=JD")
83+
}
84+
85+
@Test("content type is form URL encoded")
86+
func contentType_returnsFormURLEncoded() {
87+
#expect(FormURLEncoder.contentType == .formURLEncoded)
88+
}
89+
}
90+
}
91+
92+
// MARK: - Fixtures
93+
94+
private struct SingleField: Encodable {
95+
let name: String
96+
}
97+
98+
private struct MultipleFields: Encodable {
99+
let name: String
100+
let age: Int
101+
}
102+
103+
private struct BoolField: Encodable {
104+
let active: Bool
105+
}
106+
107+
private struct DoubleField: Encodable {
108+
let score: Double
109+
}
110+
111+
private struct OptionalField: Encodable {
112+
let name: String
113+
let nickname: String?
114+
}
115+
116+
private struct SpecialKeyField: Encodable {
117+
enum CodingKeys: String, CodingKey {
118+
case value = "my key"
119+
}
120+
121+
let value: String
122+
}

0 commit comments

Comments
 (0)