Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request/response observer via godog attachments #23

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add request observer
vearutop committed Oct 31, 2024
commit cf3c3b9d5b6b3bacddd9d9e390168d2294df9607
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -4,15 +4,14 @@ go 1.18

require (
github.com/bool64/dev v0.2.36
github.com/bool64/httpmock v0.1.14
github.com/bool64/httpmock v0.1.15
github.com/bool64/shared v0.1.5
github.com/cenkalti/backoff/v4 v4.2.1
github.com/cucumber/godog v0.14.2-0.20241016144147-ecd2dfebbdab
github.com/godogx/resource v0.1.1
github.com/godogx/vars v0.1.8
github.com/stretchr/testify v1.9.0
github.com/swaggest/assertjson v1.9.0
moul.io/http2curl v1.0.0
)

require (
@@ -27,7 +26,6 @@ require (
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff // indirect
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/bool64/dev v0.2.36 h1:yU3bbOTujoxhWnt8ig8t94PVmZXIkCaRj9C57OtqJBY=
github.com/bool64/dev v0.2.36/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/httpmock v0.1.14 h1:C1agtHmeHr3F1niq75xZe4QSC9TCOZcKxjW0SwLhyjg=
github.com/bool64/httpmock v0.1.14/go.mod h1:Ht82vaR7YJu8YHcJoK5mDi5OG6NJfv/MmYfdfod1huQ=
github.com/bool64/httpmock v0.1.15 h1:PWvuqpew/FEigT7cvv03/t9G+UeE3wD2QP8PVyBBUwc=
github.com/bool64/httpmock v0.1.15/go.mod h1:Ht82vaR7YJu8YHcJoK5mDi5OG6NJfv/MmYfdfod1huQ=
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
github.com/bool64/shared v0.1.5/go.mod h1:081yz68YC9jeFB3+Bbmno2RFWvGKv1lPKkMP6MHJlPs=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
@@ -27,7 +27,6 @@ github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
@@ -43,7 +42,6 @@ github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
@@ -60,9 +58,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
@@ -104,5 +99,3 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
34 changes: 12 additions & 22 deletions local_client.go
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@ import (
"github.com/cucumber/godog"
"github.com/godogx/vars"
"github.com/swaggest/assertjson/json5"
_ "moul.io/http2curl"
)

type sentinelError string
@@ -229,13 +228,9 @@ func (l *LocalClient) RegisterSteps(s *godog.ScenarioContext) {
s.After(l.afterScenario)
}

func (l *LocalClient) afterScenario(ctx context.Context, _ *godog.Scenario, err error) (context.Context, error) {
func (l *LocalClient) afterScenario(ctx context.Context, _ *godog.Scenario, _ error) (context.Context, error) {
var errs []string

if err != nil {
errs = append(errs, err.Error())
}

for service := range l.services {
client, _, err := l.Service(ctx, service)
if err != nil {
@@ -587,14 +582,6 @@ func (l *LocalClient) iShouldHaveOtherResponsesWithStatus(ctx context.Context, s
return ctx, c.ExpectOtherResponsesStatus(code)
}

type roundTripperFunc func(*http.Request) (*http.Response, error)

func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}

type httpMessageDumpedCtxKey struct{}

// DefaultExposeHTTPDetails instruments context with godog.Attachment items of HTTP transaction.
func DefaultExposeHTTPDetails(ctx context.Context, d httpmock.HTTPValue) (context.Context, error) {
d.Req.Body = io.NopCloser(bytes.NewReader(d.ReqBody))
@@ -663,20 +650,23 @@ func (l *LocalClient) expectResponse(ctx context.Context, service string, expect
return ctx, err
}

err = expect(c)
if err != nil {
return ctx, err
}
expErr := expect(c)

d := c.Details()

if l.ExposeHTTPDetails != nil && d.Req != nil && d.Req.Context().Value(httpMessageDumpedCtxKey{}) == nil {
d.Req.WithContext(context.WithValue(d.Req.Context(), httpMessageDumpedCtxKey{}, true))

if l.ExposeHTTPDetails != nil && d.Req != nil && !d.AlreadyRequested {
ctx, err = l.ExposeHTTPDetails(ctx, d)
}

return ctx, nil
if expErr != nil {
if err == nil {
err = expErr
} else {
err = fmt.Errorf("%w (%s)", expErr, err.Error())
}
}

return ctx, err
}

func (l *LocalClient) iShouldHaveResponseWithStatus(ctx context.Context, service, statusOrCode string) (context.Context, error) {
12 changes: 1 addition & 11 deletions local_client_concurrency_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package httpsteps //nolint:testpackage // This test extends internal implementation for better control, so it has to be internal.

import (
"context"
"net/http"
"testing"

@@ -66,16 +65,7 @@ func TestLocalClient_RegisterSteps_concurrencyNonBlocked(t *testing.T) {
client.ConcurrencyLevel = concurrency
client.Transport = http.DefaultTransport
})
local.Observe = func(ctx context.Context, r HTTPValue) context.Context {
if r.Sequence != 1 {
return ctx
}

return godog.Attach(ctx, godog.Attachment{
Body: []byte(r.Request.URL.String()),
MediaType: "text/plain",
})
}

local.AddService("service-one", srvURL)
local.AddService("service-two", srvURL)