Skip to content

Commit 8606ca2

Browse files
committed
Fix code issue in hoverfly
1 parent 7453cc4 commit 8606ca2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+238
-279
lines changed

core/action/action.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7-
"io/ioutil"
87
"net/http"
98
"net/url"
109
"os"
@@ -60,7 +59,7 @@ func setScript(action *Action, scriptContent string) error {
6059
tempDir := path.Join(os.TempDir(), "hoverfly")
6160
os.Mkdir(tempDir, 0777)
6261

63-
newScript, err := ioutil.TempFile(tempDir, "hoverfly_")
62+
newScript, err := os.CreateTemp(tempDir, "hoverfly_")
6463
if err != nil {
6564
return err
6665
}
@@ -91,7 +90,7 @@ func (action *Action) GetScript() (string, error) {
9190
if action.Script == nil {
9291
return "", nil
9392
}
94-
contents, err := ioutil.ReadFile(action.Script.Name())
93+
contents, err := os.ReadFile(action.Script.Name())
9594
if err != nil {
9695
return "", err
9796
}

core/authentication/jwt_backend_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func TestAuthenticate(t *testing.T) {
2727
ab.AddUser(username, passw, true)
2828
jwtBackend := authentication.InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
2929
user := &backends.User{
30-
Username: string(username),
31-
Password: string(passw),
30+
Username: username,
31+
Password: passw,
3232
UUID: "uuid_here",
3333
IsAdmin: true}
3434

core/benchmark_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"fmt"
77
"github.com/SpectoLabs/hoverfly/core/handlers/v2"
88
. "github.com/onsi/gomega"
9-
"io/ioutil"
109
"net/http"
10+
"os"
1111
"testing"
1212
"time"
1313
)
@@ -21,7 +21,7 @@ func BenchmarkPutSimulationAPI(b *testing.B) {
2121
Mode: "simulate",
2222
})
2323

24-
data, _ := ioutil.ReadFile("../testdata/large_file.json")
24+
data, _ := os.ReadFile("../testdata/large_file.json")
2525

2626
adminApi := AdminApi{}
2727

@@ -156,7 +156,7 @@ func BenchmarkProcessRequest(b *testing.B) {
156156
}
157157
}`), &templated)
158158

159-
bytes, _ := ioutil.ReadFile("../testdata/large_response_body.json")
159+
bytes, _ := os.ReadFile("../testdata/large_response_body.json")
160160
largeResponse := v2.SimulationViewV5{}
161161
_ = json.Unmarshal(bytes, &largeResponse)
162162

core/cache/boltdb_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func NewBoltDBCache(db *bolt.DB, bucket []byte) *BoltCache {
1313
return &BoltCache{
1414
DS: db,
15-
CurrentBucket: []byte(bucket),
15+
CurrentBucket: bucket,
1616
}
1717
}
1818

core/cache/memory_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (c *InMemoryCache) GetAllValues() (values [][]byte, err error) {
4141
values = make([][]byte, len(c.elements), len(c.elements))
4242
index := 0
4343
for _, v := range c.elements {
44-
values[index] = []byte(v)
44+
values[index] = v
4545
index++
4646
}
4747
c.RUnlock()

core/cache/memory_cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func Test_InMemoryCache_DeleteKey(t *testing.T) {
188188
unit.Set(expectedKey1, expectedValue1)
189189
unit.Set(expectedKey2, expectedValue2)
190190

191-
unit.Delete([]byte(expectedKey1))
191+
unit.Delete(expectedKey1)
192192

193193
value, err := unit.Get(expectedKey1)
194194
Expect(err).ToNot(BeNil())

core/cmd/hoverfly/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"flag"
2626
"fmt"
2727
"io"
28-
"io/ioutil"
2928
"os"
3029
"path/filepath"
3130
"strconv"
@@ -576,7 +575,7 @@ func main() {
576575
delayInMs = 1000
577576
}
578577

579-
if fileContents, err := ioutil.ReadFile(splitPostServeAction[2]); err == nil {
578+
if fileContents, err := os.ReadFile(splitPostServeAction[2]); err == nil {
580579
err = hoverfly.SetLocalPostServeAction(splitPostServeAction[0], splitPostServeAction[1], string(fileContents), delayInMs)
581580
if err != nil {
582581
log.WithFields(log.Fields{
@@ -615,7 +614,7 @@ func main() {
615614

616615
splitTemplateDataSource := strings.Split(v, " ")
617616
if len(splitTemplateDataSource) == 2 {
618-
if fileContents, err := ioutil.ReadFile(splitTemplateDataSource[1]); err == nil {
617+
if fileContents, err := os.ReadFile(splitTemplateDataSource[1]); err == nil {
619618
err = hoverfly.SetCsvDataSource(splitTemplateDataSource[0], string(fileContents))
620619
if err != nil {
621620
log.WithFields(log.Fields{
@@ -677,7 +676,7 @@ func main() {
677676
cfg.Webserver = *webserver
678677

679678
if *pacFile != "" {
680-
pacFileContent, err := ioutil.ReadFile(*pacFile)
679+
pacFileContent, err := os.ReadFile(*pacFile)
681680
if err != nil {
682681
log.WithFields(log.Fields{"error": err.Error(), "pacFile": *pacFile}).
683682
Fatal("Failed to import pac file")

core/cors/cors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cors
22

33
import (
44
"bytes"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"strconv"
88
)
@@ -55,7 +55,7 @@ func (c *Configs) InterceptPreflightRequest(r *http.Request) *http.Response {
5555
resp.StatusCode = http.StatusOK
5656
buf := bytes.NewBufferString("")
5757
resp.ContentLength = 0
58-
resp.Body = ioutil.NopCloser(buf)
58+
resp.Body = io.NopCloser(buf)
5959
return resp
6060
}
6161

core/cors/cors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cors
22

33
import (
44
. "github.com/onsi/gomega"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"testing"
88
)
@@ -25,7 +25,7 @@ func Test_InterceptPreflightRequest_ReturnSuccessResponseWithDefaultHeaders(t *t
2525
Expect(resp.Header.Get("Access-Control-Max-Age")).To(Equal("1800"))
2626
Expect(resp.Header.Get("Access-Control-Allow-Credentials")).To(Equal("true"))
2727
Expect(resp.Header.Get("Access-Control-Allow-Headers")).To(Equal("Content-Type,Origin,Accept,Authorization,Content-Length,X-Requested-With"))
28-
responseBody, err := ioutil.ReadAll(resp.Body)
28+
responseBody, err := io.ReadAll(resp.Body)
2929
Expect(string(responseBody)).To(Equal(""))
3030
}
3131

core/handlers/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package handlers
33
import (
44
"encoding/json"
55
"errors"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"time"
99

@@ -28,7 +28,7 @@ type AdminHandler interface {
2828
func ReadFromRequest(request *http.Request, v interface{}) error {
2929
defer request.Body.Close()
3030

31-
body, _ := ioutil.ReadAll(request.Body)
31+
body, _ := io.ReadAll(request.Body)
3232

3333
err := json.Unmarshal(body, &v)
3434
if err != nil {
@@ -123,7 +123,7 @@ func NewWebsocket(handler WebSocketHandler, w http.ResponseWriter, r *http.Reque
123123
"message": string(p),
124124
}).Debug("Got message...")
125125

126-
for _ = range time.Tick(1 * time.Second) {
126+
for range time.Tick(1 * time.Second) {
127127

128128
updateBytes, err := handler()
129129

0 commit comments

Comments
 (0)