Skip to content

Commit

Permalink
fix(api): Sent user-agent for login as well (#440)
Browse files Browse the repository at this point in the history
* fix(api): Sent user-agent for login as well

* chore(all): Replace deprecated io/ioutil package

* fix(api): Set user-agent for HAR requests
  • Loading branch information
zeisss authored May 6, 2024
1 parent 9639597 commit 2b6e895
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 32 deletions.
7 changes: 3 additions & 4 deletions api/file_fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"io"
"io/ioutil"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -31,7 +30,7 @@ func (c *Client) MoveFileFixture(organization string, fileFixtureUID string, new
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -83,7 +82,7 @@ func (c *Client) PushFileFixture(fileName string, data io.Reader, organization s
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, nil, err
}
Expand All @@ -110,7 +109,7 @@ func (c *Client) DeleteFileFixture(fileFixtureUID string, organization string) (
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand Down
7 changes: 4 additions & 3 deletions api/har.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
)

Expand All @@ -14,7 +13,7 @@ import (
func (c *Client) Har(fileName string, data io.Reader) (string, error) {
extraParams := url.Values{}

input, err := ioutil.ReadAll(data)
input, err := io.ReadAll(data)
if err != nil {
return "", fmt.Errorf("%s: %v", fileName, err)
}
Expand All @@ -28,12 +27,14 @@ func (c *Client) Har(fileName string, data io.Reader) (string, error) {
return "", err
}

c.setUserAgent(req)

resp, err := c.HTTPClient.Do(req)
if err != nil {
return "", err
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
11 changes: 6 additions & 5 deletions api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
)

// Login acquires a JWT access token for the given email/password
func (c *Client) Login(email string, password string) (string, error) {
data := map[string]string{"email": email, "password": password}

body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(data)
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(data)
if err != nil {
return "", err
}

req, err := http.NewRequest("POST", c.APIEndpoint+"/user/token", body)
req, err := http.NewRequest("POST", c.APIEndpoint+"/user/token", &body)
if err != nil {
return "", err
}

req.Header.Set("Content-Type", "application/json")
c.setUserAgent(req)

resp, err := c.HTTPClient.Do(req)
if err != nil {
Expand All @@ -32,7 +33,7 @@ func (c *Client) Login(email string, password string) (string, error) {

defer close(resp.Body)

responseBody, err := ioutil.ReadAll(resp.Body)
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions api/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -31,7 +31,7 @@ func (c *Client) CreateServiceAccount(org, token_label string) (bool, string, er
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand Down
7 changes: 3 additions & 4 deletions api/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"io"
"io/ioutil"
"net/url"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func (c *Client) TestCaseValidate(organization string, fileName string, data io.
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -74,7 +73,7 @@ func (c *Client) TestCaseCreate(organization string, testCaseName string, fileNa
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -107,7 +106,7 @@ func (c *Client) TestCaseUpdate(testCaseUID string, fileName string, data io.Rea
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand Down
11 changes: 5 additions & 6 deletions api/testrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (c *Client) TestRunWatch(uid string) (testrun.TestRun, string, error) {
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return testrun.TestRun{}, "", err
}
Expand Down Expand Up @@ -239,7 +238,7 @@ func (c *Client) TestRunCreate(testCaseUID string, options TestRunLaunchOptions)
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand All @@ -263,7 +262,7 @@ func (c *Client) TestRunAbort(testRunUID string) (bool, string, error) {
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, "", err
}
Expand All @@ -290,7 +289,7 @@ func (c *Client) TestRunAbortAll(organisationUID string) (bool, string, error) {

defer close(resp.Body)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, "", err
}
Expand All @@ -316,7 +315,7 @@ func (c *Client) TestRunNfrCheck(uid string, fileName string, data io.Reader) (b
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return false, nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -62,7 +61,7 @@ func runLogin(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

err = ioutil.WriteFile(stormforgerConfig, content, 0644)
err = os.WriteFile(stormforgerConfig, content, 0644)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/testcase_build_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestTestcaseBuild__WithUndefinedVariable(t *testing.T) {
func GivenTestdataContents(t *testing.T, file string) string {
p := filepath.Join("testdata", file)

data, err := ioutil.ReadFile(p)
data, err := os.ReadFile(p)
require.NoError(t, err)

return string(data)
Expand Down
3 changes: 1 addition & 2 deletions cmd/testcase_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -56,7 +55,7 @@ func runTestCaseGet(cmd *cobra.Command, args []string) {
}

if len(args) == 2 && args[1] != "-" {
err := ioutil.WriteFile(args[1], response, 0644)
err := os.WriteFile(args[1], response, 0644)
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/testcase_launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -243,7 +242,7 @@ func MainTestRunLaunch(client *api.Client, testCaseSpec string, testRunLaunchOpt

if testRunLaunchOpts.TestRunIDOutputFile != "" {
f := testRunLaunchOpts.TestRunIDOutputFile
err := ioutil.WriteFile(f, []byte(testRun.ID), 0644)
err := os.WriteFile(f, []byte(testRun.ID), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write file: %v\n", err)
os.Exit(1)
Expand Down

0 comments on commit 2b6e895

Please sign in to comment.