Created
March 4, 2020 02:45
-
-
Save swankjesse/b504eb456474e4c5f47fbbeba1ef0244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2020 Square, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package okhttp3.internal.ws | |
import okio.Buffer | |
import okio.DeflaterSink | |
import okio.InflaterSource | |
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.Test | |
import java.util.zip.Deflater | |
import java.util.zip.Inflater | |
class ResetTest { | |
private val deflatedBuffer = Buffer() | |
private val deflater = Deflater() | |
private val deflaterSink = DeflaterSink(deflatedBuffer, deflater) | |
private val inflaterBuffer = Buffer() | |
private val inflater = Inflater() | |
private val inflaterSource = InflaterSource(inflaterBuffer, inflater) | |
@Test | |
fun inflaterResets() { | |
val deflatedMessages = mutableListOf<Buffer>() | |
deflatedMessages += deflateMessage("abc".repeat(100)) | |
deflater.reset() | |
deflatedMessages += deflateMessage("def".repeat(100)) | |
deflater.reset() | |
assertThat(inflateMessage(deflatedMessages.removeAt(0))) | |
.isEqualTo("abc".repeat(100)) | |
inflater.reset() | |
assertThat(inflateMessage(deflatedMessages.removeAt(0))) | |
.isEqualTo("def".repeat(100)) | |
inflater.reset() | |
} | |
@Test | |
fun inflaterDoesNotReset() { | |
val deflatedMessages = mutableListOf<Buffer>() | |
deflatedMessages += deflateMessage("abc".repeat(100)) | |
deflater.reset() | |
deflatedMessages += deflateMessage("def".repeat(100)) | |
deflater.reset() | |
assertThat(inflateMessage(deflatedMessages.removeAt(0))) | |
.isEqualTo("abc".repeat(100)) | |
assertThat(inflateMessage(deflatedMessages.removeAt(0))) | |
.isEqualTo("def".repeat(100)) | |
} | |
private fun deflateMessage(message: String): Buffer { | |
val contentBuffer = Buffer().writeUtf8(message) | |
deflaterSink.write(contentBuffer, contentBuffer.size) | |
deflaterSink.flush() | |
return Buffer().also { it.writeAll(deflatedBuffer) } | |
} | |
private fun inflateMessage(message: Buffer): String { | |
val targetSize = inflater.bytesRead + message.size | |
inflaterBuffer.writeAll(message) | |
val result = Buffer() | |
while (inflater.bytesRead < targetSize) { | |
check(inflaterSource.read(result, Long.MAX_VALUE) != -1L) | |
} | |
return result.readUtf8() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
inflaterResets()
test passes.The
inflaterDoesNotReset()
test fails like this: