Skip to content

Commit

Permalink
feat(deployed_version): support for post requests
Browse files Browse the repository at this point in the history
* with/without body

fixes #397
  • Loading branch information
JosephKav committed May 7, 2024
1 parent 64dc504 commit 3340e18
Show file tree
Hide file tree
Showing 34 changed files with 532 additions and 232 deletions.
3 changes: 2 additions & 1 deletion config/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ func testServiceURL(id string) *service.Service {
&latestver.LookupDefaults{}, &latestver.LookupDefaults{}),
DeployedVersionLookup: deployedver.New(
test.BoolPtr(false),
nil, nil,
nil, nil, nil,
"version",
"GET",
nil, "", nil, nil,
"https://valid.release-argus.io/json",
&deployedver.LookupDefaults{}, &deployedver.LookupDefaults{}),
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"standard-version": "^9.5.0"
},
"dependencies": {
"@commitlint/cli": "^19.2.2",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2"
}
}
11 changes: 11 additions & 0 deletions service/deployed_version/gets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package deployedver

import (
"io"
"strings"

"github.com/release-argus/Argus/util"
)

Expand All @@ -30,3 +33,11 @@ func (l *Lookup) GetAllowInvalidCerts() bool {
func (l *Lookup) GetURL() string {
return util.EvalEnvVars(l.URL)
}

// GetBody will return the Body of the Lookup.
func (l *Lookup) GetBody() io.Reader {
if l.Body == nil {
return nil
}
return strings.NewReader(*l.Body)
}
38 changes: 38 additions & 0 deletions service/deployed_version/gets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package deployedver

import (
"io"
"os"
"reflect"
"strings"
"testing"

"github.com/release-argus/Argus/test"
Expand Down Expand Up @@ -112,3 +115,38 @@ func TestLookup_GetURL(t *testing.T) {
})
}
}

func TestLookup_GetBody(t *testing.T) {
// GIVEN a Lookup
tests := map[string]struct {
body *string
want io.Reader
}{
"nil body": {
body: nil,
want: nil,
},
"non-nil body": {
body: test.StringPtr("test body"),
want: strings.NewReader("test body"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

lookup := testLookup()
lookup.Body = tc.body

// WHEN GetBody is called
got := lookup.GetBody()

// THEN the function returns the correct result
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("want: %v\ngot: %v",
tc.want, got)
}
})
}
}
2 changes: 2 additions & 0 deletions service/deployed_version/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func testLookup() (lookup *Lookup) {
test.BoolPtr(true),
nil,
nil,
nil,
"version",
"GET",
opt.New(
nil, "", test.BoolPtr(true),
&opt.OptionsDefaults{},
Expand Down
6 changes: 3 additions & 3 deletions service/deployed_version/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,23 @@ func (l *Lookup) httpRequest(logFrom *util.LogFrom) (rawBody []byte, err error)
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}

req, err := http.NewRequest(http.MethodGet, l.GetURL(), nil)
// Create the request.
req, err := http.NewRequest(l.Method, l.GetURL(), l.GetBody())
if err != nil {
jLog.Error(err, logFrom, true)
return
}

// Set headers
req.Header.Set("Connection", "close")
for _, header := range l.Headers {
req.Header.Set(util.EvalEnvVars(header.Key), util.EvalEnvVars(header.Value))
}

// Basic auth
if l.BasicAuth != nil {
req.SetBasicAuth(util.EvalEnvVars(l.BasicAuth.Username), util.EvalEnvVars(l.BasicAuth.Password))
}

// Send the request.
client := &http.Client{Transport: customTransport}
resp, err := client.Do(req)
if err != nil {
Expand Down
Loading

0 comments on commit 3340e18

Please sign in to comment.