@@ -3,13 +3,10 @@ package check
33import (
44 "fmt"
55 "io"
6- "io/ioutil"
76 "net/http"
87 "regexp"
98 "strings"
109 "time"
11-
12- "github.com/pkg/errors"
1310)
1411
1512// Option configures a check
@@ -62,7 +59,7 @@ func NewCheck(client *http.Client, url string, opts ...Option) *Check {
6259func (c * Check ) Run () error {
6360 req , err := http .NewRequest ("GET" , c .url , nil )
6461 if err != nil {
65- return errors . Wrap ( err , "Could not create request" )
62+ return fmt . Errorf ( "could not create request: %w" , err )
6663 }
6764
6865 req .SetBasicAuth (c .username , c .password )
@@ -71,7 +68,7 @@ func (c *Check) Run() error {
7168 resp , err := c .client .Do (req )
7269 if err != nil {
7370 if strings .Contains (err .Error (), "Timeout" ) {
74- return fmt .Errorf ("Timeout exceeded (%v)" , c .client .Timeout )
71+ return fmt .Errorf ("timeout exceeded (%v)" , c .client .Timeout )
7572 }
7673
7774 return err
@@ -96,7 +93,7 @@ func (c *Check) AssertStatusCodeIn(codes []uint32) {
9693 }
9794 }
9895
99- return fmt .Errorf ("Unexpected status code: %s (expected: %v)" , resp .Status , codes )
96+ return fmt .Errorf ("unexpected status code: %s (expected: %v)" , resp .Status , codes )
10097 })
10198}
10299
@@ -105,7 +102,7 @@ func (c *Check) AssertHeaderExists(name, value string) {
105102 c .assertions = append (c .assertions , func (resp * http.Response ) error {
106103 h := resp .Header .Get (name )
107104 if h != value {
108- return fmt .Errorf ("Expected header '%s' with value '%v'" , name , value )
105+ return fmt .Errorf ("expected header '%s' with value '%v'" , name , value )
109106 }
110107
111108 return nil
@@ -115,13 +112,13 @@ func (c *Check) AssertHeaderExists(name, value string) {
115112// AssertBodyContains tests if the body contains the specified string
116113func (c * Check ) AssertBodyContains (s string ) {
117114 c .assertions = append (c .assertions , func (resp * http.Response ) error {
118- b , err := ioutil .ReadAll (resp .Body )
115+ b , err := io .ReadAll (resp .Body )
119116 if err != nil {
120- return errors . Wrap ( err , "Could not read body" )
117+ return fmt . Errorf ( "could not read body: %w" , err )
121118 }
122119
123120 if ! strings .Contains (string (b ), s ) {
124- return fmt .Errorf ("String '%s' not found in body" , s )
121+ return fmt .Errorf ("string '%s' not found in body" , s )
125122 }
126123
127124 return nil
@@ -131,18 +128,18 @@ func (c *Check) AssertBodyContains(s string) {
131128// AssertBodyMatches tests if the body matches the specified regex
132129func (c * Check ) AssertBodyMatches (regex string ) {
133130 c .assertions = append (c .assertions , func (resp * http.Response ) error {
134- b , err := ioutil .ReadAll (resp .Body )
131+ b , err := io .ReadAll (resp .Body )
135132 if err != nil {
136- return errors . Wrap ( err , "Could not read body" )
133+ return fmt . Errorf ( "could not read body: %w" , err )
137134 }
138135
139136 r , err := regexp .Compile (regex )
140137 if err != nil {
141- return errors . Wrap ( err , "Invalid regex" )
138+ return fmt . Errorf ( "invalid regex: %w" , err )
142139 }
143140
144141 if ! r .Match (b ) {
145- return fmt .Errorf ("Regex '%s' does not match body" , regex )
142+ return fmt .Errorf ("regex '%s' does not match body" , regex )
146143 }
147144
148145 return nil
@@ -153,13 +150,13 @@ func (c *Check) AssertBodyMatches(regex string) {
153150func (c * Check ) AssertCertificateExpireDays (d time.Duration ) {
154151 c .assertions = append (c .assertions , func (resp * http.Response ) error {
155152 if resp .TLS == nil || len (resp .TLS .PeerCertificates ) == 0 {
156- return fmt .Errorf ("No certificate returned" )
153+ return fmt .Errorf ("no certificate returned" )
157154 }
158155
159156 first := resp .TLS .PeerCertificates [0 ]
160157 min := time .Now ().Add (d )
161158 if ! first .NotAfter .After (min ) {
162- return fmt .Errorf ("Certificate expires on %v" , first .NotAfter )
159+ return fmt .Errorf ("certificate expires on %v" , first .NotAfter )
163160 }
164161
165162 return nil
0 commit comments