change: convert brotli.Reader to io.ReadCloser

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -11,7 +11,6 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
@@ -808,30 +807,10 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
return stepResult, err 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 { func decodeResponseBody(resp *http.Response) error {
switch resp.Header.Get("Content-Encoding") { switch resp.Header.Get("Content-Encoding") {
case "br": case "br":
resp.Body = newBrotliReader(resp.Body) resp.Body = io.NopCloser(brotli.NewReader(resp.Body))
case "gzip": case "gzip":
gr, err := gzip.NewReader(resp.Body) gr, err := gzip.NewReader(resp.Body)
if err != nil { if err != nil {
@@ -920,7 +899,7 @@ func (r *caseRunner) getSummary() *testCaseSummary {
} }
func setBodyBytes(req *http.Request, data []byte) { 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)) req.ContentLength = int64(len(data))
} }