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

Code before:
            .init(value: "baz", parameter: "2;=3"),
        ])
    }

    func testValue_serialize() throws {
        let serializer = HTTPHeaders.DirectiveSerializer.init(directives: [
            [.init(value: "foo"), .init(value: "bar", parameter: "baz")],
            [.init(value: "qux", parameter: "quuz")]
        ])
        XCTAssertEqual(serializer.serialize(), "foo; bar=baz, qux=quuz")
    }

    func testForwarded() throws {
        var headers = HTTPHeaders()
        headers.replaceOrAdd(name: .forwarded, value: "for=192.0.2.60;proto=http;by=203.0.113.43")
        XCTAssertEqual(headers.forwarded.first?.for, "192.0.2.60")
        XCTAssertEqual(headers.forwarded.first?.proto, "http")
        XCTAssertEqual(headers.forwarded.first?.by, "203.0.113.43")
    }

    func testAcceptType() throws {
        var headers = HTTPHeaders()

        // Simple accept type
        do {
            headers.replaceOrAdd(name: .accept, value: "text/html")
            XCTAssertEqual(headers.accept.mediaTypes.count, 1)
        }

        // Complex accept type (used e.g. from safari browser)
        do {
            headers.replaceOrAdd(name: .accept, value: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
            XCTAssertEqual(headers.accept.mediaTypes.count, 4)
            XCTAssertTrue(headers.accept.comparePreference(for: .html, to: .xml) == .orderedDescending)
            XCTAssertEqual(headers.accept.first(where: { $0.mediaType == .xml })?.q, 0.9)
            XCTAssertEqual(headers.accept.last?.q, 0.8)
            XCTAssertTrue(headers.accept.comparePreference(for: .xml, to: .bzip2) == .orderedDescending)
        }
    }

    func testForwarded_quote() throws {
        var headers = HTTPHeaders()
        headers.replaceOrAdd(name: .forwarded, value: #"For="[2001:db8:cafe::17]:4711""#)
        XCTAssertEqual(headers.forwarded.first?.for, "[2001:db8:cafe::17]:4711")
    }

    func testForwarded_multiple() throws {
        var headers = HTTPHeaders()
        headers.replaceOrAdd(name: .forwarded, value: #"for=192.0.2.43, for="[2001:db8:cafe::17]""#)
        XCTAssertEqual(headers.forwarded.map { $0.for }, [
            "192.0.2.43",

Review comment (suggestion):
Just for complete sanity and to avoid future regressions could you add an assert to these tests that ensures that the accepted media types match the ones in the header. E.g.

```swift
XCTAssertTrue(headers.accept.mediaTypes.contains(.html))
```

Code after:
            .init(value: "baz", parameter: "2;=3"),
        ])
    }

    func testValue_serialize() throws {
        let serializer = HTTPHeaders.DirectiveSerializer.init(directives: [
            [.init(value: "foo"), .init(value: "bar", parameter: "baz")],
            [.init(value: "qux", parameter: "quuz")]
        ])
        XCTAssertEqual(serializer.serialize(), "foo; bar=baz, qux=quuz")
    }

    func testForwarded() throws {
        var headers = HTTPHeaders()
        headers.replaceOrAdd(name: .forwarded, value: "for=192.0.2.60;proto=http;by=203.0.113.43")
        XCTAssertEqual(headers.forwarded.first?.for, "192.0.2.60")
        XCTAssertEqual(headers.forwarded.first?.proto, "http")
        XCTAssertEqual(headers.forwarded.first?.by, "203.0.113.43")
    }

    func testAcceptType() throws {
        var headers = HTTPHeaders()

        // Simple accept type
        do {
            headers.replaceOrAdd(name: .accept, value: "text/html")
            XCTAssertEqual(headers.accept.mediaTypes.count, 1)
            XCTAssertTrue(headers.accept.mediaTypes.contains(.html))
        }

        // Complex accept type (used e.g. from safari browser)
        do {
            headers.replaceOrAdd(name: .accept, value: "text/html,application/xhtml+xml,application/xml;q=0.9,image/png;q=0.8")
            XCTAssertEqual(headers.accept.mediaTypes.count, 4)
            XCTAssertTrue(headers.accept.mediaTypes.contains(.html))
            XCTAssertTrue(headers.accept.mediaTypes.contains(.xml))
            XCTAssertTrue(headers.accept.mediaTypes.contains(.png))
            XCTAssertTrue(headers.accept.comparePreference(for: .html, to: .xml) == .orderedDescending)
            XCTAssertEqual(headers.accept.first(where: { $0.mediaType == .xml })?.q, 0.9)
            XCTAssertEqual(headers.accept.first(where: { $0.mediaType == .png })?.q, 0.8)
            XCTAssertTrue(headers.accept.comparePreference(for: .xml, to: .png) == .orderedDescending)
        }
    }

    func testForwarded_quote() throws {
        var headers = HTTPHeaders()
        headers.replaceOrAdd(name: .forwarded, value: #"For="[2001:db8:cafe::17]:4711""#)
        XCTAssertEqual(headers.forwarded.first?.for, "[2001:db8:cafe::17]:4711")
    }

    func testForwarded_multiple() throws {