Skip to content
Permalink
Browse files

Initializing HTTPMethod with String picks explicit value over catch a…

…ll RAW(value) (#1329)

Motivation:

When initializing an `HTTPMethod` with a `String` we currently switch over all expected values to pick an explicit value, only to overwrite our found explicit value with a catchall `RAW(value: String)`. This doesn’t make a ton of sense. This went unnoticed because we use the conditional conformance on `RawRepresentable` to be `Equatable`. But `switch`es  don’t use the `Equatable` protocol to match enums. The issue is visible there.

Modifications:

Added a testcase with a switch that fails, if the `.RAW(value: String)` value is used for `”GET”`. Removed the final overwrite in HTTPMethod(rawValue: String). Therefore the test passes.

Result:

We can initialize the HTTPMethod with a String and get an explicit value.
  • Loading branch information
fabianfett authored and Lukasa committed Jan 2, 2020
1 parent 6534927 commit ea5a64978d944567498533a63eb44bf965ba3d45
@@ -1416,6 +1416,5 @@ extension HTTPMethod: RawRepresentable {
default:
self = .RAW(value: rawValue)
}
self = .RAW(value: rawValue)
}
}
@@ -29,6 +29,7 @@ extension HTTPTypesTest {
return [
("testConvertToString", testConvertToString),
("testConvertFromString", testConvertFromString),
("testConvertFromStringToExplicitValue", testConvertFromStringToExplicitValue),
]
}
}
@@ -92,5 +92,16 @@ final class HTTPTypesTest: XCTestCase {
XCTAssertEqual(HTTPMethod(rawValue: "SOURCE"), .SOURCE)
XCTAssertEqual(HTTPMethod(rawValue: "SOMETHINGELSE"), HTTPMethod.RAW(value: "SOMETHINGELSE"))
}

func testConvertFromStringToExplicitValue() {
switch HTTPMethod(rawValue: "GET") {
case .RAW(value: "GET"):
XCTFail("Expected \"GET\" to map to explicit .GET value and not .RAW(value: \"GET\")")
case .GET:
break // everything is awesome
default:
XCTFail("Unexpected case")
}
}
}

0 comments on commit ea5a649

Please sign in to comment.
You can’t perform that action at this time.