-
Notifications
You must be signed in to change notification settings - Fork 398
/
ServerFilesTests.scala
718 lines (699 loc) · 30.8 KB
/
ServerFilesTests.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package sttp.tapir.server.tests
import cats.data.NonEmptyList
import cats.effect.unsafe.implicits.global
import cats.effect.{IO, Resource}
import cats.syntax.all._
import org.scalatest.matchers.should.Matchers._
import sttp.capabilities.WebSockets
import sttp.capabilities.fs2.Fs2Streams
import sttp.client3._
import sttp.model._
import sttp.tapir._
import sttp.tapir.files._
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.files.FilesOptions
import sttp.tapir.server.interceptor.CustomiseInterceptors
import sttp.tapir.server.interceptor.decodefailure.DefaultDecodeFailureHandler
import sttp.tapir.tests._
import java.io.File
import java.nio.file.{Files, StandardOpenOption}
import java.util.Comparator
import scala.concurrent.Future
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPOutputStream
import java.util.zip.GZIPInputStream
import java.io.ByteArrayInputStream
class ServerFilesTests[F[_], OPTIONS, ROUTE](
serverInterpreter: TestServerInterpreter[F, Any, OPTIONS, ROUTE],
backend: SttpBackend[IO, Fs2Streams[IO] with WebSockets],
supportSettingContentLength: Boolean = true
) {
private def get(port: Int, path: List[String]): IO[Response[String]] = basicRequest
.get(uri"http://localhost:$port/$path")
.response(asStringAlways)
.send(backend)
private def head(port: Int, path: List[String]): IO[Response[String]] = basicRequest
.head(uri"http://localhost:$port/$path")
.response(asStringAlways)
.send(backend)
private val classLoader = classOf[ServerFilesTests[F, OPTIONS, ROUTE]].getClassLoader
def tests(): List[Test] = {
val baseTests = List(
Test("should serve files from the given system path") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F](emptyInput)(testDir.getAbsolutePath))
.use { port =>
get(port, "f1" :: Nil).map(_.body shouldBe "f1 content") >>
get(port, "f2" :: Nil).map(_.body shouldBe "f2 content") >>
get(port, "d1" :: "f3" :: Nil).map(_.body shouldBe "f3 content") >>
get(port, "d1" :: "d2" :: "f4" :: Nil).map(_.body shouldBe "f4 content")
}
.unsafeToFuture()
}
},
Test("should serve files from the given system path with a prefix") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("static" / "content")(testDir.getAbsolutePath))
.use { port =>
get(port, "static" :: "content" :: "f1" :: Nil).map(_.body shouldBe "f1 content") >>
get(port, "static" :: "content" :: "d1" :: "f3" :: Nil).map(_.body shouldBe "f3 content")
}
.unsafeToFuture()
}
},
Test("should serve index.html when a directory is requested") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F](emptyInput)(testDir.getAbsolutePath))
.use { port =>
get(port, List("d1")).map(_.body shouldBe "index content")
}
.unsafeToFuture()
}
},
Test("should serve files from the given system path with filter") {
withTestFilesDirectory { testDir =>
serveRoute(
staticFilesGetServerEndpoint[F](emptyInput)(testDir.getAbsolutePath, FilesOptions.default.fileFilter(_.exists(_.contains("2"))))
)
.use { port =>
get(port, "f1" :: Nil).map(_.code shouldBe StatusCode.NotFound) >>
get(port, "f2" :: Nil).map(_.body shouldBe "f2 content") >>
get(port, "d1" :: "f3" :: Nil).map(_.code shouldBe StatusCode.NotFound) >>
get(port, "d1" :: "d2" :: "f4" :: Nil).map(_.body shouldBe "f4 content")
}
.unsafeToFuture()
}
},
Test("should return acceptRanges for file head request") {
withTestFilesDirectory { testDir =>
val file = testDir.toPath.resolve("f1").toFile
serveRoute(staticFilesHeadServerEndpoint("test")(testDir.getAbsolutePath))
.use { port =>
head(port, List("test", "f1"))
.map { r =>
r.code shouldBe StatusCode.Ok
r.headers contains Header(HeaderNames.AcceptRanges, ContentRangeUnits.Bytes) shouldBe true
r.headers contains Header(HeaderNames.ContentLength, file.length().toString) shouldBe true
}
}
.unsafeToFuture()
}
},
Test("should handle ranged HEAD request like a GET request") {
withTestFilesDirectory { testDir =>
val file = testDir.toPath.resolve("f1").toFile
serveRoute(staticFilesHeadServerEndpoint("test")(testDir.getAbsolutePath))
.use { port =>
head(port, List("test", "f1"))
.map { r =>
r.code shouldBe StatusCode.Ok
r.headers contains Header(HeaderNames.AcceptRanges, ContentRangeUnits.Bytes) shouldBe true
r.headers contains Header(HeaderNames.ContentLength, file.length().toString) shouldBe true
}
}
.unsafeToFuture()
}
},
Test("should return acceptRanges for resource head request") {
serveRoute(staticResourcesHeadServerEndpoint(emptyInput)(classLoader, "test/r3.txt"))
.use { port =>
head(port, List(""))
.map { r =>
r.code shouldBe StatusCode.Ok
r.headers contains Header(HeaderNames.AcceptRanges, ContentRangeUnits.Bytes) shouldBe true
r.headers contains Header(HeaderNames.ContentLength, "10") shouldBe true
}
}
.unsafeToFuture()
},
Test("should return 404 when files are not found") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port => get(port, List("test")).map(_.code shouldBe StatusCode.NotFound) }
.unsafeToFuture()
}
},
Test("should return 404 for HEAD request and not existing file ") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesHeadServerEndpoint(emptyInput)(testDir.getAbsolutePath))
.use { port => head(port, List("test")).map(_.code shouldBe StatusCode.NotFound) }
.unsafeToFuture()
}
},
Test("should return default file when file is not found") {
withTestFilesDirectory { testDir =>
serveRoute(
staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath, FilesOptions.default.defaultFile(List("d1", "index.html")))
)
.use { port =>
get(port, List("test", "f10")).map(_.body shouldBe "index content") >>
get(port, List("test", "f1")).map(_.body shouldBe "f1 content")
}
.unsafeToFuture()
}
},
Test("should return pre-gzipped files") {
// https://github.com/softwaremill/tapir/issues/2869
// https://github.com/softwaremill/tapir/issues/2929
withTestFilesDirectory { testDir =>
serveRoute(
staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath, FilesOptions.default.withUseGzippedIfAvailable)
)
.use { port =>
val testCases =
for (acceptEncodingValue <- List("gzip, deflate", "gzip", "deflate, gzip;q=1.0, *;q=0.5"))
yield emptyRequest
.acceptEncoding(acceptEncodingValue)
.get(uri"http://localhost:$port/test/img.gif")
.response(asStringAlways)
.send(backend)
.map(r => {
r.code shouldBe StatusCode.Ok
r.body shouldBe "img gzipped content"
r.headers should contain(Header(HeaderNames.ContentEncoding, "gzip"))
r.headers should contain(Header(HeaderNames.ContentType, MediaType.ImageGif.toString()))
})
testCases.sequence.map(_.last)
}
.unsafeToFuture()
}
},
Test("should return compressed file directly") {
// https://github.com/softwaremill/tapir/issues/2929
withTestFilesDirectory { testDir =>
serveRoute(
staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath, FilesOptions.default.withUseGzippedIfAvailable)
)
.use { port =>
val testCases =
for (acceptEncodingValue <- List("gzip, deflate", "gzip", "deflate, gzip;q=1.0, *;q=0.5"))
yield emptyRequest
.acceptEncoding(acceptEncodingValue)
.get(uri"http://localhost:$port/test/img.gif.gz")
.response(asByteArrayAlways)
.send(backend)
.map(r => {
r.code shouldBe StatusCode.Ok
decompress(r.body) shouldBe "img gzipped content"
r.headers should contain(Header(HeaderNames.ContentType, MediaType.ApplicationGzip.toString()))
})
testCases.sequence.map(_.last)
}
.unsafeToFuture()
}
},
Test("should return whole while file if range header not present") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port => get(port, List("test", "f2")).map(_.body shouldBe "f2 content") }
.unsafeToFuture()
}
},
Test("should return 200 status code for whole file") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port => get(port, List("test", "f2")).map(_.code shouldBe StatusCode.Ok) }
.unsafeToFuture()
}
},
Test("should return 416 if over range") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=0-11"))
.get(uri"http://localhost:$port/test/f2")
.response(asStringAlways)
.send(backend)
.map(_.code shouldBe StatusCode.RangeNotSatisfiable)
}
.unsafeToFuture()
}
},
Test("should return content range header with matching bytes") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=1-3"))
.get(uri"http://localhost:$port/test/f1")
.response(asStringAlways)
.send(backend)
.map(_.headers contains Header(HeaderNames.ContentRange, "bytes 1-3/10") shouldBe true)
}
.unsafeToFuture()
}
},
Test("should return 206 status code for partial content") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=1-3"))
.get(uri"http://localhost:$port/test/f1")
.response(asStringAlways)
.send(backend)
.map(_.code shouldBe StatusCode.PartialContent)
}
.unsafeToFuture()
}
},
Test("should return bytes 4-7 from file") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=4-7"))
.get(uri"http://localhost:$port/test/f1")
.response(asStringAlways)
.send(backend)
.map(_.body shouldBe "onte")
}
.unsafeToFuture()
}
},
Test("should return bytes 100000-200000 from file") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=100000-200000"))
.get(uri"http://localhost:$port/test/f5")
.response(asStringAlways)
.send(backend)
.map { r =>
r.body.length shouldBe 100001
r.body.head shouldBe 'x'
}
}
.unsafeToFuture()
}
},
Test("should return bytes from 100000 from file") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=100000-"))
.get(uri"http://localhost:$port/test/f5")
.response(asStringAlways)
.send(backend)
.map { r =>
r.body.length shouldBe 200000
r.body.head shouldBe 'x'
}
}
.unsafeToFuture()
}
},
Test("should return last 100000 bytes from file") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=-100000"))
.get(uri"http://localhost:$port/test/f5")
.response(asStringAlways)
.send(backend)
.map { r =>
r.body.length shouldBe 100000
r.body.head shouldBe 'x'
}
}
.unsafeToFuture()
}
},
Test("should fail for incorrect range") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F]("test")(testDir.getAbsolutePath))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=-"))
.get(uri"http://localhost:$port/test/f5")
.response(asStringAlways)
.send(backend)
.map { _.code shouldBe StatusCode.BadRequest }
}
.unsafeToFuture()
}
},
Test("should return file metadata") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F](emptyInput)(testDir.getAbsolutePath))
.use { port =>
get(port, List("img.gif")) map { r =>
r.contentLength shouldBe Some(11)
r.contentType shouldBe Some(MediaType.ImageGif.toString())
r.header(HeaderNames.LastModified)
.flatMap(t => Header.parseHttpDate(t).toOption)
.map(_.toEpochMilli)
.get should be > (System.currentTimeMillis() - 10000L)
r.header(HeaderNames.Etag).isDefined shouldBe true
}
}
.unsafeToFuture()
}
},
Test("should not return a file outside of the system path") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F](emptyInput)(testDir.getAbsolutePath + "/d1"))
.use { port => get(port, List("..", "f1")).map(_.body should not be "f1 content") }
.unsafeToFuture()
}
},
Test("should not return a file outside of the system path, when the path is given as a single segment") {
withTestFilesDirectory { testDir =>
serveRoute(staticFilesGetServerEndpoint[F](emptyInput)(testDir.getAbsolutePath + "/d1"))
.use { port => get(port, List("../f1")).map(_.body should not be "f1 content") }
.unsafeToFuture()
}
},
Test("should serve a single resource") {
serveRoute(staticResourceGetServerEndpoint[F](emptyInput)(classLoader, "test/r1.txt"))
.use { port => get(port, Nil).map(_.body shouldBe "Resource 1") }
.unsafeToFuture()
},
Test("should serve a single resource range") {
serveRoute(staticResourceGetServerEndpoint[F](emptyInput)(classLoader, "test/r1.txt"))
.use { port =>
basicRequest
.get(uri"http://localhost:$port")
.headers(Header(HeaderNames.Range, "bytes=6-8"))
.response(asStringAlways)
.send(backend)
.map(_.body shouldBe "ce ")
}
.unsafeToFuture()
},
Test("should serve single pre-gzipped resource") {
serveRoute(
staticResourceGetServerEndpoint[F](emptyInput)(classLoader, "test/r3.txt", FilesOptions.default.withUseGzippedIfAvailable)
)
.use { port =>
emptyRequest
.acceptEncoding("gzip")
.get(uri"http://localhost:$port")
.response(asStringAlways)
.send(backend)
.map(r => {
r.code shouldBe StatusCode.Ok
r.body shouldBe "Gzipped resource"
r.headers should contain(Header(HeaderNames.ContentEncoding, "gzip"))
r.headers should contain (Header(HeaderNames.ContentType, MediaType.TextPlain.toString()))
})
}
.unsafeToFuture()
},
Test("should return 404 for resources without extension") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port =>
emptyRequest
.get(uri"http://localhost:$port/r3")
.response(asStringAlways)
.send(backend)
.map(_.code shouldBe StatusCode.NotFound)
}
.unsafeToFuture()
},
Test("should serve resource at path for preGzipped endpoint without correct header") {
serveRoute(
staticResourceGetServerEndpoint[F](emptyInput)(classLoader, "test/r3.txt", FilesOptions.default.withUseGzippedIfAvailable)
)
.use { port =>
emptyRequest
.get(uri"http://localhost:$port")
.response(asStringAlways)
.send(backend)
.map(_.body shouldBe "Resource 3")
}
.unsafeToFuture()
},
Test("should not return a resource outside of the resource prefix directory") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port => get(port, List("..", "test", "r5.txy")).map(_.body should not be "Resource 5") }
.unsafeToFuture()
},
Test("should not return a resource outside of the resource prefix directory, when the path is given as a single segment") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port => get(port, List("../test2/r5.txt")).map(_.body should not be "Resource 5") }
.unsafeToFuture()
},
Test("should serve resources") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port =>
get(port, "r1.txt" :: Nil).map(_.body shouldBe "Resource 1") >>
get(port, "r2.txt" :: Nil).map(_.body shouldBe "Resource 2") >>
get(port, "d1" :: "r3.txt" :: Nil).map(_.body shouldBe "Resource 3") >>
get(port, "d1" :: "d2" :: "r4.txt" :: Nil).map(_.body shouldBe "Resource 4")
}
.unsafeToFuture()
},
Test("should serve resources with filter") {
serveRoute(
staticResourcesGetServerEndpoint[F](emptyInput)(
classLoader,
"test",
FilesOptions.default.fileFilter(_.exists(_.contains("2")))
)
)
.use { port =>
get(port, "r1.txt" :: Nil).map(_.code shouldBe StatusCode.NotFound) >>
get(port, "r2.txt" :: Nil).map(_.body shouldBe "Resource 2") >>
get(port, "d1" :: "r3.txt" :: Nil).map(_.code shouldBe StatusCode.NotFound) >>
get(port, "d1" :: "d2" :: "r4.txt" :: Nil).map(_.body shouldBe "Resource 4")
}
.unsafeToFuture()
},
Test("should return 404 when a resource is not found") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port => get(port, List("r3")).map(_.code shouldBe StatusCode.NotFound) }
.unsafeToFuture()
},
Test("should return default resource and its etag, when resource is not found") {
serveRoute(
staticResourcesGetServerEndpoint[F]("test")(classLoader, "test", FilesOptions.default.defaultFile(List("d1", "r3.txt")))
)
.use { port =>
get(port, List("test", "r10.txt")).map { resp =>
resp.body shouldBe "Resource 3"
resp.header(HeaderNames.Etag).get should endWith("-a\"")
} >>
get(port, List("test", "r1.txt")).map(_.body shouldBe "Resource 1")
}
.unsafeToFuture()
},
Test("should serve a single file from the given system path") {
withTestFilesDirectory { testDir =>
serveRoute(staticFileGetServerEndpoint[F]("test")(testDir.toPath.resolve("f1").toFile.getAbsolutePath))
.use { port => get(port, List("test")).map(_.body shouldBe "f1 content") }
.unsafeToFuture()
}
},
Test("if an etag is present, should only return the resource if it doesn't match the etag") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port =>
def get(etag: Option[String]) = basicRequest
.get(uri"http://localhost:$port/r1.txt")
.header(HeaderNames.IfNoneMatch, etag)
.response(asStringAlways)
.send(backend)
get(None).flatMap { r1 =>
r1.code shouldBe StatusCode.Ok
val etag = r1.header(HeaderNames.Etag).get
get(Some(etag)).map { r2 =>
r2.code shouldBe StatusCode.NotModified
} >> get(Some(etag.replace("-", "-x"))).map { r2 =>
r2.code shouldBe StatusCode.Ok
}
}
}
.unsafeToFuture()
},
Test("should serve index.html when a resource directory is requested (from file)") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port =>
get(port, List("d1")).map(_.body shouldBe "Index resource")
}
.unsafeToFuture()
},
Test("should serve a resource from a jar") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "META-INF/maven/org.slf4j/slf4j-api"))
.use { port =>
get(port, List("pom.properties")).map(_.body should include("groupId=org.slf4j"))
}
.unsafeToFuture()
},
Test("should return 404 when a resource directory is requested from jar and index.html does not exist") {
serveRoute(staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "META-INF/maven/org.slf4j/slf4j-api"))
.use { port =>
get(port, Nil).map(_.code shouldBe StatusCode.NotFound)
}
.unsafeToFuture()
}
)
val resourceMetadataTests = List(
Test("should return resource metadata") {
serveRoute(
staticResourcesGetServerEndpoint[F](emptyInput)(classLoader, "test")
)
.use { port =>
get(port, List("r1.txt")).map { r =>
r.contentLength shouldBe Some(10)
r.contentType shouldBe Some(MediaType.TextPlain.toString())
r.header(HeaderNames.LastModified)
.flatMap(t => Header.parseHttpDate(t).toOption)
.map(_.toEpochMilli)
.get should be > (1629180000000L) // 8:00 17 Aug 2021 when the test was written
r.header(HeaderNames.Etag).isDefined shouldBe true
}
}
.unsafeToFuture()
},
Test("should create both head and get endpoints for files") {
withTestFilesDirectory { testDir =>
val headAndGetEndpoint = staticFilesServerEndpoints[F]("test")(testDir.getAbsolutePath)
serverInterpreter
.server(NonEmptyList.of(serverInterpreter.route(headAndGetEndpoint)))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=3-6"))
.head(uri"http://localhost:$port/test/f2")
.response(asStringAlways)
.send(backend)
.map(r => {
r.body shouldBe ""
r.code shouldBe StatusCode.PartialContent
r.body.length shouldBe 0
r.headers contains Header(HeaderNames.ContentRange, "bytes 3-6/10") shouldBe true
}) >>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=3-6"))
.get(uri"http://localhost:$port/test/f2")
.response(asStringAlways)
.send(backend)
.map(r => {
r.body shouldBe "cont"
r.code shouldBe StatusCode.PartialContent
r.body.length shouldBe 4
r.headers contains Header(HeaderNames.ContentRange, "bytes 3-6/10") shouldBe true
})
}
.unsafeToFuture()
}
},
Test("should create both head and get endpoints for resources") {
val headAndGetEndpoint = staticResourcesServerEndpoints[F](emptyInput)(classLoader, "test")
serverInterpreter
.server(NonEmptyList.of(serverInterpreter.route(headAndGetEndpoint)))
.use { port =>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=6-9"))
.head(uri"http://localhost:$port/r1.txt")
.response(asStringAlways)
.send(backend)
.map(r => {
r.body shouldBe ""
r.code shouldBe StatusCode.PartialContent
r.body.length shouldBe 0
r.headers contains Header(HeaderNames.ContentRange, "bytes 6-9/10") shouldBe true
}) >>
basicRequest
.headers(Header(HeaderNames.Range, "bytes=6-9"))
.get(uri"http://localhost:$port/r1.txt")
.response(asStringAlways)
.send(backend)
.map(r => {
r.body shouldBe "ce 1"
r.code shouldBe StatusCode.PartialContent
r.body.length shouldBe 4
r.headers contains Header(HeaderNames.ContentRange, "bytes 6-9/10") shouldBe true
})
}
.unsafeToFuture()
},
Test("if an etag is present, should only return the file if it doesn't match the etag") {
withTestFilesDirectory { testDir =>
val headAndGetEndpoint = staticFilesServerEndpoints[F](emptyInput)(testDir.getAbsolutePath)
serverInterpreter
.server(NonEmptyList.of(serverInterpreter.route(headAndGetEndpoint)))
.use { port =>
def testHttpMethod(method: Request[Either[String, String], Any]) = {
def send(etag: Option[String]) =
method
.header(HeaderNames.IfNoneMatch, etag)
.response(asStringAlways)
.send(backend)
send(etag = None).flatMap { r1 =>
r1.code shouldBe StatusCode.Ok
val etag = r1.header(HeaderNames.Etag).get
send(etag = Some(etag)).map { r2 =>
r2.code shouldBe StatusCode.NotModified
} >> send(Some(etag.replace("-", "-x"))).map { r2 =>
r2.code shouldBe StatusCode.Ok
}
}
}
testHttpMethod(
basicRequest
.get(uri"http://localhost:$port/f1")
) >>
testHttpMethod(
basicRequest
.head(uri"http://localhost:$port/f1")
)
}
.unsafeToFuture()
}
},
Test("should not return resource metadata outside of the resource prefix directory") {
serveRoute(staticResourcesHeadServerEndpoint[F](emptyInput)(classLoader, "test"))
.use { port => head(port, List("..", "test", "r5.txy")).map(_.code.isClientError shouldBe true) }
.unsafeToFuture()
}
)
if (supportSettingContentLength) baseTests ++ resourceMetadataTests
else baseTests
}
def serveRoute(e: ServerEndpoint[Any, F]): Resource[IO, Port] =
serverInterpreter.server(
NonEmptyList.of(
serverInterpreter.route(e, (ci: CustomiseInterceptors[F, OPTIONS]) => ci.decodeFailureHandler(DefaultDecodeFailureHandler.default))
)
)
def gzipCompress(input: String): Array[Byte] = {
val outputStream = new ByteArrayOutputStream()
val gzipOutputStream = new GZIPOutputStream(outputStream)
gzipOutputStream.write(input.getBytes("UTF-8"))
gzipOutputStream.close()
outputStream.toByteArray
}
def decompress(bytes: Array[Byte]): String = {
val inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))
val outputStream = new ByteArrayOutputStream()
inputStream.transferTo(outputStream)
outputStream.toString("UTF-8")
}
def withTestFilesDirectory[T](t: File => Future[T]): Future[T] = {
val parent = Files.createTempDirectory("tapir-tests")
parent.resolve("d1/d2").toFile.mkdirs()
Files.write(parent.resolve("f1"), "f1 content".getBytes, StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("f2"), "f2 content".getBytes, StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("img.gif"), "img content".getBytes, StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("img.gif.gz"), gzipCompress("img gzipped content"), StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("d1/f3"), "f3 content".getBytes, StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("d1/index.html"), "index content".getBytes, StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("d1/d2/f4"), "f4 content".getBytes, StandardOpenOption.CREATE_NEW)
Files.write(parent.resolve("f5"), ("x" * 300000).getBytes, StandardOpenOption.CREATE_NEW)
parent.toFile.deleteOnExit()
import scala.concurrent.ExecutionContext.Implicits.global
val result = t(parent.toFile)
result.onComplete(_ =>
Files
.walk(parent)
.sorted(Comparator.reverseOrder())
.map[File](f => f.toFile)
.forEach(f => { val _ = f.delete })
)
result
}
}