-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbody_equal.go
42 lines (35 loc) · 903 Bytes
/
body_equal.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
package check
import (
"context"
"fmt"
"net/http"
)
// UnexpectedValueError is returned when a check fails.
type UnexpectedValueError struct {
// Expected is the expected value.
Expected string
// Actual is the actual value.
Actual string
}
// Error returns an error string.
func (e *UnexpectedValueError) Error() string {
return fmt.Sprintf("unexpected value: expected %v, got %v", e.Expected, e.Actual)
}
// BodyEqualChecker is used to validate the http response body string exactly matches `Value`
type BodyEqualChecker struct {
Value string
}
// Check performs the BodyEqual check
func (c *BodyEqualChecker) Check(ctx context.Context, response *http.Response) error {
body, err := readResponseBody(response)
if err != nil {
return err
}
if exp, got := c.Value, string(body); exp != got {
return &UnexpectedValueError{
Expected: exp,
Actual: got,
}
}
return nil
}