forked from http4s/http4s
/
BlazeServerSpec.scala
143 lines (126 loc) · 4.37 KB
/
BlazeServerSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.http4s
package server
package blaze
import cats.effect.IO
import java.net.{HttpURLConnection, URL}
import java.nio.charset.StandardCharsets
import org.http4s.blaze.channel.ChannelOptions
import org.http4s.dsl.io._
import scala.concurrent.duration._
import scala.io.Source
class BlazeServerSpec extends Http4sSpec {
def builder =
BlazeServerBuilder[IO]
.withResponseHeaderTimeout(1.second)
.withExecutionContext(testExecutionContext)
val service: HttpApp[IO] = HttpApp {
case GET -> Root / "thread" / "routing" =>
val thread = Thread.currentThread.getName
Ok(thread)
case GET -> Root / "thread" / "effect" =>
IO(Thread.currentThread.getName).flatMap(Ok(_))
case req @ POST -> Root / "echo" =>
Ok(req.body)
case _ -> Root / "never" =>
IO.never
case _ => NotFound()
}
val serverR =
builder
.bindAny()
.withHttpApp(service)
.resource
withResource(serverR) { server =>
// This should be in IO and shifted but I'm tired of fighting this.
def get(path: String): String =
Source
.fromURL(new URL(s"http://127.0.0.1:${server.address.getPort}$path"))
.getLines
.mkString
// This should be in IO and shifted but I'm tired of fighting this.
def getStatus(path: String): IO[Status] = {
val url = new URL(s"http://127.0.0.1:${server.address.getPort}$path")
for {
conn <- IO(url.openConnection().asInstanceOf[HttpURLConnection])
_ = conn.setRequestMethod("GET")
status <- IO.fromEither(Status.fromInt(conn.getResponseCode()))
} yield status
}
// This too
def post(path: String, body: String): String = {
val url = new URL(s"http://127.0.0.1:${server.address.getPort}$path")
val conn = url.openConnection().asInstanceOf[HttpURLConnection]
val bytes = body.getBytes(StandardCharsets.UTF_8)
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Length", bytes.size.toString)
conn.setDoOutput(true)
conn.getOutputStream.write(bytes)
Source.fromInputStream(conn.getInputStream, StandardCharsets.UTF_8.name).getLines.mkString
}
"A server" should {
"route requests on the service executor" in {
get("/thread/routing") must startWith("http4s-spec-")
}
"execute the service task on the service executor" in {
get("/thread/effect") must startWith("http4s-spec-")
}
"be able to echo its input" in {
val input = """{ "Hello": "world" }"""
post("/echo", input) must startWith(input)
}
"return a 503 if the server doesn't respond" in {
getStatus("/never") must returnValue(Status.ServiceUnavailable)
}
}
}
"ChannelOptions" should {
"default to empty" in {
builder.channelOptions must_== ChannelOptions(Vector.empty)
}
"set socket send buffer size" in {
builder.withSocketSendBufferSize(8192).socketSendBufferSize must beSome(8192)
}
"set socket receive buffer size" in {
builder.withSocketReceiveBufferSize(8192).socketReceiveBufferSize must beSome(8192)
}
"set socket keepalive" in {
builder.withSocketKeepAlive(true).socketKeepAlive must beSome(true)
}
"set socket reuse address" in {
builder.withSocketReuseAddress(true).socketReuseAddress must beSome(true)
}
"set TCP nodelay" in {
builder.withTcpNoDelay(true).tcpNoDelay must beSome(true)
}
"unset socket send buffer size" in {
builder
.withSocketSendBufferSize(8192)
.withDefaultSocketSendBufferSize
.socketSendBufferSize must beNone
}
"unset socket receive buffer size" in {
builder
.withSocketReceiveBufferSize(8192)
.withDefaultSocketReceiveBufferSize
.socketReceiveBufferSize must beNone
}
"unset socket keepalive" in {
builder.withSocketKeepAlive(true).withDefaultSocketKeepAlive.socketKeepAlive must beNone
}
"unset socket reuse address" in {
builder
.withSocketReuseAddress(true)
.withDefaultSocketReuseAddress
.socketReuseAddress must beNone
}
"unset TCP nodelay" in {
builder.withTcpNoDelay(true).withDefaultTcpNoDelay.tcpNoDelay must beNone
}
"overwrite keys" in {
builder
.withSocketSendBufferSize(8192)
.withSocketSendBufferSize(4096)
.socketSendBufferSize must beSome(4096)
}
}
}