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

Code before:
            .response { resp in
                requestError = resp.error
                expectation1.fulfill()
            }

        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"
        ]

Review comment (bug):
This test title doesn't really make sense, since this test is about ensuring the possible content types are sorted, not that the failure happens in the first place. I'd suggest `testThatContentTypeValidationFailureSortsPossibleContentTypes()`.

Code after:
            .response { resp in
                requestError = resp.error
                expectation1.fulfill()
            }

        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"
        ]