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

MockServer: make MockResponse.body a Flow<ByteString> #4096

Merged
merged 3 commits into from
May 12, 2022

Conversation

BoD
Copy link
Contributor

@BoD BoD commented May 11, 2022

In this PR, MockResponse.body becomes a Flow<ByteString> (previously was ByteString). This allows to not treat chunks as a special case.

The JS MockServer was also changed to use a net.Server (instead of http.Server), which allows sharing more common code (previously, MockServerCommon.writeResponse was used on JVM and Apple, but not JS).

@netlify
Copy link

netlify bot commented May 11, 2022

Deploy Preview for apollo-android-docs canceled.

Name Link
🔨 Latest commit 159e216
🔍 Latest deploy log https://app.netlify.com/sites/apollo-android-docs/deploys/627bddb3adfde60008cc2668

@BoD BoD force-pushed the moskerver-body-simplification branch from 3dafbe5 to 4a2c9b5 Compare May 11, 2022 09:31
import kotlin.test.assertEquals
import kotlin.test.assertTrue

fun assertMockResponse(
mockResponse: MockResponse,
body: ByteString,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding a new parameter, could you do this instead?

  assertEquals(
      mockResponse.body.toList().fold(Buffer()) { buffer, byteString ->
        buffer.write(byteString)
      }.readByteString(),
      httpResponse.body!!.readByteString()
  )

This can suspend so you don't want to use it when delays are involved but I think so far it's only used in non-suspending code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Arh it seemed like a good idea, but it's a bit tricky. When using chunked you pass something like c\r\nFirst chunk\n\r\nc\r\nSecond chunk\r\n0\r\n\r\n to the MockResponse but what you get in the HttpResponse is First chunk\nSecond chunk. So you can't really compare them as-is.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we need a chunk decoder then ? Should be relatively easy to implement with all the okio/kotlin goodness?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah that could work (in fact we already have chunk decoding code here 👍

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

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

👍

@BoD BoD merged commit 7776bc0 into main May 12, 2022
@BoD BoD deleted the moskerver-body-simplification branch May 12, 2022 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants