Repository: astral-sh/uv
File: crates/uv-publish/src/lib.rs (Rust)

Code before:
    }

    #[tokio::test]
    async fn upload_redirect_different_realm() {
        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/final"))
            .respond_with(
                ResponseTemplate::new(308)
                    .insert_header("Location", "https://different.auth.tld/final/"),
            )
            .mount(&mock_server)
            .await;

        let err = mock_server_upload(&mock_server).await.unwrap_err();

        let mut capture = String::new();
        write_error_chain(&err, &mut capture, "error", AnsiColors::Red).unwrap();

        let capture = capture.replace(&mock_server.uri(), "[SERVER]");
        let capture = anstream::adapter::strip_str(&capture);
        assert_snapshot!(
            &capture,
            @"
        error: Failed to publish `../../test/links/tqdm-4.66.1-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl` to https://different.auth.tld/final/
          Caused by: Redirected URL is not in the same realm. Redirected to: https://different.auth.tld/final/
        "
        );
    }

    /// PyPI returns `application/json` with a `code` field.
    #[tokio::test]
    async fn upload_error_pypi_json() {
        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/final"))
            .respond_with(
                ResponseTemplate::new(400)
                    .insert_header("content-type", "application/json")
                    .set_body_raw(
                        r#"{"message": "The server could not comply with the request since it is either malformed or otherwise incorrect.\n\n\nError: Use 'source' as Python version for an sdist.\n\n", "code": "400 Error: Use 'source' as Python version for an sdist.", "title": "Bad Request"}"#,
                        "application/json",
                    ),
            )
            .mount(&mock_server)
            .await;

        let err = mock_server_upload(&mock_server).await.unwrap_err();

        let mut capture = String::new();
        write_error_chain(&err, &mut capture, "error", AnsiColors::Red).unwrap();

Review comment (suggestion):
Hmm, is this correct? I *think* PyPI returns plain text on its upload endpoint per this:

https://github.com/pypi/warehouse/blob/ed8fc9cdfc7c93be63297eb55e0eb72dfe45858f/warehouse/forklift/legacy.py#L219-L231

(Twine also treats the response from the upload API as if it's plain text.)

Code after:
        Mock::given(method("POST"))
            .and(path("/final"))
            .respond_with(
                ResponseTemplate::new(308)
                    .insert_header("Location", "https://different.auth.tld/final/"),
            )
            .mount(&mock_server)
            .await;

        let err = mock_server_upload(&mock_server).await.unwrap_err();

        let mut capture = String::new();
        write_error_chain(&err, &mut capture, "error", AnsiColors::Red).unwrap();

        let capture = capture.replace(&mock_server.uri(), "[SERVER]");
        let capture = anstream::adapter::strip_str(&capture);
        assert_snapshot!(
            &capture,
            @"
        error: Failed to publish `../../test/links/tqdm-4.66.1-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl` to https://different.auth.tld/final/
          Caused by: Redirected URL is not in the same realm. Redirected to: https://different.auth.tld/final/
        "
        );
    }

    /// PyPI returns `application/json` with a `code` field.
    #[tokio::test]
    async fn upload_error_pypi_json() {
        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/final"))
            .respond_with(
                ResponseTemplate::new(400)
                    .insert_header("content-type", "application/json")
                    .set_body_raw(
                        r#"{"message": "The server could not comply with the request since it is either malformed or otherwise incorrect.\n\n\nError: Use 'source' as Python version for an sdist.\n\n", "code": "400 Error: Use 'source' as Python version for an sdist.", "title": "Bad Request"}"#,
                        "application/json",
                    ),
            )
            .mount(&mock_server)
            .await;

        let err = mock_server_upload(&mock_server).await.unwrap_err();

        let mut capture = String::new();
        write_error_chain(&err, &mut capture, "error", AnsiColors::Red).unwrap();

        let capture = capture.replace(&mock_server.uri(), "[SERVER]");
        let capture = anstream::adapter::strip_str(&capture);
        assert_snapshot!(
            &capture,