From 29c6425c4acff5bf8462c1e3cd6eb8134d5184f8 Mon Sep 17 00:00:00 2001 From: Alan Chiu Date: Wed, 10 May 2023 15:26:01 -0700 Subject: [PATCH] fixup --- .../build/buf/connect/ProtocolClientConfig.kt | 2 +- .../connect/protocols/ConnectInterceptor.kt | 15 ++++++++--- .../protocols/ConnectInterceptorTest.kt | 26 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/library/src/main/kotlin/build/buf/connect/ProtocolClientConfig.kt b/library/src/main/kotlin/build/buf/connect/ProtocolClientConfig.kt index 1d7f8cbf..6315c996 100644 --- a/library/src/main/kotlin/build/buf/connect/ProtocolClientConfig.kt +++ b/library/src/main/kotlin/build/buf/connect/ProtocolClientConfig.kt @@ -39,7 +39,7 @@ class ProtocolClientConfig( val requestCompression: RequestCompression? = null, val enableGet: Boolean = false, val getMaxUrlBytes: Int = 50_000, - val getFallback: Boolean = false, + val getFallback: Boolean = true, // Set of interceptors that should be invoked with requests/responses. interceptors: List<(ProtocolClientConfig) -> Interceptor> = emptyList(), // Compression pools that provide support for the provided `compressionName`, as well as any diff --git a/library/src/main/kotlin/build/buf/connect/protocols/ConnectInterceptor.kt b/library/src/main/kotlin/build/buf/connect/protocols/ConnectInterceptor.kt index e90c7cc0..78485b39 100644 --- a/library/src/main/kotlin/build/buf/connect/protocols/ConnectInterceptor.kt +++ b/library/src/main/kotlin/build/buf/connect/protocols/ConnectInterceptor.kt @@ -78,9 +78,7 @@ internal class ConnectInterceptor( } val serializationStrategy = clientConfig.serializationStrategy val requestCodec = serializationStrategy.codec(request.methodSpec.requestClass) - val isCacheableGetMethod = clientConfig.enableGet && - request.methodSpec.idempotency == Idempotency.NO_SIDE_EFFECTS - if (isCacheableGetMethod) { + if (shouldUseGetMethod(request, finalRequestBody)) { val url = getUrlFromMethodSpec( request, requestCodec, @@ -129,6 +127,17 @@ internal class ConnectInterceptor( ) } + private fun shouldUseGetMethod(request: HTTPRequest, finalRequestBody: Buffer): Boolean { + if (request.methodSpec.idempotency == Idempotency.NO_SIDE_EFFECTS && + clientConfig.enableGet) { + if (clientConfig.getFallback) { + return clientConfig.getMaxUrlBytes < finalRequestBody.size + } + return true + } + return false + } + override fun streamFunction(): StreamFunction { val requestCompression = clientConfig.requestCompression return StreamFunction( diff --git a/library/src/test/kotlin/build/buf/connect/protocols/ConnectInterceptorTest.kt b/library/src/test/kotlin/build/buf/connect/protocols/ConnectInterceptorTest.kt index 204a9fea..3ad8454d 100644 --- a/library/src/test/kotlin/build/buf/connect/protocols/ConnectInterceptorTest.kt +++ b/library/src/test/kotlin/build/buf/connect/protocols/ConnectInterceptorTest.kt @@ -544,4 +544,30 @@ class ConnectInterceptorTest { val completion = result as StreamResult.Complete assertThat(completion.code).isEqualTo(Code.UNKNOWN) } + + @Test + fun getRequestTest() { + /** + * The matrix is the following: + * get enabled: true + * get fallback: true + * max bytes over limit: true + * E: URL is correctly constructed for a GET + * + * get enabled: true + * get fallback: true + * max bytes over limit: false + * E: POST request is made + * + * get enabled: true + * get fallback: false + * max bytes over limit: n/a since we always shove it in the url + * E: URL is correctly constructed with the max bytes = 0 + * + * get enabled: false + * get fallback: n/a since we always use the POST path + * max bytes over limit: n/a since this is not read + * E: POST request is made + */ + } }