diff --git a/README.md b/README.md index 9b29a7c..797d5b5 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,15 @@ client: method: GET # Timeout is in millisecond, default is 0 (no timeout) timeout: 1000 - # Status to check, default 200 + # Headers is the HTTP headers to use headers: Accept: application/json Content-Type: application/json Authorization: Bearer ABCDEFG + # BasicAuth is the basic auth to use + basicAuth: + username: "username" + password: "password" respond: # Status to check, default 200 status: 200 diff --git a/internal/model/rest.go b/internal/model/rest.go index 35dcce1..e71ddda 100644 --- a/internal/model/rest.go +++ b/internal/model/rest.go @@ -21,6 +21,13 @@ type RestCheckBody struct { Map *string `json:"map" yaml:"map"` } +type RestBasicAuth struct { + // Username is the username + Username string `json:"username" yaml:"username"` + // Password is the password + Password string `json:"password" yaml:"password"` +} + type RestRequest struct { // URL could be multiple URLs, separated by space URL string `json:"url" yaml:"url"` @@ -30,6 +37,8 @@ type RestRequest struct { Timeout int `json:"timeout" yaml:"timeout"` // Headers is the HTTP headers to be used Headers map[string]string `json:"headers" yaml:"headers"` + // BasicAuth is the basic auth to be used + BasicAuth *RestBasicAuth `json:"basicAuth" yaml:"basicAuth"` } type RestRespond struct { diff --git a/internal/route/rest/call_test.go b/internal/route/rest/call_test.go index 6f1db5f..08034ef 100644 --- a/internal/route/rest/call_test.go +++ b/internal/route/rest/call_test.go @@ -50,7 +50,7 @@ func TestRequest(t *testing.T) { Request: model.RestRequest{ Timeout: 2, Method: "GET", - URL: "/abc /xyz", + URL: "/abc /xyz /def /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p /q /r /s /t /u /v /w /x /y /z", }, Respond: model.RestRespond{ Status: 200, @@ -69,7 +69,7 @@ func TestRequest(t *testing.T) { args: args{ check: &model.RestCheck{ Request: model.RestRequest{ - URL: "/abc /xyz", + URL: "/abc /xyz /def /a /b /c /d /e /f /g /h /i /j /k /l /m /n /o /p /q /r /s /t /u /v /w /x /y /z", Method: "GET", Timeout: 2, }, @@ -78,7 +78,7 @@ func TestRequest(t *testing.T) { }, }, }, - concurrent: 2, + concurrent: 20, want: `status code: 502; want: 200`, wantIn: true, handler: func(w http.ResponseWriter, r *http.Request) { @@ -117,8 +117,12 @@ func TestRequest(t *testing.T) { } var handlerFunc = func(w http.ResponseWriter, r *http.Request) {} + mx := sync.RWMutex{} srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mx.RLock() + defer mx.RUnlock() + if handlerFunc != nil { handlerFunc(w, r) } @@ -130,7 +134,10 @@ func TestRequest(t *testing.T) { errs := ®istry.Errors{} reg := registry.NewClientReg(errs, nil) + mx.Lock() handlerFunc = tt.handler + mx.Unlock() + urls := strings.Split(tt.args.check.Request.URL, " ") for i, url := range urls { urls[i] = fmt.Sprintf("%s%s", srv.URL, url) diff --git a/internal/route/rest/client.go b/internal/route/rest/client.go index 53129ea..1d2d419 100644 --- a/internal/route/rest/client.go +++ b/internal/route/rest/client.go @@ -78,6 +78,11 @@ func (c *ClientHolder) DoRequest(ctx context.Context, timeout time.Duration, url req.Header.Set(k, v) } + // add basic auth + if m.Request.BasicAuth != nil { + req.SetBasicAuth(m.Request.BasicAuth.Username, m.Request.BasicAuth.Password) + } + resp, err := c.Client.Do(req) if err != nil { return fmt.Errorf("%s, doing request: %w", urlV, err) diff --git a/testdata/testBasic.yaml b/testdata/testBasic.yaml new file mode 100644 index 0000000..411ca21 --- /dev/null +++ b/testdata/testBasic.yaml @@ -0,0 +1,13 @@ +log_level: "warn" +client: + rest: + - concurrent: 1 + check: + - request: + url: "http://httpbin.org/basic-auth/user/passwd" + timeout: 1000 + basicAuth: + username: "user" + password: "passwd" + respond: + status: 200