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