forked from http4s/http4s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Http1ClientStageSpec.scala
319 lines (255 loc) · 9.72 KB
/
Http1ClientStageSpec.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package org.http4s
package client
package blaze
import cats.effect._
import cats.effect.concurrent.Deferred
import cats.implicits._
import fs2.Stream
import fs2.concurrent.Queue
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import org.http4s.blaze.pipeline.LeafBuilder
import org.http4s.blazecore.{QueueTestHead, SeqTestHead}
import org.http4s.client.blaze.bits.DefaultUserAgent
import org.http4s.headers.`User-Agent`
import scala.concurrent.duration._
class Http1ClientStageSpec extends Http4sSpec {
val trampoline = org.http4s.blaze.util.Execution.trampoline
val www_foo_test = Uri.uri("http://www.foo.test")
val FooRequest = Request[IO](uri = www_foo_test)
val FooRequestKey = RequestKey.fromRequest(FooRequest)
val LongDuration = 30.seconds
// Common throw away response
val resp = "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndone"
private def mkConnection(key: RequestKey, userAgent: Option[`User-Agent`] = None) =
new Http1Connection[IO](
key,
executionContext = trampoline,
maxResponseLineSize = 4096,
maxHeaderLength = 40960,
maxChunkSize = Int.MaxValue,
chunkBufferMaxSize = 1024,
parserMode = ParserMode.Strict,
userAgent = userAgent
)
private def mkBuffer(s: String): ByteBuffer =
ByteBuffer.wrap(s.getBytes(StandardCharsets.ISO_8859_1))
private def bracketResponse[T](req: Request[IO], resp: String)(
f: Response[IO] => IO[T]): IO[T] = {
val stage = mkConnection(FooRequestKey)
IO.suspend {
val h = new SeqTestHead(resp.toSeq.map { chr =>
val b = ByteBuffer.allocate(1)
b.put(chr.toByte).flip()
b
})
LeafBuilder(stage).base(h)
for {
resp <- stage.runRequest(req, IO.never)
t <- f(resp)
_ <- IO(stage.shutdown())
} yield t
}
}
private def getSubmission(
req: Request[IO],
resp: String,
stage: Http1Connection[IO]): IO[(String, String)] =
for {
q <- Queue.unbounded[IO, Option[ByteBuffer]]
h = new QueueTestHead(q)
d <- Deferred[IO, Unit]
_ <- IO(LeafBuilder(stage).base(h))
_ <- (d.get >> Stream
.emits(resp.toList)
.map { c =>
val b = ByteBuffer.allocate(1)
b.put(c.toByte).flip()
b
}
.noneTerminate
.to(q.enqueue)
.compile
.drain).start
req0 = req.withBodyStream(req.body.onFinalize(d.complete(())))
response <- stage.runRequest(req0, IO.never)
result <- response.as[String]
_ <- IO(h.stageShutdown())
buff <- IO.fromFuture(IO(h.result))
request = new String(buff.array(), StandardCharsets.ISO_8859_1)
} yield (request, result)
private def getSubmission(
req: Request[IO],
resp: String,
userAgent: Option[`User-Agent`] = None): IO[(String, String)] = {
val key = RequestKey.fromRequest(req)
val tail = mkConnection(key, userAgent)
getSubmission(req, resp, tail)
}
"Http1ClientStage" should {
"Run a basic request" in {
val (request, response) = getSubmission(FooRequest, resp).unsafeRunSync()
val statusline = request.split("\r\n").apply(0)
statusline must_== "GET / HTTP/1.1"
response must_== "done"
}
"Submit a request line with a query" in {
val uri = "/huh?foo=bar"
val Right(parsed) = Uri.fromString("http://www.foo.test" + uri)
val req = Request[IO](uri = parsed)
val (request, response) = getSubmission(req, resp).unsafeRunSync()
val statusline = request.split("\r\n").apply(0)
statusline must_== "GET " + uri + " HTTP/1.1"
response must_== "done"
}
"Fail when attempting to get a second request with one in progress" in {
val tail = mkConnection(FooRequestKey)
val (frag1, frag2) = resp.splitAt(resp.length - 1)
val h = new SeqTestHead(List(mkBuffer(frag1), mkBuffer(frag2), mkBuffer(resp)))
LeafBuilder(tail).base(h)
try {
tail.runRequest(FooRequest, IO.never).unsafeRunAsync {
case Right(_) => (); case Left(_) => ()
} // we remain in the body
tail
.runRequest(FooRequest, IO.never)
.unsafeRunSync() must throwA[Http1Connection.InProgressException.type]
} finally {
tail.shutdown()
}
}
"Reset correctly" in {
val tail = mkConnection(FooRequestKey)
try {
val h = new SeqTestHead(List(mkBuffer(resp), mkBuffer(resp)))
LeafBuilder(tail).base(h)
// execute the first request and run the body to reset the stage
tail.runRequest(FooRequest, IO.never).unsafeRunSync().body.compile.drain.unsafeRunSync()
val result = tail.runRequest(FooRequest, IO.never).unsafeRunSync()
tail.shutdown()
result.headers.size must_== 1
} finally {
tail.shutdown()
}
}
"Alert the user if the body is to short" in {
val resp = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\ndone"
val tail = mkConnection(FooRequestKey)
try {
val h = new SeqTestHead(List(mkBuffer(resp)))
LeafBuilder(tail).base(h)
val result = tail.runRequest(FooRequest, IO.never).unsafeRunSync()
result.body.compile.drain.unsafeRunSync() must throwA[InvalidBodyException]
} finally {
tail.shutdown()
}
}
"Interpret a lack of length with a EOF as a valid message" in {
val resp = "HTTP/1.1 200 OK\r\n\r\ndone"
val (_, response) = getSubmission(FooRequest, resp).unsafeRunSync()
response must_== "done"
}
"Utilize a provided Host header" in {
val resp = "HTTP/1.1 200 OK\r\n\r\ndone"
val req = FooRequest.withHeaders(headers.Host("bar.test"))
val (request, response) = getSubmission(req, resp).unsafeRunSync()
val requestLines = request.split("\r\n").toList
requestLines must contain("Host: bar.test")
response must_== "done"
}
"Insert a User-Agent header" in {
val resp = "HTTP/1.1 200 OK\r\n\r\ndone"
val (request, response) = getSubmission(FooRequest, resp, DefaultUserAgent).unsafeRunSync()
val requestLines = request.split("\r\n").toList
requestLines must contain(s"User-Agent: http4s-blaze/${BuildInfo.version}")
response must_== "done"
}
"Use User-Agent header provided in Request" in {
val resp = "HTTP/1.1 200 OK\r\n\r\ndone"
val req = FooRequest.withHeaders(Header.Raw("User-Agent".ci, "myagent"))
val (request, response) = getSubmission(req, resp).unsafeRunSync()
val requestLines = request.split("\r\n").toList
requestLines must contain("User-Agent: myagent")
response must_== "done"
}
"Not add a User-Agent header when configured with None" in {
val resp = "HTTP/1.1 200 OK\r\n\r\ndone"
val tail = mkConnection(FooRequestKey)
try {
val (request, response) = getSubmission(FooRequest, resp, tail).unsafeRunSync()
tail.shutdown()
val requestLines = request.split("\r\n").toList
requestLines.find(_.startsWith("User-Agent")) must beNone
response must_== "done"
} finally {
tail.shutdown()
}
}
// TODO fs2 port - Currently is elevating the http version to 1.1 causing this test to fail
"Allow an HTTP/1.0 request without a Host header" in {
val resp = "HTTP/1.0 200 OK\r\n\r\ndone"
val req = Request[IO](uri = www_foo_test, httpVersion = HttpVersion.`HTTP/1.0`)
val (request, response) = getSubmission(req, resp).unsafeRunSync()
request must not contain "Host:"
response must_== "done"
}.pendingUntilFixed
"Support flushing the prelude" in {
val req = Request[IO](uri = www_foo_test, httpVersion = HttpVersion.`HTTP/1.0`)
/*
* We flush the prelude first to test connection liveness in pooled
* scenarios before we consume the body. Make sure we can handle
* it. Ensure that we still get a well-formed response.
*/
val (_, response) = getSubmission(req, resp).unsafeRunSync()
response must_== "done"
}
"Not expect body if request was a HEAD request" in {
val contentLength = 12345L
val resp = s"HTTP/1.1 200 OK\r\nContent-Length: $contentLength\r\n\r\n"
val headRequest = FooRequest.withMethod(Method.HEAD)
val tail = mkConnection(FooRequestKey)
try {
val h = new SeqTestHead(List(mkBuffer(resp)))
LeafBuilder(tail).base(h)
val response = tail.runRequest(headRequest, IO.never).unsafeRunSync()
response.contentLength must beSome(contentLength)
// connection reusable immediately after headers read
tail.isRecyclable must_=== true
// body is empty due to it being HEAD request
response.body.compile.toVector
.unsafeRunSync()
.foldLeft(0L)((long, byte) => long + 1L) must_== 0L
} finally {
tail.shutdown()
}
}
{
val resp = "HTTP/1.1 200 OK\r\n" +
"Transfer-Encoding: chunked\r\n\r\n" +
"3\r\n" +
"foo\r\n" +
"0\r\n" +
"Foo:Bar\r\n" +
"\r\n"
val req = Request[IO](uri = www_foo_test, httpVersion = HttpVersion.`HTTP/1.1`)
"Support trailer headers" in {
val hs: IO[Headers] = bracketResponse(req, resp) { response: Response[IO] =>
for {
_ <- response.as[String]
hs <- response.trailerHeaders
} yield hs
}
hs.unsafeRunSync().mkString must_== "Foo: Bar"
}
"Fail to get trailers before they are complete" in {
val hs: IO[Headers] = bracketResponse(req, resp) { response: Response[IO] =>
for {
//body <- response.as[String]
hs <- response.trailerHeaders
} yield hs
}
hs.unsafeRunSync() must throwA[IllegalStateException]
}
}
}
}