Skip to content

Commit

Permalink
feat: basic auth added
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <eates23@gmail.com>
  • Loading branch information
rytsh committed Sep 30, 2022
1 parent a7e70a0 commit 0909faa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/model/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions internal/route/rest/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -130,7 +134,10 @@ func TestRequest(t *testing.T) {
errs := &registry.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)
Expand Down
5 changes: 5 additions & 0 deletions internal/route/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions testdata/testBasic.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0909faa

Please sign in to comment.