package org.meshbay.client import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test import org.meshbay.client.photos.ByteRange class ByteRangeTest { @Test fun `a range is read as the upload asks for it`() { assertEquals(0L..1023L, ByteRange.parse("bytes=0-1023", 5000)) assertEquals(4096L..4999L, ByteRange.parse("bytes=4096-", 5000)) assertEquals("the last chunk may ask past the end", 4096L..4999L, ByteRange.parse("bytes=4096-8191", 5000)) } @Test fun `what cannot be served is refused`() { assertNull(ByteRange.parse(null, 10)) assertNull(ByteRange.parse("bytes=10-20", 10)) assertNull(ByteRange.parse("bytes=5-2", 10)) assertNull(ByteRange.parse("bytes=0-1,4-5", 10)) assertNull(ByteRange.parse("bytes=-5", 10)) assertNull(ByteRange.parse("items=0-1", 10)) } @Test fun `a slice holds exactly those bytes`() { val data = ByteArray(100_000) { (it % 251).toByte() } val got = ByteRange.slice(data.inputStream(), 70_000L..99_999L).readBytes() assertArrayEquals(data.copyOfRange(70_000, 100_000), got) assertArrayEquals(data.copyOfRange(10, 12), ByteRange.slice(data.inputStream(), 10L..11L).readBytes()) } }