forked from http4s/http4s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArbitraryInstances.scala
723 lines (614 loc) · 23.5 KB
/
ArbitraryInstances.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
719
720
721
722
723
package org.http4s
package testing
import cats._
import cats.data.NonEmptyList
import cats.effect.{Effect, IO}
import cats.effect.laws.discipline.arbitrary._
import cats.effect.laws.util.TestContext
import cats.implicits.{catsSyntaxEither => _, _}
import fs2.{Pure, Stream}
import java.nio.charset.{Charset => NioCharset}
import java.time._
import java.util.Locale
import org.http4s.headers._
import org.http4s.syntax.literals._
import org.http4s.syntax.string._
import org.http4s.util.CaseInsensitiveString
import org.scalacheck._
import org.scalacheck.Arbitrary._
import org.scalacheck.Gen._
import org.scalacheck.rng.Seed
import scala.collection.JavaConverters._
import scala.concurrent.duration._
import scala.concurrent.Future
import scala.util.Try
trait ArbitraryInstances {
private implicit class ParseResultSyntax[A](self: ParseResult[A]) {
def yolo: A = self.valueOr(e => sys.error(e.toString))
}
implicit val arbitraryCaseInsensitiveString: Arbitrary[CaseInsensitiveString] =
Arbitrary(arbitrary[String].map(_.ci))
implicit val cogenCaseInsensitiveString: Cogen[CaseInsensitiveString] =
Cogen[String].contramap(_.value.toLowerCase(Locale.ROOT))
implicit def arbitraryNonEmptyList[A: Arbitrary]: Arbitrary[NonEmptyList[A]] =
Arbitrary {
for {
a <- arbitrary[A]
list <- arbitrary[List[A]]
} yield NonEmptyList(a, list)
}
val genChar: Gen[Char] = choose('\u0000', '\u007F')
val ctlChar: List[Char] = ('\u007F' +: ('\u0000' to '\u001F')).toList
val lws: List[Char] = " \t".toList
val genCrLf: Gen[String] = const("\r\n")
val genRightLws: Gen[String] = nonEmptyListOf(oneOf(lws)).map(_.mkString)
val genLws: Gen[String] =
oneOf(sequence[List[String], String](List(genCrLf, genRightLws)).map(_.mkString), genRightLws)
val octets: List[Char] = ('\u0000' to '\u00FF').toList
val genOctet: Gen[Char] = oneOf(octets)
val allowedText: List[Char] = octets.diff(ctlChar)
val genText: Gen[String] = oneOf(nonEmptyListOf(oneOf(allowedText)).map(_.mkString), genLws)
// TODO Fix Rfc2616BasicRules.QuotedString to support the backslash character
val allowedQDText: List[Char] = allowedText.filterNot(c => c == '"' || c == '\\')
val genQDText: Gen[String] = nonEmptyListOf(oneOf(allowedQDText)).map(_.mkString)
val genQuotedPair: Gen[String] =
genChar.map(c => s"\\$c")
val genQuotedString: Gen[String] = oneOf(genQDText, genQuotedPair).map(s => s"""\"$s\"""")
val genTchar: Gen[Char] = oneOf {
Seq('!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~') ++
('0' to '9') ++ ('A' to 'Z') ++ ('a' to 'z')
}
val genToken: Gen[String] =
nonEmptyListOf(genTchar).map(_.mkString)
val genVchar: Gen[Char] =
oneOf('\u0021' to '\u007e')
val genFieldVchar: Gen[Char] =
genVchar
val genFieldContent: Gen[String] =
for {
head <- genFieldVchar
tail <- containerOf[Vector, Vector[Char]](
frequency(
9 -> genFieldVchar.map(Vector(_)),
1 -> (for {
spaces <- nonEmptyContainerOf[Vector, Char](oneOf(' ', '\t'))
fieldVchar <- genFieldVchar
} yield spaces :+ fieldVchar)
)
).map(_.flatten)
} yield (head +: tail).mkString
val genFieldValue: Gen[String] =
genFieldContent
val genStandardMethod: Gen[Method] =
oneOf(Method.all)
implicit val arbitraryMethod: Arbitrary[Method] = Arbitrary(
frequency(
10 -> genStandardMethod,
1 -> genToken.map(Method.fromString(_).yolo)
))
implicit val cogenMethod: Cogen[Method] =
Cogen[Int].contramap(_.##)
val genValidStatusCode =
choose(100, 599)
val genStandardStatus =
oneOf(Status.registered.toSeq)
val genCustomStatus = for {
code <- genValidStatusCode
reason <- arbitrary[String]
} yield Status.fromIntAndReason(code, reason).yolo
implicit val arbitraryStatus: Arbitrary[Status] = Arbitrary(
frequency(
10 -> genStandardStatus,
1 -> genCustomStatus
))
implicit val cogenStatus: Cogen[Status] =
Cogen[Int].contramap(_.code)
implicit val arbitraryQueryParam: Arbitrary[(String, Option[String])] =
Arbitrary {
frequency(
5 -> {
for {
k <- arbitrary[String]
v <- arbitrary[Option[String]]
} yield (k, v)
},
2 -> const(("foo" -> Some("bar"))) // Want some repeats
)
}
implicit val arbitraryQuery: Arbitrary[Query] =
Arbitrary {
for {
n <- size
vs <- containerOfN[Vector, (String, Option[String])](n % 8, arbitraryQueryParam.arbitrary)
} yield Query(vs: _*)
}
implicit val arbitraryHttpVersion: Arbitrary[HttpVersion] =
Arbitrary {
for {
major <- choose(0, 9)
minor <- choose(0, 9)
} yield HttpVersion.fromVersion(major, minor).yolo
}
implicit val cogenHttpVersion: Cogen[HttpVersion] =
Cogen[(Int, Int)].contramap(v => (v.major, v.minor))
implicit val arbitraryNioCharset: Arbitrary[NioCharset] =
Arbitrary(oneOf(NioCharset.availableCharsets.values.asScala.toSeq))
implicit val cogenNioCharset: Cogen[NioCharset] =
Cogen[String].contramap(_.name)
implicit val arbitraryCharset: Arbitrary[Charset] =
Arbitrary { arbitrary[NioCharset].map(Charset.fromNioCharset) }
implicit val cogenCharset: Cogen[Charset] =
Cogen[NioCharset].contramap(_.nioCharset)
implicit val arbitraryQValue: Arbitrary[QValue] =
Arbitrary {
oneOf(const(0), const(1000), choose(0, 1000))
.map(QValue.fromThousandths(_).yolo)
}
implicit val cogenQValue: Cogen[QValue] =
Cogen[Int].contramap(_.thousandths)
implicit val arbitraryCharsetRange: Arbitrary[CharsetRange] =
Arbitrary {
for {
charsetRange <- genCharsetRangeNoQuality
q <- arbitrary[QValue]
} yield charsetRange.withQValue(q)
}
implicit val cogenCharsetRange: Cogen[CharsetRange] =
Cogen[Either[(Charset, QValue), QValue]].contramap {
case CharsetRange.Atom(charset, qValue) =>
Left((charset, qValue))
case CharsetRange.`*`(qValue) =>
Right(qValue)
}
implicit val arbitraryCharsetAtomRange: Arbitrary[CharsetRange.Atom] =
Arbitrary {
for {
charset <- arbitrary[Charset]
q <- arbitrary[QValue]
} yield charset.withQuality(q)
}
implicit val arbitraryCharsetSplatRange: Arbitrary[CharsetRange.`*`] =
Arbitrary { arbitrary[QValue].map(CharsetRange.`*`.withQValue(_)) }
def genCharsetRangeNoQuality: Gen[CharsetRange] =
frequency(
3 -> arbitrary[Charset].map(CharsetRange.fromCharset),
1 -> const(CharsetRange.`*`)
)
@deprecated("Use genCharsetRangeNoQuality. This one may cause deadlocks.", "0.15.7")
val charsetRangesNoQuality: Gen[CharsetRange] =
genCharsetRangeNoQuality
implicit val arbitraryAcceptCharset: Arbitrary[`Accept-Charset`] =
Arbitrary {
for {
// make a set first so we don't have contradictory q-values
charsetRanges <- nonEmptyContainerOf[Set, CharsetRange](genCharsetRangeNoQuality)
.map(_.toVector)
qValues <- containerOfN[Vector, QValue](charsetRanges.size, arbitraryQValue.arbitrary)
charsetRangesWithQ = charsetRanges.zip(qValues).map {
case (range, q) => range.withQValue(q)
}
} yield `Accept-Charset`(charsetRangesWithQ.head, charsetRangesWithQ.tail: _*)
}
def genContentCodingNoQuality: Gen[ContentCoding] =
Gen.frequency(
(10, oneOf(ContentCoding.standard.values.toSeq)),
(2, genToken.map(ContentCoding.unsafeFromString))
)
implicit val arbitraryContentCoding: Arbitrary[ContentCoding] =
Arbitrary {
for {
cc <- genContentCodingNoQuality
q <- arbitrary[QValue]
} yield cc.withQValue(q)
}
implicit val cogenContentCoding: Cogen[ContentCoding] =
Cogen[String].contramap(_.coding)
// MediaRange exepects the quoted pair without quotes
val http4sGenUnquotedPair = genQuotedPair.map { c =>
c.substring(1, c.length - 1)
}
val http4sGenMediaRangeExtension: Gen[(String, String)] =
for {
token <- genToken
value <- oneOf(http4sGenUnquotedPair, genQDText)
} yield (token, value)
val http4sGenMediaRangeExtensions: Gen[Map[String, String]] =
Gen.listOf(http4sGenMediaRangeExtension).map(_.toMap)
val http4sGenMediaRange: Gen[MediaRange] =
for {
`type` <- genToken
extensions <- http4sGenMediaRangeExtensions
} yield new MediaRange(`type`, extensions)
implicit val http4sArbitraryMediaRange: Arbitrary[MediaRange] =
Arbitrary {
for {
`type` <- genToken
extensions <- http4sGenMediaRangeExtensions
} yield new MediaRange(`type`, extensions)
}
implicit val http4sCogenMediaRange: Cogen[MediaRange] =
Cogen[(String, Map[String, String])].contramap(m => (m.mainType, m.extensions))
implicit val arbitraryAcceptEncoding: Arbitrary[`Accept-Encoding`] =
Arbitrary {
for {
// make a set first so we don't have contradictory q-values
contentCodings <- nonEmptyContainerOf[Set, ContentCoding](genContentCodingNoQuality)
.map(_.toVector)
qValues <- containerOfN[Vector, QValue](contentCodings.size, arbitraryQValue.arbitrary)
contentCodingsWithQ = contentCodings.zip(qValues).map {
case (coding, q) => coding.withQValue(q)
}
} yield `Accept-Encoding`(contentCodingsWithQ.head, contentCodingsWithQ.tail: _*)
}
implicit val arbitraryContentEncoding: Arbitrary[`Content-Encoding`] =
Arbitrary {
for {
contentCoding <- genContentCodingNoQuality
} yield `Content-Encoding`(contentCoding)
}
def genLanguageTagNoQuality: Gen[LanguageTag] =
frequency(
3 -> (for {
primaryTag <- genToken
subTags <- frequency(4 -> Nil, 1 -> listOf(genToken))
} yield LanguageTag(primaryTag, subTags = subTags)),
1 -> const(LanguageTag.`*`)
)
implicit val arbitraryLanguageTag: Arbitrary[LanguageTag] =
Arbitrary {
for {
lt <- genLanguageTagNoQuality
q <- arbitrary[QValue]
} yield lt.copy(q = q)
}
implicit val arbitraryAcceptLanguage: Arbitrary[`Accept-Language`] =
Arbitrary {
for {
// make a set first so we don't have contradictory q-values
languageTags <- nonEmptyContainerOf[Set, LanguageTag](genLanguageTagNoQuality)
.map(_.toVector)
qValues <- containerOfN[Vector, QValue](languageTags.size, arbitraryQValue.arbitrary)
tagsWithQ = languageTags.zip(qValues).map { case (tag, q) => tag.copy(q = q) }
} yield `Accept-Language`(tagsWithQ.head, tagsWithQ.tail: _*)
}
implicit val arbitraryUrlForm: Arbitrary[UrlForm] = Arbitrary {
// new String("\ufffe".getBytes("UTF-16"), "UTF-16") != "\ufffe".
// Ain't nobody got time for that.
arbitrary[Map[String, Seq[String]]]
.map(UrlForm.apply)
.suchThat(!_.toString.contains('\ufffe'))
}
implicit val arbitraryAllow: Arbitrary[Allow] =
Arbitrary {
for {
methods <- nonEmptyContainerOf[Set, Method](arbitrary[Method]).map(_.toList)
} yield Allow(methods.head, methods.tail: _*)
}
implicit val arbitraryContentLength: Arbitrary[`Content-Length`] =
Arbitrary {
for {
long <- Gen.chooseNum(0L, Long.MaxValue)
} yield `Content-Length`.unsafeFromLong(long)
}
implicit val arbitraryXB3TraceId: Arbitrary[`X-B3-TraceId`] =
Arbitrary {
for {
long <- arbitrary[Long]
} yield `X-B3-TraceId`(long)
}
implicit val arbitraryXB3SpanId: Arbitrary[`X-B3-SpanId`] =
Arbitrary {
for {
long <- arbitrary[Long]
} yield `X-B3-SpanId`(long)
}
implicit val arbitraryXB3ParentSpanId: Arbitrary[`X-B3-ParentSpanId`] =
Arbitrary {
for {
long <- arbitrary[Long]
} yield `X-B3-ParentSpanId`(long)
}
implicit val arbitraryXB3Flags: Arbitrary[`X-B3-Flags`] =
Arbitrary {
for {
flags <- Gen.listOfN(
3,
Gen.oneOf(
`X-B3-Flags`.Flag.Debug,
`X-B3-Flags`.Flag.Sampled,
`X-B3-Flags`.Flag.SamplingSet))
} yield `X-B3-Flags`(flags.toSet)
}
implicit val arbitraryXB3Sampled: Arbitrary[`X-B3-Sampled`] =
Arbitrary {
for {
boolean <- arbitrary[Boolean]
} yield `X-B3-Sampled`(boolean)
}
val genHttpDate: Gen[HttpDate] = {
val min = ZonedDateTime
.of(1900, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"))
.toInstant
.toEpochMilli / 1000
val max = ZonedDateTime
.of(9999, 12, 31, 23, 59, 59, 0, ZoneId.of("UTC"))
.toInstant
.toEpochMilli / 1000
choose[Long](min, max).map(HttpDate.unsafeFromEpochSecond)
}
implicit val arbitraryDateHeader: Arbitrary[headers.Date] =
Arbitrary {
for {
httpDate <- genHttpDate
} yield headers.Date(httpDate)
}
val genHttpExpireDate: Gen[HttpDate] = {
// RFC 2616 says Expires should be between now and 1 year in the future, though other values are allowed
val min = ZonedDateTime.of(LocalDateTime.now, ZoneId.of("UTC")).toInstant.toEpochMilli / 1000
val max = ZonedDateTime
.of(LocalDateTime.now.plusYears(1), ZoneId.of("UTC"))
.toInstant
.toEpochMilli / 1000
choose[Long](min, max).map(HttpDate.unsafeFromEpochSecond)
}
val genFiniteDuration: Gen[FiniteDuration] =
// Only consider positive durations
Gen.posNum[Long].map(_.seconds)
implicit val arbitraryExpiresHeader: Arbitrary[headers.Expires] =
Arbitrary {
for {
date <- genHttpExpireDate
} yield headers.Expires(date)
}
val http4sGenMediaRangeAndQValue: Gen[MediaRangeAndQValue] =
for {
mediaRange <- http4sGenMediaRange
qValue <- arbitrary[QValue]
} yield MediaRangeAndQValue(mediaRange, qValue)
implicit val http4sArbitraryAcceptHeader: Arbitrary[headers.Accept] =
Arbitrary {
for {
values <- nonEmptyListOf(http4sGenMediaRangeAndQValue)
} yield headers.Accept(NonEmptyList.of(values.head, values.tail: _*))
}
implicit val arbitraryRetryAfterHeader: Arbitrary[headers.`Retry-After`] =
Arbitrary {
for {
retry <- Gen.oneOf(genHttpExpireDate.map(Left(_)), Gen.posNum[Long].map(Right(_)))
} yield
retry.fold(
headers.`Retry-After`.apply,
headers.`Retry-After`.unsafeFromLong
)
}
implicit val arbitraryAgeHeader: Arbitrary[headers.Age] =
Arbitrary {
for {
// age is always positive
age <- genFiniteDuration
} yield headers.Age.unsafeFromDuration(age)
}
implicit val arbitrarySTS: Arbitrary[headers.`Strict-Transport-Security`] =
Arbitrary {
for {
// age is always positive
age <- genFiniteDuration
includeSubDomains <- Gen.oneOf(true, false)
preload <- Gen.oneOf(true, false)
} yield
headers.`Strict-Transport-Security`.unsafeFromDuration(age, includeSubDomains, preload)
}
implicit val arbitraryTransferEncoding: Arbitrary[`Transfer-Encoding`] =
Arbitrary {
for {
codings <- arbitrary[NonEmptyList[TransferCoding]]
} yield `Transfer-Encoding`(codings)
}
implicit val arbitraryRawHeader: Arbitrary[Header.Raw] =
Arbitrary {
for {
token <- genToken
value <- genFieldValue
} yield Header.Raw(token.ci, value)
}
implicit val arbitraryHeader: Arbitrary[Header] =
Arbitrary {
oneOf(
arbitrary[`Accept-Charset`],
arbitrary[Allow],
arbitrary[`Content-Length`],
arbitrary[Date],
arbitrary[Header.Raw]
)
}
implicit val arbitraryHeaders: Arbitrary[Headers] =
Arbitrary(listOf(arbitrary[Header]).map(Headers(_: _*)))
implicit val arbitraryServerSentEvent: Arbitrary[ServerSentEvent] = {
import ServerSentEvent._
def singleLineString: Gen[String] =
arbitrary[String].suchThat { s =>
!s.contains("\r") && !s.contains("\n")
}
Arbitrary(for {
data <- singleLineString
event <- frequency(
4 -> None,
1 -> singleLineString.map(Some.apply)
)
id <- frequency(
8 -> None,
1 -> Some(EventId.reset),
1 -> singleLineString.suchThat(_.nonEmpty).map(id => Some(EventId(id)))
)
retry <- frequency(
4 -> None,
1 -> posNum[Long].map(Some.apply)
)
} yield ServerSentEvent(data, event, id, retry))
}
// https://tools.ietf.org/html/rfc2234#section-6
val genHexDigit: Gen[Char] = oneOf(
Seq('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'))
private implicit def semigroupGen[T: Semigroup]: Semigroup[Gen[T]] = new Semigroup[Gen[T]] {
def combine(g1: Gen[T], g2: Gen[T]): Gen[T] = for { t1 <- g1; t2 <- g2 } yield t1 |+| t2
}
private def timesBetween[T: Monoid](min: Int, max: Int, g: Gen[T]): Gen[T] =
for {
n <- choose(min, max)
l <- listOfN(n, g).suchThat(_.length == n)
} yield l.fold(Monoid[T].empty)(_ |+| _)
private def times[T: Monoid](n: Int, g: Gen[T]): Gen[T] =
listOfN(n, g).suchThat(_.length == n).map(_.reduce(_ |+| _))
private def atMost[T: Monoid](n: Int, g: Gen[T]): Gen[T] =
timesBetween(min = 0, max = n, g)
private def opt[T](g: Gen[T])(implicit ev: Monoid[T]): Gen[T] =
oneOf(g, const(ev.empty))
// https://tools.ietf.org/html/rfc3986#appendix-A
implicit val arbitraryIPv4: Arbitrary[Uri.IPv4] = Arbitrary {
val num = numChar.map(_.toString)
def range(min: Int, max: Int) = choose(min.toChar, max.toChar).map(_.toString)
val genDecOctet = oneOf(
num,
range(49, 57) |+| num,
const("1") |+| num |+| num,
const("2") |+| range(48, 52) |+| num,
const("25") |+| range(48, 51)
)
listOfN(4, genDecOctet).map(_.mkString(".")).map(Uri.IPv4.apply)
}
// https://tools.ietf.org/html/rfc3986#appendix-A
implicit val arbitraryIPv6: Arbitrary[Uri.IPv6] = Arbitrary {
val h16 = timesBetween(min = 1, max = 4, genHexDigit.map(_.toString))
val ls32 = oneOf(h16 |+| const(":") |+| h16, arbitraryIPv4.arbitrary.map(_.address.value))
val h16colon = h16 |+| const(":")
val :: = const("::")
oneOf(
times(6, h16colon) |+| ls32,
:: |+| times(5, h16colon) |+| ls32,
opt(h16) |+| :: |+| times(4, h16colon) |+| ls32,
opt(atMost(1, h16colon) |+| h16) |+| :: |+| times(3, h16colon) |+| ls32,
opt(atMost(2, h16colon) |+| h16) |+| :: |+| times(2, h16colon) |+| ls32,
opt(atMost(3, h16colon) |+| h16) |+| :: |+| opt(h16colon) |+| ls32,
opt(atMost(4, h16colon) |+| h16) |+| :: |+| ls32,
opt(atMost(5, h16colon) |+| h16) |+| :: |+| h16,
opt(atMost(6, h16colon) |+| h16) |+| ::
).map(Uri.IPv6.apply)
}
implicit val arbitraryUriHost: Arbitrary[Uri.Host] = Arbitrary {
val genRegName =
listOf(oneOf(genUnreserved, genPctEncoded, genSubDelims)).map(rn => Uri.RegName(rn.mkString))
oneOf(arbitraryIPv4.arbitrary, arbitraryIPv6.arbitrary, genRegName)
}
implicit val arbitraryAuthority: Arbitrary[Uri.Authority] = Arbitrary {
for {
userInfo <- identifier
maybeUserInfo <- Gen.option(userInfo)
host <- arbitraryUriHost.arbitrary
maybePort <- Gen.option(posNum[Int].suchThat(port => port >= 0 && port <= 65536))
} yield Uri.Authority(maybeUserInfo, host, maybePort)
}
val genPctEncoded: Gen[String] = const("%") |+| genHexDigit.map(_.toString) |+| genHexDigit.map(
_.toString)
val genUnreserved: Gen[Char] =
oneOf(alphaChar, numChar, const('-'), const('.'), const('_'), const('~'))
val genSubDelims: Gen[Char] = oneOf(Seq('!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='))
implicit val arbitraryScheme: Arbitrary[Uri.Scheme] = Arbitrary {
frequency(
5 -> Uri.Scheme.http,
5 -> Uri.Scheme.https,
1 -> scheme"HTTP",
1 -> scheme"HTTPS",
3 -> (for {
head <- alphaChar
tail <- listOf(
frequency(
36 -> alphaNumChar,
1 -> const('+'),
1 -> const('-'),
1 -> const('.')
)
)
} yield HttpCodec[Uri.Scheme].parseOrThrow(tail.mkString(head.toString, "", "")))
)
}
implicit val cogenScheme: Cogen[Uri.Scheme] =
Cogen[String].contramap(_.value.toLowerCase(Locale.ROOT))
implicit val arbitraryTransferCoding: Arbitrary[TransferCoding] = Arbitrary {
Gen.oneOf(
TransferCoding.chunked,
TransferCoding.compress,
TransferCoding.deflate,
TransferCoding.gzip,
TransferCoding.identity)
}
implicit val cogenTransferCoding: Cogen[TransferCoding] =
Cogen[String].contramap(_.coding.toLowerCase(Locale.ROOT))
/** https://tools.ietf.org/html/rfc3986 */
implicit val arbitraryUri: Arbitrary[Uri] = Arbitrary {
val genSegmentNzNc =
nonEmptyListOf(oneOf(genUnreserved, genPctEncoded, genSubDelims, const("@"))).map(_.mkString)
val genPChar = oneOf(genUnreserved, genPctEncoded, genSubDelims, const(":"), const("@"))
val genSegmentNz = nonEmptyListOf(genPChar).map(_.mkString)
val genSegment = listOf(genPChar).map(_.mkString)
val genPathEmpty = const("")
val genPathAbEmpty = listOf(const("/") |+| genSegment).map(_.mkString)
val genPathRootless = genSegmentNz |+| genPathAbEmpty
val genPathNoScheme = genSegmentNzNc |+| genPathAbEmpty
val genPathAbsolute = const("/") |+| opt(genPathRootless)
val genScheme = oneOf(Uri.Scheme.http, Uri.Scheme.https)
val genPath =
oneOf(genPathAbEmpty, genPathAbsolute, genPathNoScheme, genPathRootless, genPathEmpty)
val genFragment: Gen[Uri.Fragment] =
listOf(oneOf(genPChar, const("/"), const("?"))).map(_.mkString)
for {
scheme <- Gen.option(genScheme)
authority <- Gen.option(arbitraryAuthority.arbitrary)
path <- genPath
query <- arbitraryQuery.arbitrary
fragment <- Gen.option(genFragment)
} yield Uri(scheme, authority, path, query, fragment)
}
// TODO This could be a lot more interesting.
// See https://github.com/functional-streams-for-scala/fs2/blob/fd3d0428de1e71c10d1578f2893ee53336264ffe/core/shared/src/test/scala/fs2/TestUtil.scala#L42
implicit def genEntityBody[F[_]]: Gen[Stream[Pure, Byte]] = Gen.sized { size =>
Gen.listOfN(size, arbitrary[Byte]).map(Stream.emits)
}
// Borrowed from cats-effect tests for the time being
def cogenFuture[A](implicit ec: TestContext, cg: Cogen[Try[A]]): Cogen[Future[A]] =
Cogen { (seed: Seed, fa: Future[A]) =>
ec.tick()
fa.value match {
case None => seed
case Some(ta) => cg.perturb(seed, ta)
}
}
implicit def cogenEntityBody[F[_]](implicit F: Effect[F], ec: TestContext): Cogen[EntityBody[F]] =
catsEffectLawsCogenForIO(cogenFuture[Vector[Byte]]).contramap { stream =>
var bytes: Vector[Byte] = null
val readBytes = IO(bytes)
F.runAsync(stream.compile.toVector) {
case Right(bs) => IO { bytes = bs }
case Left(t) => IO.raiseError(t)
} *> readBytes
}
implicit def arbitraryEntity[F[_]]: Arbitrary[Entity[F]] =
Arbitrary(Gen.sized { size =>
for {
body <- genEntityBody
length <- Gen.oneOf(Some(size.toLong), None)
} yield Entity(body, length)
})
implicit def cogenEntity[F[_]](implicit F: Effect[F], ec: TestContext): Cogen[Entity[F]] =
Cogen[(EntityBody[F], Option[Long])].contramap(entity => (entity.body, entity.length))
implicit def arbitraryEntityEncoder[F[_], A](
implicit CA: Cogen[A],
AF: Arbitrary[F[Entity[F]]]): Arbitrary[EntityEncoder[F, A]] =
Arbitrary(for {
f <- arbitrary[A => F[Entity[F]]]
hs <- arbitrary[Headers]
} yield EntityEncoder.encodeBy(hs)(f))
}
object ArbitraryInstances extends ArbitraryInstances {
// This were introduced after .0 and need to be kept out of the
// trait. We can move them back into the trait in the next .0.
}