|
| 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