Skip to content

Commit

Permalink
Test Downloader with HttpURLConnection backend
Browse files Browse the repository at this point in the history
  • Loading branch information
voidcontext committed Mar 11, 2020
1 parent 3bb551f commit 08f5d0d
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
docker/static-files/100MB.bin*
target/
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
static-files:
build: docker/static-files
ports:
- 8088:80
6 changes: 6 additions & 0 deletions docker/static-files/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM nginx:stable

COPY ./100MB.bin /var/www/100MB.bin
COPY ./nginx.conf /etc/nginx/conf.d/default.conf

CMD ["nginx", "-g", "daemon off;"]
9 changes: 9 additions & 0 deletions docker/static-files/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 80;
server_name _;

root /var/www/;

location / {
}
}
6 changes: 6 additions & 0 deletions docker/static-files/pre_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

dd if=/dev/random of=$DIR/100MB.bin bs=1024 count=1024
shasum $DIR/100MB.bin | awk '{print $1}' > $DIR/100MB.bin.shasum
1 change: 1 addition & 0 deletions fetch-file/src/main/scala/vdx/fetchfile/Downloader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ trait Downloader[F[_]] {
}

object Downloader {

def apply[F[_]: Concurrent: ContextShift]: Downloader[F] = new Downloader[F] {
def fetch(
url: URL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package vdx.fetchfile

import cats.effect.{Resource, Sync}
import java.net.HttpURLConnection

object HttpURLConnectionBackend {
def apply[F[_]: Sync]: Backend[F] =
url => Resource.fromAutoCloseable {
Sync[F].delay {
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"
)
connection.connect()
connection.getInputStream()
}
}
}
20 changes: 2 additions & 18 deletions fetch-file/src/main/scala/vdx/fetchfile/package.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
package vdx

import cats.effect.{Resource, Sync}
import cats.effect.Resource

import java.net.URL
import java.io.InputStream
import java.net.{HttpURLConnection, URL}

package object fetchfile {

type Backend[F[_]] = URL => Resource[F, InputStream]

object Backend {
implicit def httpUrlConnectionBackend[F[_]: Sync]: Backend[F] =
url => Resource.fromAutoCloseable {
Sync[F].delay {
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"
)
connection.connect()
connection.getInputStream()
}
}

}
}
35 changes: 33 additions & 2 deletions fetch-file/src/test/scala/vdx/fetchfile/DownloaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import fs2._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.io.{ByteArrayInputStream}
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.net.URL
import java.io.ByteArrayOutputStream
import scala.io.Source
import java.security.MessageDigest

class DownloaderSpec extends AnyFlatSpec with Matchers {

implicit val cs: ContextShift[IO] = IO.contextShift(scala.concurrent.ExecutionContext.global)

"fetch" should "use the given backend to create the input stream" in {
Expand All @@ -34,5 +36,34 @@ class DownloaderSpec extends AnyFlatSpec with Matchers {
} yield content
}).unsafeRunSync() should be("http://example.com/test.file")
}

it should "download the file correctly with the provided backend" in {
val downloader = Downloader[IO]

val downloadedBytes = (Blocker[IO].use { blocker =>
implicit val backend = HttpURLConnectionBackend[IO]
val out = new ByteArrayOutputStream()
for {
_ <- downloader.fetch(
new URL("http://localhost:8088/100MB.bin"),
Resource.fromAutoCloseable(IO.delay(out)),
blocker,
1024 * 64,
(s: Stream[IO, Byte]) => s.map(_ => ())
)
content <- IO.delay(out.toByteArray())
} yield content
}).unsafeRunSync()

downloadedBytes.length should be(1024 * 1024)
val expectedShaSum = Source.fromFile("docker/static-files/100MB.bin.shasum").mkString.trim()

val shaSum = MessageDigest.getInstance("SHA-1")
.digest(downloadedBytes)
.map("%02x".format(_))
.mkString

shaSum should be(expectedShaSum)
}
}

0 comments on commit 08f5d0d

Please sign in to comment.