From 93408222d5942e6c6d036c6f75ebaeb4857c916c Mon Sep 17 00:00:00 2001 From: Adam Harvey Date: Mon, 12 Jun 2023 18:25:06 -0700 Subject: [PATCH] tests: don't base64 encode text/plain request payloads when recording In #6326, `text/plain` request payloads were decoded into... well, plain text, and `record::replay_http` was updated to handle them accordingly. The code that records those request payloads was not updated, however, so any updated test payload will now result in failures, since a base64 encoded string will be compared against a plain text string. This adds the same logic to `record::record_http` to ensure newly recorded cassettes match. --- src/tests/record.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/tests/record.rs b/src/tests/record.rs index 3c7f2d373c7..8a6d96a1ce2 100644 --- a/src/tests/record.rs +++ b/src/tests/record.rs @@ -14,7 +14,7 @@ use std::{ use base64::{engine::general_purpose, Engine}; use futures_channel::oneshot; use futures_util::future; -use http::header::CONTENT_TYPE; +use http::{header::CONTENT_TYPE, HeaderMap, HeaderValue}; use hyper::{ body::to_bytes, server::conn::AddrStream, Body, Error, Request, Response, Server, StatusCode, Uri, @@ -278,7 +278,11 @@ async fn record_http(req: Request, client: Client) -> Result { - let body = String::from_utf8_lossy(&body); - assert_eq!(body, exchange.request.body); - } - _ => { - let body = general_purpose::STANDARD.encode(body); - assert_eq!(body, exchange.request.body); - } + if plain_text { + let body = String::from_utf8_lossy(&body); + assert_eq!(body, exchange.request.body); + } else { + let body = general_purpose::STANDARD.encode(body); + assert_eq!(body, exchange.request.body); } let mut builder = Response::builder(); @@ -394,3 +394,12 @@ fn replay_http( Ok(response) } } + +fn is_plain_text(headers: &HeaderMap) -> bool { + if let Some(header_value) = headers.get(CONTENT_TYPE) { + if let Ok(value) = header_value.to_str() { + return value == "text/plain"; + } + } + false +}