-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (83 loc) · 2.15 KB
/
main.go
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
package randurl
import (
"fmt"
"math/rand"
"strconv"
"strings"
)
type PathComponent interface {
String() string
}
type URLSpec struct {
Scheme, Host string
Components []PathComponent
}
func (u URLSpec) String() string {
b := strings.Builder{}
b.WriteString(fmt.Sprintf("%s://%s", u.Scheme, u.Host))
for _, s := range u.Components {
b.WriteString(fmt.Sprintf("/%s", s.String()))
}
return b.String()
}
type StringComponent string
func (s StringComponent) String() string {
return string(s)
}
var validStatuses = []int{100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206,
207, 208, 226, 300, 301, 303, 304, 305, 307, 308, 400, 401, 402, 403, 404,
405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 421, 422,
423, 424, 425, 426, 427, 429, 430, 451, 500, 501, 502, 503, 504, 505, 506,
507, 508, 509, 510, 511,
}
type HTTPStatus struct {
Ranges []int
}
func (hs HTTPStatus) String() string {
var vc []int
// Build a list of all valid codes that were requested in `Ranges`
for _, r := range hs.Ranges {
for _, v := range validStatuses {
if r/100 == v/100 {
vc = append(vc, v)
}
}
}
return strconv.Itoa(vc[rand.Intn(len(vc))])
}
type IntegerComponent struct {
Min int
Max int
}
func (i IntegerComponent) String() string {
n := rand.Intn(i.Max-i.Min) + i.Min
return strconv.Itoa(n)
}
const (
LowercaseAlphabetChars = "abcdefghijklmnopqrstuvwxyz"
UppercaseAlphabetChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
AlphabetChars = LowercaseAlphabetChars + UppercaseAlphabetChars
DigitChars = "0123456789"
PunctuationChars = ".,-_+!()[]{}*"
)
type RandomStringComponent struct {
Chars []rune
Format string
MinLength, MaxLength int
}
func (r RandomStringComponent) String() string {
var targetLength int
if r.MaxLength == r.MinLength {
targetLength = r.MaxLength
} else {
targetLength = rand.Intn(r.MaxLength-r.MinLength) + r.MinLength
}
randomChars := make([]rune, targetLength)
for i := 0; i < targetLength; i++ {
randomChars[i] = r.Chars[rand.Intn(len(r.Chars))]
}
if r.Format != "" {
return fmt.Sprintf(r.Format, string(randomChars))
}
return string(randomChars)
}