Repository: Alamofire/Alamofire
File: Tests/ValidationTests.swift (Swift)

Code before:

        AF.download(endpoint)
            .validate(contentType: ["application/octet-stream"])
            .response { resp in
                downloadError = resp.error
                expectation2.fulfill()
            }

        waitForExpectations(timeout: timeout)

        // Then
        XCTAssertNotNil(requestError)
        XCTAssertNotNil(downloadError)

        for error in [requestError, downloadError] {
            XCTAssertEqual(error?.isUnacceptableContentType, true)
            XCTAssertEqual(error?.responseContentType, "application/xml")
            XCTAssertEqual(error?.acceptableContentTypes?.first, "application/octet-stream")
        }
    }

    func testThatValidationForRequestWithUnacceptableContentTypeResponseFailsWhenMultipleContentTypesAreAcceptable() {
        // Given
        let endpoint = Endpoint.xml

        let expectation1 = expectation(description: "request should succeed and return xml")
        let expectation2 = expectation(description: "download should succeed and return xml")

        var requestError: AFError?
        var downloadError: AFError?

        let acceptableContentTypes = [ // Sorted in a random order, not alphabetically
            "application/octet-stream",
            "image/gif",
            "image/x-xbitmap",
            "image/tiff",
            "image/jpg",
            "image/x-bmp",
            "image/jpeg",
            "image/x-icon",
            "image/jp2",
            "image/png",
            "image/ico",
            "image/bmp",
            "image/x-ms-bmp",
            "image/x-win-bitmap"
        ]

        // When
        AF.request(endpoint)
            .validate(contentType: acceptableContentTypes)

Review comment (suggestion):
These can be given better names. Perhaps `request1DidComplete` and `request2DidComplete`? And update the `description`s to indicate which request it's talking about.

Code after:

        AF.download(endpoint)
            .validate(contentType: ["application/octet-stream"])
            .response { resp in
                downloadError = resp.error
                expectation2.fulfill()
            }

        waitForExpectations(timeout: timeout)

        // Then
        XCTAssertNotNil(requestError)
        XCTAssertNotNil(downloadError)

        for error in [requestError, downloadError] {
            XCTAssertEqual(error?.isUnacceptableContentType, true)
            XCTAssertEqual(error?.responseContentType, "application/xml")
            XCTAssertEqual(error?.acceptableContentTypes?.first, "application/octet-stream")
        }
    }

    func testThatContentTypeValidationFailureSortsPossibleContentTypes() {
        // Given
        let endpoint = Endpoint.xml

        let requestDidCompleteExpectation = expectation(description: "request should succeed and return xml")
        let downloadDidCompleteExpectation = expectation(description: "download should succeed and return xml")

        var requestError: AFError?
        var downloadError: AFError?

        let acceptableContentTypes = [ // Sorted in a random order, not alphabetically
            "application/octet-stream",
            "image/gif",
            "image/x-xbitmap",
            "image/tiff",
            "image/jpg",
            "image/x-bmp",
            "image/jpeg",
            "image/x-icon",
            "image/jp2",
            "image/png",
            "image/ico",
            "image/bmp",
            "image/x-ms-bmp",
            "image/x-win-bitmap"
        ]

        // When
        AF.request(endpoint)
            .validate(contentType: acceptableContentTypes)