Skip to content

Commit

Permalink
Merge pull request #83 from 2manymws/fix-testutil
Browse files Browse the repository at this point in the history
Use `http.Read*` in testutil
  • Loading branch information
k1LoW committed Jul 12, 2024
2 parents 3781590 + e8816cb commit 84c1816
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 55 deletions.
8 changes: 5 additions & 3 deletions testdata/go_test.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module github.com/2manymws/rc

go 1.21.5
go 1.21.11

toolchain go1.22.3

require (
github.com/google/go-cmp v0.6.0
github.com/k1LoW/httpstub v0.16.4
github.com/k1LoW/httpstub v0.16.5
)

require (
Expand All @@ -13,7 +15,7 @@ require (
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/minio/pkg v1.7.5 // indirect
github.com/pb33f/libopenapi v0.16.7 // indirect
github.com/pb33f/libopenapi v0.16.8 // indirect
github.com/pb33f/libopenapi-validator v0.0.56 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions testdata/go_test.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLe
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/k1LoW/httpstub v0.16.4 h1:XM27/PQH/leSthyH/jzMxivAehxUMXVMudah3tDpX74=
github.com/k1LoW/httpstub v0.16.4/go.mod h1:LJ607j74kHbbtMbkloC9q+0pG3VsJIn8+FwK1jut8Qo=
github.com/k1LoW/httpstub v0.16.5 h1:PtrOUrYIko6M3nnsKEHtu/bNp8uAh+MrblQ905pnKhA=
github.com/k1LoW/httpstub v0.16.5/go.mod h1:1vUx0ehYo19Tz8gVaWZ2PWvOPtRyLeQ3ZWYiw5VUmyk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -64,8 +64,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.16.7 h1:kgzBpcX+ESF+C/UUlpqd+H4qdWloBfsDJVMgUmbPDts=
github.com/pb33f/libopenapi v0.16.7/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi v0.16.8 h1:EceJ+JTHViwjKNyP3LJ8J39jSQ4F2oOvHw6KfWCcVxM=
github.com/pb33f/libopenapi v0.16.8/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi-validator v0.0.56 h1:kYO8DRsSRpnxhUExuEfsgARuT/hkc/ISC77CX0BMze4=
github.com/pb33f/libopenapi-validator v0.0.56/go.mod h1:Cwdf0GUmnUrbw1LBpcGV1Wx5q/LC9akREV4Jc6/yJDc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
66 changes: 18 additions & 48 deletions testutil/rcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,46 @@ package testutil
// Copy from github.com/2manymws/rcutil

import (
"bufio"
"bytes"
"io"
"net/http"
"net/url"
)

type cachedReqRes struct {
Method string `json:"method"`
Host string `json:"host"`
URL string `json:"url"`
ReqHeader http.Header `json:"req_header"`
ReqBody []byte `json:"req_body"`

StatusCode int `json:"status_code"`
ResHeader http.Header `json:"res_header"`
ResBody []byte `json:"res_body"`
req []byte
res []byte
}

// encodeReqRes encodes http.Request and http.Response.
func encodeReqRes(req *http.Request, res *http.Response) (*cachedReqRes, error) {
c := &cachedReqRes{
Method: req.Method,
Host: req.Host,
URL: req.URL.String(),
ReqHeader: req.Header,

StatusCode: res.StatusCode,
ResHeader: res.Header,
reqb := &bytes.Buffer{}
if err := req.Write(reqb); err != nil {
return nil, err
}
{
// FIXME: Use stream
b, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
defer req.Body.Close()
c.ReqBody = b
resb := &bytes.Buffer{}
if err := res.Write(resb); err != nil {
return nil, err
}

{
// FIXME: Use stream
b, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
defer res.Body.Close()
c.ResBody = b
c := &cachedReqRes{
req: reqb.Bytes(),
res: resb.Bytes(),
}

return c, nil
}

// decodeReqRes decodes to http.Request and http.Response.
func decodeReqRes(c *cachedReqRes) (*http.Request, *http.Response, error) {
u, err := url.Parse(c.URL)
reqb := bytes.NewReader(c.req)
req, err := http.ReadRequest(bufio.NewReader(reqb))
if err != nil {
return nil, nil, err
}
req := &http.Request{
Method: c.Method,
Host: c.Host,
URL: u,
Header: c.ReqHeader,
Body: io.NopCloser(bytes.NewReader(c.ReqBody)),
}
res := &http.Response{
Status: http.StatusText(c.StatusCode),
StatusCode: c.StatusCode,
Header: c.ResHeader,
Body: io.NopCloser(bytes.NewReader(c.ResBody)),
resb := bytes.NewReader(c.res)
res, err := http.ReadResponse(bufio.NewReader(resb), req)
if err != nil {
return nil, nil, err
}
return req, res, nil
}

0 comments on commit 84c1816

Please sign in to comment.