From c0dfa23f00d7a18e0ff88fe6d119cbecbe3ddd52 Mon Sep 17 00:00:00 2001 From: Christopher Drury Date: Tue, 5 Sep 2023 09:32:06 -0400 Subject: [PATCH] Fix Request's key missing headers --- .../java/com/squareup/picasso3/Request.kt | 8 ++++++ .../squareup/picasso3/RequestCreatorTest.kt | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/picasso/src/main/java/com/squareup/picasso3/Request.kt b/picasso/src/main/java/com/squareup/picasso3/Request.kt index cf38c237b0..412505d1d7 100644 --- a/picasso/src/main/java/com/squareup/picasso3/Request.kt +++ b/picasso/src/main/java/com/squareup/picasso3/Request.kt @@ -214,6 +214,13 @@ class Request internal constructor(builder: Builder) { val path = data.uri.toString() builder.ensureCapacity(path.length + KEY_PADDING) builder.append(path) + + data.headers?.toMultimap()?.let { + builder + .append(KEY_SEPARATOR) + .append("headers:") + .append(it) + } } else { builder.ensureCapacity(KEY_PADDING) builder.append(data.resourceId) @@ -330,6 +337,7 @@ class Request internal constructor(builder: Builder) { priority = request.priority memoryPolicy = request.memoryPolicy networkPolicy = request.networkPolicy + headers = request.headers } fun hasImage(): Boolean { diff --git a/picasso/src/test/java/com/squareup/picasso3/RequestCreatorTest.kt b/picasso/src/test/java/com/squareup/picasso3/RequestCreatorTest.kt index e8d6ae3b93..c7f171076b 100644 --- a/picasso/src/test/java/com/squareup/picasso3/RequestCreatorTest.kt +++ b/picasso/src/test/java/com/squareup/picasso3/RequestCreatorTest.kt @@ -710,4 +710,29 @@ class RequestCreatorTest { assertThat(actionCaptor.value.request.headers!![CUSTOM_HEADER_NAME]) .isEqualTo(CUSTOM_HEADER_VALUE) } + + @Test fun customHeadersArePartOfRequestKey() { + RequestCreator(picasso, URI_1, 0) + .addHeader(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE) + .into(mockImageViewTarget()) + verify(picasso).enqueueAndSubmit(actionCaptor.capture()) + assertThat(actionCaptor.value.request.key).isEqualTo( + """ + http://example.com/1.png + headers:{cache-control=[no-cache]} + + """.trimIndent() + ) + } + + @Test fun imageViewActionWithCustomHeadersCopiesHeaders() { + RequestCreator(picasso, URI_1, 0) + .addHeader(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE) + .into(mockImageViewTarget()) + verify(picasso).enqueueAndSubmit(actionCaptor.capture()) + + val newRequest = actionCaptor.value.request.newBuilder().build() + + assertThat(newRequest.headers!![CUSTOM_HEADER_NAME]).isEqualTo(CUSTOM_HEADER_VALUE) + } }