diff --git a/cmd/run.go b/cmd/run.go index 6652350..a590af9 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -9,6 +9,7 @@ import ( "github.com/eynopv/lac/pkg/param" "github.com/eynopv/lac/pkg/printer" "github.com/eynopv/lac/pkg/request" + "github.com/eynopv/lac/pkg/request/authentication" "github.com/eynopv/lac/pkg/utils" "github.com/eynopv/lac/pkg/validators" ) @@ -33,7 +34,7 @@ func runCommandFunction( os.Exit(1) } - auth, err := request.NewAuth(requestTemplate) + auth, err := authentication.NewAuth(requestTemplate) if err != nil { fmt.Println(err) os.Exit(1) @@ -47,7 +48,7 @@ func runRequest( variables map[string]interface{}, headers map[string]string, client *client.Client, - auth request.Auth, + auth authentication.Auth, ) { resolvedHeaders := map[string]request.StringOrStringList{} for key, value := range headers { diff --git a/docs/json-schemas/request.schema.json b/docs/json-schemas/request.schema.json index ffa65fa..0a0fb9d 100644 --- a/docs/json-schemas/request.schema.json +++ b/docs/json-schemas/request.schema.json @@ -8,6 +8,24 @@ "auth": { "description": "Request authentication", "oneOf": [ + { + "type": "object", + "description": "API authentication", + "properties": { + "type": { + "const": "api" + }, + "header": { + "type": "string", + "description": "Header for api authentication" + }, + "key": { + "type": "string", + "description": "Key for api authentication" + } + }, + "required": ["type", "header", "key"] + }, { "type": "object", "description": "Basic authentication", diff --git a/internal/errorsx/errorsx.go b/internal/errorsx/errorsx.go index 49b6db4..5643ade 100644 --- a/internal/errorsx/errorsx.go +++ b/internal/errorsx/errorsx.go @@ -4,6 +4,8 @@ import "errors" var ErrAuthUnknown = errors.New("unknown auth type") var ErrAuthParse = errors.New("failed to parse auth") +var ErrApiAuthParse = errors.New("failed to parse api auth") +var ErrApiAuthInvalid = errors.New("header and key are required") var ErrBasicAuthParse = errors.New("failed to parse basic auth") var ErrBasicAuthInvalid = errors.New("username and password are required") var ErrBearerAuthParse = errors.New("failed to parse bearer auth") diff --git a/pkg/client/client.go b/pkg/client/client.go index f0cb925..88471c6 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -7,6 +7,7 @@ import ( "github.com/eynopv/lac/pkg/printer" "github.com/eynopv/lac/pkg/request" + "github.com/eynopv/lac/pkg/request/authentication" "github.com/eynopv/lac/pkg/result" ) @@ -30,7 +31,7 @@ func NewClient(config *ClientConfig) *Client { } } -func (c *Client) Do(r *request.Request, auth request.Auth) (*result.Result, error) { +func (c *Client) Do(r *request.Request, auth authentication.Auth) (*result.Result, error) { request, err := r.ToHttpRequest() if err != nil { return nil, err diff --git a/pkg/request/authentication.go b/pkg/request/authentication.go deleted file mode 100644 index 9f99808..0000000 --- a/pkg/request/authentication.go +++ /dev/null @@ -1,128 +0,0 @@ -package request - -import ( - "encoding/json" - "fmt" - "net/http" - - "gopkg.in/yaml.v3" - - "github.com/eynopv/lac/internal/errorsx" -) - -type AuthType string - -const ( - Basic AuthType = "basic" - Bearer AuthType = "bearer" -) - -type Auth interface { - Apply(r *http.Request) - GetType() AuthType -} - -type AuthBase struct { - Type AuthType `json:"type" yaml:"type"` -} - -func NewAuth(t *Template) (Auth, error) { - var wrapper struct { - Auth *AuthBase `json:"auth" yaml:"auth"` - } - - bs := []byte(*t) - - err := json.Unmarshal(bs, &wrapper) - if err != nil { - if err = yaml.Unmarshal(bs, &wrapper); err != nil { - return nil, fmt.Errorf("%w: %v", errorsx.ErrAuthParse, err) - } - } - - if wrapper.Auth == nil { - return nil, nil - } - - switch wrapper.Auth.Type { - case Basic: - return NewBasicAuth(t) - case Bearer: - return NewBearerAuth(t) - default: - return nil, errorsx.ErrAuthUnknown - } -} - -type BasicAuth struct { - Username string `json:"username" yaml:"username"` - Password string `json:"password" yaml:"password"` -} - -func NewBasicAuth(t *Template) (*BasicAuth, error) { - var wrapper struct { - Auth *BasicAuth `json:"auth" yaml:"auth"` - } - - bs := []byte(*t) - - err := json.Unmarshal(bs, &wrapper) - if err != nil { - err = yaml.Unmarshal(bs, &wrapper) - } - - if err != nil { - return nil, fmt.Errorf("%w: %v", errorsx.ErrBasicAuthParse, err) - } - - if wrapper.Auth != nil && (wrapper.Auth.Username == "" || wrapper.Auth.Password == "") { - return nil, errorsx.ErrBasicAuthInvalid - } - - return wrapper.Auth, nil -} - -func (a *BasicAuth) Apply(r *http.Request) { - if a != nil { - r.SetBasicAuth(a.Username, a.Password) - } -} - -func (a *BasicAuth) GetType() AuthType { - return Basic -} - -type BearerAuth struct { - Token string `json:"token"` -} - -func NewBearerAuth(t *Template) (*BearerAuth, error) { - var wrapper struct { - Auth *BearerAuth `json:"auth" yaml:"auth"` - } - - bs := []byte(*t) - - err := json.Unmarshal(bs, &wrapper) - if err != nil { - if err = yaml.Unmarshal(bs, &wrapper); err != nil { - return nil, fmt.Errorf("%w: %v", errorsx.ErrBearerAuthParse, err) - } - } - - if wrapper.Auth != nil && wrapper.Auth.Token == "" { - return nil, errorsx.ErrBearerAuthInvalid - } - - return wrapper.Auth, nil -} - -func (a *BearerAuth) GetType() AuthType { - return Bearer -} - -func (a *BearerAuth) Apply(r *http.Request) { - if a != nil { - r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", a.Token)) - } -} diff --git a/pkg/request/authentication/api.go b/pkg/request/authentication/api.go new file mode 100644 index 0000000..81a7913 --- /dev/null +++ b/pkg/request/authentication/api.go @@ -0,0 +1,47 @@ +package authentication + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/eynopv/lac/internal/errorsx" + "github.com/eynopv/lac/pkg/request" + "gopkg.in/yaml.v3" +) + +type ApiAuth struct { + Header string `json:"header" yaml:"header"` + Key string `json:"key" yaml:"key"` +} + +func NewApiAuth(t *request.Template) (*ApiAuth, error) { + var wrapper struct { + Auth *ApiAuth `json:"auth" yaml:"auth"` + } + + bs := []byte(*t) + + err := json.Unmarshal(bs, &wrapper) + if err != nil { + err = yaml.Unmarshal(bs, &wrapper) + } + + if err != nil { + return nil, fmt.Errorf("%w: %v", errorsx.ErrApiAuthParse, err) + } + + if wrapper.Auth != nil && (wrapper.Auth.Header == "" || wrapper.Auth.Key == "") { + return nil, errorsx.ErrApiAuthInvalid + } + + return wrapper.Auth, nil +} + +func (a *ApiAuth) Apply(r *http.Request) { + r.Header.Set(a.Header, a.Key) +} + +func (a *ApiAuth) GetType() AuthType { + return Api +} diff --git a/pkg/request/authentication/api_test.go b/pkg/request/authentication/api_test.go new file mode 100644 index 0000000..ffcc27b --- /dev/null +++ b/pkg/request/authentication/api_test.go @@ -0,0 +1,82 @@ +package authentication + +import ( + "net/http" + "testing" + + "github.com/eynopv/lac/internal/assert" + "github.com/eynopv/lac/pkg/request" +) + +func TestNewApiAuth(t *testing.T) { + t.Run("yaml", func(t *testing.T) { + template := request.Template(` + auth: + header: x-api-key + key: helloworld + `) + + auth, err := NewApiAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.Header, "x-api-key") + assert.Equal(t, auth.Key, "helloworld") + }) + + t.Run("json", func(t *testing.T) { + template := request.Template(` + { + "auth": { + "header": "x-api-key", + "key": "helloworld" + } + } + `) + + auth, err := NewApiAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.Header, "x-api-key") + assert.Equal(t, auth.Key, "helloworld") + }) + + t.Run("invalid", func(t *testing.T) { + template := request.Template("this is invalid template") + + auth, err := NewApiAuth(&template) + + assert.Error(t, err) + assert.Nil(t, auth) + }) + + t.Run("template without auth", func(t *testing.T) { + template := request.Template(` + { + "hello": "world" + } + `) + + auth, err := NewApiAuth(&template) + + assert.NoError(t, err) + assert.True(t, auth == nil) + }) +} + +func TestApiAuthApply(t *testing.T) { + apiAuth := ApiAuth{ + Header: "x-api-key", + Key: "helloworld", + } + + request, err := http.NewRequest(http.MethodGet, "", nil) + + assert.NotNil(t, request) + assert.NoError(t, err) + + apiAuth.Apply(request) + + assert.Equal(t, request.Header.Get("X-Api-Key"), "helloworld") +} diff --git a/pkg/request/authentication/authentication.go b/pkg/request/authentication/authentication.go new file mode 100644 index 0000000..329066d --- /dev/null +++ b/pkg/request/authentication/authentication.go @@ -0,0 +1,59 @@ +package authentication + +import ( + "encoding/json" + "fmt" + "net/http" + + "gopkg.in/yaml.v3" + + "github.com/eynopv/lac/internal/errorsx" + "github.com/eynopv/lac/pkg/request" +) + +type AuthType string + +const ( + Api AuthType = "api" + Basic AuthType = "basic" + Bearer AuthType = "bearer" +) + +type Auth interface { + Apply(r *http.Request) + GetType() AuthType +} + +type AuthBase struct { + Type AuthType `json:"type" yaml:"type"` +} + +func NewAuth(t *request.Template) (Auth, error) { + var wrapper struct { + Auth *AuthBase `json:"auth" yaml:"auth"` + } + + bs := []byte(*t) + + err := json.Unmarshal(bs, &wrapper) + if err != nil { + if err = yaml.Unmarshal(bs, &wrapper); err != nil { + return nil, fmt.Errorf("%w: %v", errorsx.ErrAuthParse, err) + } + } + + if wrapper.Auth == nil { + return nil, nil + } + + switch wrapper.Auth.Type { + case Api: + return NewApiAuth(t) + case Basic: + return NewBasicAuth(t) + case Bearer: + return NewBearerAuth(t) + default: + return nil, errorsx.ErrAuthUnknown + } +} diff --git a/pkg/request/authentication/authentication_test.go b/pkg/request/authentication/authentication_test.go new file mode 100644 index 0000000..21f9b3a --- /dev/null +++ b/pkg/request/authentication/authentication_test.go @@ -0,0 +1,97 @@ +package authentication + +import ( + "testing" + + "github.com/eynopv/lac/internal/assert" + "github.com/eynopv/lac/internal/errorsx" + "github.com/eynopv/lac/pkg/request" +) + +func TestNewAuth(t *testing.T) { + t.Run("yaml", func(t *testing.T) { + template := request.Template(` + auth: + type: unknown + `) + + auth, err := NewAuth(&template) + + assert.Error(t, err) + assert.Equal(t, err.Error(), errorsx.ErrAuthUnknown.Error()) + assert.Nil(t, auth) + }) + + t.Run("json", func(t *testing.T) { + template := request.Template(` + { + "auth": { + "type": "unknown" + } + } + `) + + auth, err := NewAuth(&template) + + assert.Error(t, err) + assert.Equal(t, err.Error(), errorsx.ErrAuthUnknown.Error()) + assert.Nil(t, auth) + }) + + t.Run("no auth", func(t *testing.T) { + template := request.Template(` + { + "hello": "world" + } + `) + + auth, err := NewAuth(&template) + + assert.NoError(t, err) + assert.Nil(t, auth) + }) + + t.Run("basic", func(t *testing.T) { + template := request.Template(` + auth: + type: basic + username: hello + password: world + `) + + auth, err := NewAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.GetType(), Basic) + }) + + t.Run("bearer", func(t *testing.T) { + template := request.Template(` + auth: + type: bearer + token: helloworld + `) + + auth, err := NewAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.GetType(), Bearer) + }) + + t.Run("api", func(t *testing.T) { + template := request.Template(` + auth: + type: api + header: x-api-key + key: helloworld + `) + + auth, err := NewAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.GetType(), Api) + }) +} diff --git a/pkg/request/authentication/basic.go b/pkg/request/authentication/basic.go new file mode 100644 index 0000000..d033f33 --- /dev/null +++ b/pkg/request/authentication/basic.go @@ -0,0 +1,50 @@ +package authentication + +import ( + "encoding/json" + "fmt" + "net/http" + + "gopkg.in/yaml.v3" + + "github.com/eynopv/lac/internal/errorsx" + "github.com/eynopv/lac/pkg/request" +) + +type BasicAuth struct { + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` +} + +func NewBasicAuth(t *request.Template) (*BasicAuth, error) { + var wrapper struct { + Auth *BasicAuth `json:"auth" yaml:"auth"` + } + + bs := []byte(*t) + + err := json.Unmarshal(bs, &wrapper) + if err != nil { + err = yaml.Unmarshal(bs, &wrapper) + } + + if err != nil { + return nil, fmt.Errorf("%w: %v", errorsx.ErrBasicAuthParse, err) + } + + if wrapper.Auth != nil && (wrapper.Auth.Username == "" || wrapper.Auth.Password == "") { + return nil, errorsx.ErrBasicAuthInvalid + } + + return wrapper.Auth, nil +} + +func (a *BasicAuth) Apply(r *http.Request) { + if a != nil { + r.SetBasicAuth(a.Username, a.Password) + } +} + +func (a *BasicAuth) GetType() AuthType { + return Basic +} diff --git a/pkg/request/authentication/basic_test.go b/pkg/request/authentication/basic_test.go new file mode 100644 index 0000000..8ef9d78 --- /dev/null +++ b/pkg/request/authentication/basic_test.go @@ -0,0 +1,138 @@ +package authentication + +import ( + "net/http" + "testing" + + "github.com/eynopv/lac/internal/assert" + "github.com/eynopv/lac/internal/errorsx" + "github.com/eynopv/lac/pkg/request" +) + +func TestNewBasicAuth(t *testing.T) { + t.Run("yaml", func(t *testing.T) { + data := ` +auth: + username: hello + password: world + ` + + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.Username, "hello") + assert.Equal(t, auth.Password, "world") + }) + + t.Run("json", func(t *testing.T) { + data := ` +{ + "auth": { + "username": "hello", + "password": "world" + } +} +` + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.Username, "hello") + assert.Equal(t, auth.Password, "world") + }) + + t.Run("invalid", func(t *testing.T) { + data := "this is invalid template" + + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.Error(t, err) + assert.Nil(t, auth) + }) + + t.Run("template without auth", func(t *testing.T) { + data := ` +{ + "hello": "world" +} +` + + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.NoError(t, err) + assert.True(t, auth == nil) + }) + + t.Run("only username", func(t *testing.T) { + data := ` +{ + "auth": { + "username": "hello" + } +} +` + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.Error(t, err) + assert.Equal(t, err.Error(), errorsx.ErrBasicAuthInvalid.Error()) + assert.Nil(t, auth) + }) + + t.Run("only password", func(t *testing.T) { + data := ` +{ + "auth": { + "password": "hello" + } +} +` + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.Error(t, err) + assert.Equal(t, err.Error(), errorsx.ErrBasicAuthInvalid.Error()) + assert.Nil(t, auth) + }) + + t.Run("empty username and password", func(t *testing.T) { + data := ` +{ + "auth": { + "username": "", + "password": "" + } +} +` + template := request.Template(data) + auth, err := NewBasicAuth(&template) + + assert.Error(t, err) + assert.Equal(t, err.Error(), errorsx.ErrBasicAuthInvalid.Error()) + assert.Nil(t, auth) + }) +} + +func TestBasicAuthApply(t *testing.T) { + basic := BasicAuth{ + Username: "hello", + Password: "world", + } + + request, err := http.NewRequest(http.MethodGet, "", nil) + + assert.NotNil(t, request) + assert.NoError(t, err) + + basic.Apply(request) + + username, password, ok := request.BasicAuth() + assert.True(t, ok) + assert.Equal(t, basic.Username, username) + assert.Equal(t, basic.Password, password) +} diff --git a/pkg/request/authentication/bearer.go b/pkg/request/authentication/bearer.go new file mode 100644 index 0000000..f54a1e9 --- /dev/null +++ b/pkg/request/authentication/bearer.go @@ -0,0 +1,47 @@ +package authentication + +import ( + "encoding/json" + "fmt" + "net/http" + + "gopkg.in/yaml.v3" + + "github.com/eynopv/lac/internal/errorsx" + "github.com/eynopv/lac/pkg/request" +) + +type BearerAuth struct { + Token string `json:"token" yaml:"token"` +} + +func NewBearerAuth(t *request.Template) (*BearerAuth, error) { + var wrapper struct { + Auth *BearerAuth `json:"auth" yaml:"auth"` + } + + bs := []byte(*t) + + err := json.Unmarshal(bs, &wrapper) + if err != nil { + if err = yaml.Unmarshal(bs, &wrapper); err != nil { + return nil, fmt.Errorf("%w: %v", errorsx.ErrBearerAuthParse, err) + } + } + + if wrapper.Auth != nil && wrapper.Auth.Token == "" { + return nil, errorsx.ErrBearerAuthInvalid + } + + return wrapper.Auth, nil +} + +func (a *BearerAuth) GetType() AuthType { + return Bearer +} + +func (a *BearerAuth) Apply(r *http.Request) { + if a != nil { + r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", a.Token)) + } +} diff --git a/pkg/request/authentication/bearer_test.go b/pkg/request/authentication/bearer_test.go new file mode 100644 index 0000000..f4ceafb --- /dev/null +++ b/pkg/request/authentication/bearer_test.go @@ -0,0 +1,77 @@ +package authentication + +import ( + "net/http" + "testing" + + "github.com/eynopv/lac/internal/assert" + "github.com/eynopv/lac/pkg/request" +) + +func TestNewBearerAuth(t *testing.T) { + t.Run("yaml", func(t *testing.T) { + template := request.Template(` + auth: + token: helloworld + `) + + auth, err := NewBearerAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.Token, "helloworld") + }) + + t.Run("json", func(t *testing.T) { + template := request.Template(` + { + "auth": { + "token": "helloworld" + } + } + `) + + auth, err := NewBearerAuth(&template) + + assert.NoError(t, err) + assert.NotNil(t, auth) + assert.Equal(t, auth.Token, "helloworld") + }) + + t.Run("invalid", func(t *testing.T) { + template := request.Template("this is invalid template") + + auth, err := NewBearerAuth(&template) + + assert.Error(t, err) + assert.Nil(t, auth) + }) + + t.Run("template without auth", func(t *testing.T) { + template := request.Template(` + { + "hello": "world" + } + `) + + auth, err := NewBearerAuth(&template) + + assert.NoError(t, err) + assert.True(t, auth == nil) + }) +} + +func TestBearerAuthApply(t *testing.T) { + bearer := BearerAuth{ + Token: "helloworld", + } + + request, err := http.NewRequest(http.MethodGet, "", nil) + + assert.NotNil(t, request) + assert.NoError(t, err) + + bearer.Apply(request) + + assert.Equal(t, request.Header.Get("Authorization"), "Bearer helloworld") +} diff --git a/pkg/request/authentication_test.go b/pkg/request/authentication_test.go deleted file mode 100644 index 5b5b5e8..0000000 --- a/pkg/request/authentication_test.go +++ /dev/null @@ -1,278 +0,0 @@ -package request - -import ( - "net/http" - "testing" - - "github.com/eynopv/lac/internal/assert" - "github.com/eynopv/lac/internal/errorsx" -) - -func TestNewAuth(t *testing.T) { - t.Run("yaml", func(t *testing.T) { - template := Template(` - auth: - type: unknown - `) - - auth, err := NewAuth(&template) - - assert.Error(t, err) - assert.Equal(t, err.Error(), errorsx.ErrAuthUnknown.Error()) - assert.Nil(t, auth) - }) - - t.Run("json", func(t *testing.T) { - template := Template(` - { - "auth": { - "type": "unknown", - } - } - `) - - auth, err := NewAuth(&template) - - assert.Error(t, err) - assert.Equal(t, err.Error(), errorsx.ErrAuthUnknown.Error()) - assert.Nil(t, auth) - }) - - t.Run("no auth", func(t *testing.T) { - template := Template(` - { - "hello": "world" - } - `) - - auth, err := NewAuth(&template) - - assert.NoError(t, err) - assert.Nil(t, auth) - }) - - t.Run("basic", func(t *testing.T) { - template := Template(` - auth: - type: basic - username: hello - password: world - `) - - auth, err := NewAuth(&template) - - assert.NoError(t, err) - assert.NotNil(t, auth) - assert.Equal(t, auth.GetType(), Basic) - }) - - t.Run("bearer", func(t *testing.T) { - template := Template(` - auth: - type: bearer - token: helloworld - `) - - auth, err := NewAuth(&template) - - assert.NoError(t, err) - assert.NotNil(t, auth) - assert.Equal(t, auth.GetType(), Bearer) - }) -} - -func TestNewBasicAuth(t *testing.T) { - t.Run("yaml", func(t *testing.T) { - data := ` -auth: - username: hello - password: world - ` - - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.NoError(t, err) - assert.NotNil(t, auth) - assert.Equal(t, auth.Username, "hello") - assert.Equal(t, auth.Password, "world") - }) - - t.Run("json", func(t *testing.T) { - data := ` -{ - "auth": { - "username": "hello", - "password": "world" - } -} -` - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.NoError(t, err) - assert.NotNil(t, auth) - assert.Equal(t, auth.Username, "hello") - assert.Equal(t, auth.Password, "world") - }) - - t.Run("invalid", func(t *testing.T) { - data := "this is invalid template" - - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.Error(t, err) - assert.Nil(t, auth) - }) - - t.Run("template without auth", func(t *testing.T) { - data := ` -{ - "hello": "world" -} -` - - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.NoError(t, err) - assert.True(t, auth == nil) - }) - - t.Run("only username", func(t *testing.T) { - data := ` -{ - "auth": { - "username": "hello" - } -} -` - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.Error(t, err) - assert.Equal(t, err.Error(), errorsx.ErrBasicAuthInvalid.Error()) - assert.Nil(t, auth) - }) - - t.Run("only password", func(t *testing.T) { - data := ` -{ - "auth": { - "password": "hello" - } -} -` - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.Error(t, err) - assert.Equal(t, err.Error(), errorsx.ErrBasicAuthInvalid.Error()) - assert.Nil(t, auth) - }) - - t.Run("empty username and password", func(t *testing.T) { - data := ` -{ - "auth": { - "username": "", - "password": "" - } -} -` - template := Template(data) - auth, err := NewBasicAuth(&template) - - assert.Error(t, err) - assert.Equal(t, err.Error(), errorsx.ErrBasicAuthInvalid.Error()) - assert.Nil(t, auth) - }) -} - -func TestBasicAuthApply(t *testing.T) { - basic := BasicAuth{ - Username: "hello", - Password: "world", - } - - request, err := http.NewRequest(http.MethodGet, "", nil) - - assert.NotNil(t, request) - assert.NoError(t, err) - - basic.Apply(request) - - username, password, ok := request.BasicAuth() - assert.True(t, ok) - assert.Equal(t, basic.Username, username) - assert.Equal(t, basic.Password, password) -} - -func TestNewBearerAuth(t *testing.T) { - t.Run("yaml", func(t *testing.T) { - template := Template(` - auth: - token: helloworld - `) - - auth, err := NewBearerAuth(&template) - - assert.NoError(t, err) - assert.NotNil(t, auth) - assert.Equal(t, auth.Token, "helloworld") - }) - - t.Run("json", func(t *testing.T) { - template := Template(` - { - "auth": { - "token": "helloworld" - } - } - `) - - auth, err := NewBearerAuth(&template) - - assert.NoError(t, err) - assert.NotNil(t, auth) - assert.Equal(t, auth.Token, "helloworld") - }) - - t.Run("invalid", func(t *testing.T) { - template := Template("this is invalid template") - - auth, err := NewBearerAuth(&template) - - assert.Error(t, err) - assert.Nil(t, auth) - }) - - t.Run("template without auth", func(t *testing.T) { - template := Template(` - { - "hello": "world" - } - `) - - auth, err := NewBearerAuth(&template) - - assert.NoError(t, err) - assert.True(t, auth == nil) - }) -} - -func TestBearerAuthApply(t *testing.T) { - bearer := BearerAuth{ - Token: "helloworld", - } - - request, err := http.NewRequest(http.MethodGet, "", nil) - - assert.NotNil(t, request) - assert.NoError(t, err) - - bearer.Apply(request) - - assert.Equal(t, request.Header.Get("Authorization"), "Bearer helloworld") -}