change: convert brotli.Reader to io.ReadCloser

This commit is contained in:
debugtalk
2022-02-21 18:43:31 +08:00
parent b3762dbbbc
commit d534fe06ee
5 changed files with 13 additions and 35 deletions

View File

@@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/rs/zerolog/log"
@@ -19,7 +19,7 @@ func loadFromJSON(path string) (*TCase, error) {
}
log.Info().Str("path", path).Msg("load json testcase")
file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)
if err != nil {
log.Error().Err(err).Msg("load json path failed")
return nil, err
@@ -40,7 +40,7 @@ func loadFromYAML(path string) (*TCase, error) {
}
log.Info().Str("path", path).Msg("load yaml testcase")
file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)
if err != nil {
log.Error().Err(err).Msg("load yaml path failed")
return nil, err

View File

@@ -7,7 +7,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
@@ -68,7 +67,7 @@ func loadFromCSV(path string) []map[string]interface{} {
}
log.Info().Str("path", path).Msg("load csv file")
file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)
if err != nil {
log.Error().Err(err).Msg("load csv file failed")
panic(err)
@@ -98,7 +97,7 @@ func Dump2JSON(data interface{}, path string) error {
}
log.Info().Str("path", path).Msg("dump data to json")
file, _ := json.MarshalIndent(data, "", "\t")
err = ioutil.WriteFile(path, file, 0644)
err = os.WriteFile(path, file, 0644)
if err != nil {
log.Error().Err(err).Msg("dump json path failed")
return err
@@ -125,7 +124,7 @@ func Dump2YAML(data interface{}, path string) error {
return err
}
err = ioutil.WriteFile(path, buffer.Bytes(), 0644)
err = os.WriteFile(path, buffer.Bytes(), 0644)
if err != nil {
log.Error().Err(err).Msg("dump yaml path failed")
return err
@@ -171,7 +170,7 @@ func CreateFolder(folderPath string) error {
func CreateFile(filePath string, data string) error {
log.Info().Str("path", filePath).Msg("create file")
err := ioutil.WriteFile(filePath, []byte(data), 0o644)
err := os.WriteFile(filePath, []byte(data), 0o644)
if err != nil {
log.Error().Err(err).Msg("create file failed")
return err

View File

@@ -4,7 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/url"
"os"
"path/filepath"
@@ -98,7 +98,7 @@ func (h *har) load() (*Har, error) {
return nil, fmt.Errorf("open: %w", err)
}
data, err := ioutil.ReadAll(fp)
data, err := io.ReadAll(fp)
fp.Close()
if err != nil {
return nil, fmt.Errorf("read: %w", err)

View File

@@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
@@ -32,7 +32,7 @@ func newResponseObject(t *testing.T, parser *parser, resp *http.Response) (*resp
}
// read response body
respBodyBytes, err := ioutil.ReadAll(resp.Body)
respBodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

View File

@@ -11,7 +11,6 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
@@ -808,30 +807,10 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
return stepResult, err
}
// convert brotli.Reader to io.ReadCloser,
// so that we can assign it to resp.Body
type brotliReader struct {
r io.Reader
}
func (br *brotliReader) Read(p []byte) (n int, err error) {
return br.r.Read(p)
}
func (br *brotliReader) Close() (err error) {
return nil
}
func newBrotliReader(r io.Reader) io.ReadCloser {
b := &brotliReader{}
b.r = brotli.NewReader(r)
return b
}
func decodeResponseBody(resp *http.Response) error {
switch resp.Header.Get("Content-Encoding") {
case "br":
resp.Body = newBrotliReader(resp.Body)
resp.Body = io.NopCloser(brotli.NewReader(resp.Body))
case "gzip":
gr, err := gzip.NewReader(resp.Body)
if err != nil {
@@ -920,7 +899,7 @@ func (r *caseRunner) getSummary() *testCaseSummary {
}
func setBodyBytes(req *http.Request, data []byte) {
req.Body = ioutil.NopCloser(bytes.NewReader(data))
req.Body = io.NopCloser(bytes.NewReader(data))
req.ContentLength = int64(len(data))
}