This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinternet.go
135 lines (124 loc) · 3.36 KB
/
internet.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
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
package faker
import (
"net"
"net/http"
)
// GenericTopLevelDomain returns random generic top-level domain
func (f *Faker) GenericTopLevelDomain() string {
return GenericTopLevelDomain(
WithRand(f.cfg.rand),
WithGenericTopLevelDomains(f.cfg.genericTopLevelDomains...),
)
}
// GenericTopLevelDomain returns random generic top-level domain
//
// faker.GenericTopLevelDomain(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))), // Rand instance
// faker.WithGenericTopLevelDomains("com", "edu", "net", "org"), // Slice of generic top-level domains for RandomElement
// )
//
func GenericTopLevelDomain(opts ...Option) string {
cfg := newConfig(opts...)
if len(cfg.genericTopLevelDomains) == 0 {
WithGenericTopLevelDomains("com", "edu", "net", "org")(cfg)
}
return RandomElement(cfg.genericTopLevelDomains, opts...)
}
// IPv4 returns random IP v4 address
func (f *Faker) IPv4() string {
return IPv4(WithRand(f.cfg.rand))
}
// IPv4 returns random IP v4 address
//
// faker.IPv4(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))), // Rand instance
// )
//
func IPv4(opts ...Option) string {
size := 4
ip := make([]byte, size)
for i := 0; i < size; i++ {
ip[i] = byte(Integer(0, 256, opts...))
}
return net.IP(ip).To4().String()
}
// IPv6 returns random IP v6 address
func (f *Faker) IPv6() string {
return IPv6(WithRand(f.cfg.rand))
}
// IPv6 returns random IP v6 address
//
// faker.IPv6(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))), // Rand instance
// )
//
func IPv6(opts ...Option) string {
size := 16
ip := make([]byte, size)
for i := 0; i < size; i++ {
ip[i] = byte(Integer(0, 256, opts...))
}
return net.IP(ip).To16().String()
}
// HTTPMethod returns random HTTP method
func (f *Faker) HTTPMethod() string {
return HTTPMethod(
WithRand(f.cfg.rand),
WithHTTPMethods(f.cfg.httpMethods...),
)
}
// HTTPMethod returns random HTTP method
//
// faker.HTTPMethod(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))), // Rand instance
// faker.SetSetHTTPMethods("GET", "HEAD"), // Slice of HTTP method for RandomElement
// )
//
func HTTPMethod(opts ...Option) string {
cfg := newConfig(opts...)
if len(cfg.httpMethods) == 0 {
WithHTTPMethods(
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
)(cfg)
}
return RandomElement(cfg.httpMethods, opts...)
}
// HTTPStatusCode returns random HTTP status code
func (f *Faker) HTTPStatusCode() int {
return HTTPStatusCode(
WithRand(f.cfg.rand),
WithHTTPStatusCodes(f.cfg.httpStatusCodes...),
)
}
// HTTPStatusCode returns random HTTP status code
//
// faker.HTTPStatusCode(
// faker.WithRand(rand.New(rand.NewSource(time.Now().Unix()))), // Rand instance
// faker.WithHTTPStatusCodes(200, 201), // Slice of HTTP status code for RandomElement
// )
//
func HTTPStatusCode(opts ...Option) int {
cfg := newConfig(opts...)
if len(cfg.httpStatusCodes) == 0 {
WithHTTPStatusCodes(
http.StatusOK,
http.StatusCreated,
http.StatusNoContent,
http.StatusBadRequest,
http.StatusUnauthorized,
http.StatusForbidden,
http.StatusNotFound,
http.StatusConflict,
http.StatusInternalServerError,
)(cfg)
}
return RandomElement(cfg.httpStatusCodes, opts...)
}