Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rt): emulate a real response body more closely to help catch subtle codegen bugs #505

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package aws.smithy.kotlin.runtime.smithy.test
import aws.smithy.kotlin.runtime.http.Headers
import aws.smithy.kotlin.runtime.http.HttpBody
import aws.smithy.kotlin.runtime.http.HttpStatusCode
import aws.smithy.kotlin.runtime.http.content.ByteArrayContent
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineBase
import aws.smithy.kotlin.runtime.http.request.HttpRequest
import aws.smithy.kotlin.runtime.http.response.HttpCall
import aws.smithy.kotlin.runtime.http.response.HttpResponse
import aws.smithy.kotlin.runtime.io.SdkByteReadChannel
import aws.smithy.kotlin.runtime.testing.runSuspendTest
import aws.smithy.kotlin.runtime.time.Instant

Expand Down Expand Up @@ -88,7 +88,16 @@ fun <T> httpResponseTest(block: HttpResponseTestBuilder<T>.() -> Unit) = runSusp
}

val body: HttpBody = testBuilder.expected.body?.let {
ByteArrayContent(it.encodeToByteArray())
// emulate a real response stream which typically can only be consumed once
// see: https://github.com/awslabs/aws-sdk-kotlin/issues/356
object : HttpBody.Streaming() {
private var consumed = false
override fun readFrom(): SdkByteReadChannel {
val content = if (consumed) ByteArray(0) else it.encodeToByteArray()
consumed = true
return SdkByteReadChannel(content)
}
}
} ?: HttpBody.Empty
Comment on lines 90 to 101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: While I recognize that we were missing how consume-once streams were handled before, it seems like we'll now miss how replayable streams and simple byte arrays are handled. Is there value in testing both cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response bodies are always going to be a single use stream. At least starting from the engine. What happens in middleware may change this (as we do with wrapped response).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I was confusing this with requests. Never mind then.


val resp = HttpResponse(testBuilder.expected.statusCode, headers, body)
Expand Down