Skip to content

Commit

Permalink
HTTPMediaType comparisons should be case insensitive (#2326)
Browse files Browse the repository at this point in the history
  • Loading branch information
grosch committed Apr 21, 2020
1 parent a844a2d commit 9b4ebf7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 4 additions & 12 deletions Sources/Vapor/HTTP/Headers/HTTPMediaType.swift
Expand Up @@ -53,20 +53,12 @@ public struct HTTPMediaType: Hashable, CustomStringConvertible, Equatable {
guard lhs.type != "*" && rhs.type != "*" else {
return true
}

guard lhs.type != rhs.type else {
guard lhs.subType != "*" && rhs.subType != "*" else {
return true
}

guard lhs.subType != rhs.subType else {
return true
}


guard lhs.type.caseInsensitiveCompare(rhs.type) == .orderedSame else {
return false
}
return false

return lhs.subType == "*" || rhs.subType == "*" || lhs.subType.caseInsensitiveCompare(rhs.subType) == .orderedSame
}

/// The `MediaType`'s discrete or composite type. Usually one of the following.
Expand Down
12 changes: 12 additions & 0 deletions Tests/VaporTests/HTTPHeaderTests.swift
Expand Up @@ -175,4 +175,16 @@ final class HTTPHeaderValueTests: XCTestCase {
XCTAssertEqual(headers.cookie?["vapor-session"]?.string, "ZFPQ46p3frNX52i3dM+JFlWbTxQX5rtGuQ5r7Gb6JUs=")
XCTAssertEqual(headers.cookie?["oauth2_consent_csrf"]?.string, "MTU4NjkzNzgwMnxEdi1CQkFFQ180SUFBUkFCRUFBQVB2LUNBQUVHYzNSeWFXNW5EQVlBQkdOemNtWUdjM1J5YVc1bkRDSUFJR1ExWVRnM09USmhOamRsWXpSbU4yRmhOR1UwTW1KaU5tRXpPRGczTmpjMHweHbVecAf193ev3_1Tcf60iY9jSsq5-IQxGTyoztRTfg==")
}

func testMediaTypeMainTypeCaseInsensitive() throws {
let lower = HTTPMediaType(type: "foo", subType: "")
let upper = HTTPMediaType(type: "FOO", subType: "")
XCTAssertEqual(lower, upper)
}

func testMediaTypeSubTypeCaseInsensitive() throws {
let lower = HTTPMediaType(type: "foo", subType: "bar")
let upper = HTTPMediaType(type: "foo", subType: "BAR")
XCTAssertEqual(lower, upper)
}
}

0 comments on commit 9b4ebf7

Please sign in to comment.