-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvhttp_example_test.go
More file actions
57 lines (47 loc) · 1.29 KB
/
vhttp_example_test.go
File metadata and controls
57 lines (47 loc) · 1.29 KB
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
package vhttp_test
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"github.com/a-poor/vhttp"
)
func asReadCloser(b []byte) io.ReadCloser {
return io.NopCloser(bytes.NewReader(b))
}
func ExampleValidateRequest() {
// Create a sample request...
u, _ := url.Parse("https://example.com/api/v1/users")
req := &http.Request{
Method: http.MethodPost,
Header: http.Header{
"Content-Type": []string{"application/json"},
"Authorization": []string{"Basic abcde12345"},
},
Body: asReadCloser([]byte(`{{{{`)),
URL: u,
}
// Validate the request...
err := vhttp.ValidateRequest(req,
// Is a "GET" request
vhttp.MethodIsGet(),
// Calling json.Valid() on the body returns true
vhttp.BodyIsValidJSON(),
// Has the header "Content-Type" and it's equal to "application/json"
vhttp.HeaderContentTypeJSON(),
// The header "Authorization" matches the regular expression ^Bearer .+$
vhttp.HeaderAuthorizationMatchesBearer(),
// Has the URL path "/api/v2/posts"
vhttp.URLPathIs("/api/v2/posts"),
)
// Print the output...
fmt.Println(err)
// Output:
// 4 errors occurred:
// * expected method "GET", found "POST"
// * body is not valid JSON
// * expected header "Authorization" to match "^Bearer .+$"
// * expected URL path "/api/v2/posts", found "/api/v1/users"
//
}