Repository: vapor/vapor
File: Tests/VaporTests/URLEncodedFormTests.swift (Swift)

Code before:
                return dateFormatter
            }
        }
        let factory = DateFormatterFactory()
        let resultCustom = try URLEncodedFormEncoder(
            configuration: .init(dateEncodingStrategy: .custom({ (date, encoder) in
                var container = encoder.singleValueContainer()
                try container.encode(factory.currentValue.string(from: date))
            }))
        ).encode(toEncode)
        XCTAssertEqual("date=Date:%201970-01-01%20Time:%2000:00:00%20Timezone:%20Z", resultCustom)
        
        let decodedCustom = try URLEncodedFormDecoder(
            configuration: .init(dateDecodingStrategy: .custom({ (decoder) -> Date in
                let container = try decoder.singleValueContainer()
                let string = try container.decode(String.self)
                guard let date = factory.currentValue.date(from: string) else {
                    throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to decode date from string '\(string)'")
                }
                return date
            }))
        ).decode(DateCoding.self, from: resultCustom)
        XCTAssertEqual(decodedCustom, toEncode)
    }

    func testGH2518() throws {
        let optionalDate: Date? = Date(timeIntervalSince1970: 0)
        let dateString = "1970-01-01T00:00:00Z"

        let resultForDecodedOptionalDate = try URLEncodedFormDecoder(
            configuration: .init(dateDecodingStrategy: .iso8601)
        ).decode(Date?.self, from: dateString)
        XCTAssertEqual(optionalDate, resultForDecodedOptionalDate)

        let resultForEncodedOptionalDate = try URLEncodedFormEncoder(
            configuration: .init(dateEncodingStrategy: .iso8601)
        ).encode(optionalDate)
        XCTAssertEqual(dateString, resultForEncodedOptionalDate)
    }

    func testEncodedArrayValues() throws {
        let user = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [.baz], nums: [3.14], isCool: true)
        let result = try URLEncodedFormEncoder(
            configuration: .init(arrayEncoding: .values)
        ).encode(user)
        XCTAssert(result.contains("pets=Zizek"))
        XCTAssert(result.contains("pets=Foo"))
        XCTAssert(result.contains("age=23"))
        XCTAssert(result.contains("name=Tanner"))
        XCTAssert(result.contains("dict[a]=1"))
        XCTAssert(result.contains("dict[b]=2"))

Review comment (suggestion):
Small nit, can we rename this so it's clearer what the test achieves
```suggestion
    func testOptionalDateEncodingAndDecoding_GH2518() throws {
```

Code after:
                return dateFormatter
            }
        }
        let factory = DateFormatterFactory()
        let resultCustom = try URLEncodedFormEncoder(
            configuration: .init(dateEncodingStrategy: .custom({ (date, encoder) in
                var container = encoder.singleValueContainer()
                try container.encode(factory.currentValue.string(from: date))
            }))
        ).encode(toEncode)
        XCTAssertEqual("date=Date:%201970-01-01%20Time:%2000:00:00%20Timezone:%20Z", resultCustom)
        
        let decodedCustom = try URLEncodedFormDecoder(
            configuration: .init(dateDecodingStrategy: .custom({ (decoder) -> Date in
                let container = try decoder.singleValueContainer()
                let string = try container.decode(String.self)
                guard let date = factory.currentValue.date(from: string) else {
                    throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to decode date from string '\(string)'")
                }
                return date
            }))
        ).decode(DateCoding.self, from: resultCustom)
        XCTAssertEqual(decodedCustom, toEncode)
    }

    func testOptionalDateEncodingAndDecoding_GH2518() throws {
        let optionalDate: Date? = Date(timeIntervalSince1970: 0)
        let dateString = "1970-01-01T00:00:00Z"

        let resultForDecodedOptionalDate = try URLEncodedFormDecoder(
            configuration: .init(dateDecodingStrategy: .iso8601)
        ).decode(Date?.self, from: dateString)
        XCTAssertEqual(optionalDate, resultForDecodedOptionalDate)

        let resultForEncodedOptionalDate = try URLEncodedFormEncoder(
            configuration: .init(dateEncodingStrategy: .iso8601)
        ).encode(optionalDate)
        XCTAssertEqual(dateString, resultForEncodedOptionalDate)
    }

    func testEncodedArrayValues() throws {
        let user = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [.baz], nums: [3.14], isCool: true)
        let result = try URLEncodedFormEncoder(
            configuration: .init(arrayEncoding: .values)
        ).encode(user)
        XCTAssert(result.contains("pets=Zizek"))
        XCTAssert(result.contains("pets=Foo"))
        XCTAssert(result.contains("age=23"))
        XCTAssert(result.contains("name=Tanner"))
        XCTAssert(result.contains("dict[a]=1"))
        XCTAssert(result.contains("dict[b]=2"))