-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
123 lines (102 loc) · 3.23 KB
/
Copy pathapi_test.go
File metadata and controls
123 lines (102 loc) · 3.23 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
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
package httpassert_test
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
ha "github.com/korya/http-assert"
)
type exampleTransport func(*http.Request) (*http.Response, error)
func (f exampleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
type customAssertion struct{}
func (customAssertion) Kind() ha.AssertionKind { return "custom" }
func (customAssertion) Check(*ha.Response) (*ha.Failure, error) { return nil, nil }
var _ ha.Assertion = customAssertion{}
func ExampleClient_Do() {
req := ha.Must(http.NewRequest(http.MethodGet, "https://example.test/health", nil))
client := ha.Client{HTTPClient: &http.Client{Transport: exampleTransport(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusNoContent,
Status: "204 No Content",
Header: make(http.Header),
Body: http.NoBody,
Request: req,
}, nil
})}}
result, err := client.Do(req, ha.AssertStatusOK(), ha.AssertBodyEmpty())
fmt.Println(err)
fmt.Println(result.Passed())
// Output:
// <nil>
// true
}
func ExampleMust() {
req := ha.Must(http.NewRequest(http.MethodGet, "https://example.test/health", nil))
assertion := ha.Must(ha.AssertJQ(`.status == "healthy"`))
fmt.Println(req.Method)
fmt.Println(assertion.Kind())
// Output:
// GET
// jq
}
func ExampleOutcome() {
req := ha.Must(http.NewRequest(http.MethodGet, "https://example.test/health", nil))
client := ha.Client{HTTPClient: &http.Client{Transport: exampleTransport(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Status: "500 Internal Server Error",
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("not JSON")),
Request: req,
}, nil
})}}
result, err := client.Do(req, ha.AssertStatusOK(), ha.Must(ha.AssertJQ(".healthy")))
if err != nil {
fmt.Println(err)
return
}
for _, outcome := range result.Outcomes {
switch {
case outcome.Err != nil:
var evaluation *ha.EvaluationError
if errors.As(outcome.Err, &evaluation) {
fmt.Printf("%s could not be evaluated: %s\n", outcome.Kind, evaluation.Code)
} else {
fmt.Printf("%s could not be evaluated: %v\n", outcome.Kind, outcome.Err)
}
case outcome.Failure != nil:
fmt.Printf("%s failed: %s (got %v)\n", outcome.Kind, outcome.Failure.Code, outcome.Failure.Actual)
}
}
// Output:
// ok failed: status_ok (got 500)
// jq could not be evaluated: json_decode
}
func ExampleClient_customHTTPPolicy() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req := ha.Must(http.NewRequestWithContext(ctx, http.MethodGet, "https://example.test/health", nil))
client := ha.Client{HTTPClient: &http.Client{
Timeout: 3 * time.Second,
Transport: exampleTransport(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("healthy")),
Request: req,
}, nil
}),
}}
result, err := client.Do(req, ha.AssertStatusOK())
fmt.Println(err)
fmt.Println(string(result.Response.BodyBytes))
// Output:
// <nil>
// healthy
}