-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from szkiba/feature/modules
Feature/modules
- Loading branch information
Showing
28 changed files
with
958 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// SPDX-FileCopyrightText: 2023 Iván Szkiba | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package addon | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"go.k6.io/k6/js/modules" | ||
khttp "go.k6.io/k6/js/modules/k6/http" | ||
"go.k6.io/k6/lib/netext/httpext" | ||
) | ||
|
||
type tripware struct { | ||
vu modules.VU | ||
} | ||
|
||
var _ http.RoundTripper = (*tripware)(nil) | ||
|
||
func NewTransport(vu modules.VU) http.RoundTripper { | ||
return &tripware{vu: vu} | ||
} | ||
|
||
const ( | ||
httpTimeout = 60 * time.Second | ||
protoFields = 2 | ||
) | ||
|
||
func (t *tripware) toParsedRequest(req *http.Request) (*httpext.ParsedHTTPRequest, error) { | ||
state := t.vu.State() | ||
if state == nil { | ||
return nil, khttp.ErrHTTPForbiddenInInitContext | ||
} | ||
|
||
u := req.URL.String() | ||
|
||
url, err := httpext.NewURL(u, u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
preq := &httpext.ParsedHTTPRequest{ // nolint:exhaustruct | ||
URL: &url, | ||
Req: req, | ||
Timeout: httpTimeout, | ||
Throw: state.Options.Throw.Bool, | ||
Redirects: state.Options.MaxRedirects, | ||
Cookies: make(map[string]*httpext.HTTPRequestCookie), | ||
TagsAndMeta: t.vu.State().Tags.GetCurrentValues(), | ||
} | ||
|
||
if state.Options.DiscardResponseBodies.Bool { | ||
preq.ResponseType = httpext.ResponseTypeNone | ||
} else { | ||
preq.ResponseType = httpext.ResponseTypeBinary | ||
} | ||
|
||
if req.Body != nil { | ||
data, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
preq.Body = bytes.NewBuffer(data) | ||
|
||
req.Body.Close() | ||
} | ||
|
||
preq.Req.Header.Set("User-Agent", state.Options.UserAgent.String) | ||
|
||
return preq, nil | ||
} | ||
|
||
func (t *tripware) toResponse(req *http.Request, eresp *httpext.Response) (*http.Response, error) { | ||
resp := new(http.Response) | ||
|
||
resp.Request = req | ||
|
||
resp.Header = http.Header{} | ||
|
||
for k, v := range eresp.Headers { | ||
resp.Header.Set(k, v) | ||
} | ||
|
||
resp.Proto = eresp.Proto | ||
fields := strings.SplitN(resp.Proto, "/", protoFields) | ||
|
||
if major, err := strconv.Atoi(fields[0]); err == nil { | ||
resp.ProtoMajor = major | ||
} | ||
|
||
if len(fields) > 0 { | ||
if minor, err := strconv.Atoi(fields[1]); err == nil { | ||
resp.ProtoMinor = minor | ||
} | ||
} | ||
|
||
if eresp.Body != nil { | ||
if data, ok := eresp.Body.([]byte); ok { | ||
resp.Body = io.NopCloser(bytes.NewBuffer(data)) | ||
} | ||
} | ||
|
||
resp.Status = eresp.StatusText | ||
resp.StatusCode = eresp.Status | ||
|
||
return resp, nil | ||
} | ||
|
||
func (t *tripware) RoundTrip(req *http.Request) (*http.Response, error) { | ||
preq, err := t.toParsedRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := httpext.MakeRequest(t.vu.Context(), t.vu.State(), preq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return t.toResponse(req, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-FileCopyrightText: 2023 Iván Szkiba | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package g0 | ||
|
||
import ( | ||
"github.com/szkiba/xk6-g0/internal/builtin/gjson" | ||
"github.com/szkiba/xk6-g0/internal/builtin/gofakeit" | ||
"github.com/szkiba/xk6-g0/internal/builtin/goquery" | ||
"github.com/szkiba/xk6-g0/internal/builtin/jsonpath" | ||
"github.com/szkiba/xk6-g0/internal/builtin/jsonschema" | ||
"github.com/szkiba/xk6-g0/internal/builtin/logrus" | ||
"github.com/szkiba/xk6-g0/internal/builtin/resty" | ||
"github.com/szkiba/xk6-g0/internal/builtin/stdlib" | ||
"github.com/szkiba/xk6-g0/internal/builtin/testify" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
func registerBuiltins() { | ||
registerExports(stdlib.Exports) | ||
registerExports(logrus.Exports) | ||
registerExports(resty.Exports) | ||
registerExports(testify.Exports) | ||
registerExports(goquery.Exports) | ||
registerExports(gjson.Exports) | ||
registerExports(jsonpath.Exports) | ||
registerExports(jsonschema.Exports) | ||
registerExports(gofakeit.Exports) | ||
} | ||
|
||
func registerExtension() { | ||
modules.Register("k6/x/g0", New()) | ||
} | ||
|
||
func Bootstrap(addons ...ExportsFunc) { | ||
registerBuiltins() | ||
registerExports(addons...) | ||
redirectStdin() | ||
registerExtension() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
// SPDX-FileCopyrightText: 2023 Iván Szkiba | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package g0 | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.