forked from h2non/baloo
/
header.go
80 lines (73 loc) · 2.27 KB
/
header.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
package assert
import (
"fmt"
"net/http"
"regexp"
)
// Header asserts a response header field value matches.
// Regular expressions can be used as value to perform the specific assertions.
func Header(key, value string) Func {
return func(res *http.Response, req *http.Request) error {
header := res.Header.Get(key)
if match, _ := regexp.MatchString(value, header); !match {
return fmt.Errorf("Header mismatch: '%s' should match '%s'", value, header)
}
return nil
}
}
// HeaderEquals asserts a response header field value
// is equal to the given value.
func HeaderEquals(key, value string) Func {
return func(res *http.Response, req *http.Request) error {
if header := res.Header.Get(key); header != value {
return fmt.Errorf("Header mismatch: '%s' == '%s'", value, header)
}
return nil
}
}
// HeaderNotEquals asserts a response header field
// with the given value.
func HeaderNotEquals(key, value string) Func {
return func(res *http.Response, req *http.Request) error {
if err := HeaderEquals(key, value)(res, req); err == nil {
header := res.Header.Get(key)
return fmt.Errorf("Header should not be equal: '%s' != '%s'", value, header)
}
return nil
}
}
// HeaderPresent asserts if a header field is present
// in the response.
func HeaderPresent(key string) Func {
return func(res *http.Response, req *http.Request) error {
if header := res.Header.Get(key); header == "" {
return fmt.Errorf("Header is not present: %s", key)
}
return nil
}
}
// HeaderNotPresent asserts if a header field is not
// present in the response.
func HeaderNotPresent(key string) Func {
return func(res *http.Response, req *http.Request) error {
if err := HeaderPresent(key)(res, req); err == nil {
return fmt.Errorf("Header should not be present: %s", err)
}
return nil
}
}
// RedirectTo asserts the server response redirects
// to the given URL pattern.
// Regular expressions are supported.
func RedirectTo(uri string) Func {
return func(res *http.Response, req *http.Request) error {
header := res.Header.Get("Location")
if header == "" {
return fmt.Errorf("Location headear is not present")
}
if match, _ := regexp.MatchString(uri, header); !match {
return fmt.Errorf("Invalid location header: '%s' should match '%s'", uri, header)
}
return nil
}
}