Skip to content

Commit

Permalink
Add idempotency_key support to fixture request spec (#1009)
Browse files Browse the repository at this point in the history
* Add idempotency_key support to fixture request spec

Also fix a bug in metadata that testing idempotency keys uncovered.

* Omit IdempotencyKey when empty

* Fix omitempty spelling
  • Loading branch information
bernerd-stripe committed Dec 6, 2022
1 parent eab26d8 commit 3e42171
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
25 changes: 15 additions & 10 deletions pkg/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FixtureRequest struct {
Path string `json:"path"`
Method string `json:"method"`
Params map[string]interface{} `json:"params"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
Context string `json:"context,omitempty"`
}

Expand Down Expand Up @@ -297,19 +298,10 @@ func (fxt *Fixture) UpdateEnv() error {
}

func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVersion string) ([]byte, error) {
var rp requests.RequestParameters

if data.Method == "post" && !fxt.FixtureData.Meta.ExcludeMetadata {
now := time.Now().String()
metadata := fmt.Sprintf("metadata[_created_by_fixture]=%s", now)
rp.AppendData([]string{metadata})
}

req := requests.Base{
Method: strings.ToUpper(data.Method),
SuppressOutput: true,
APIBaseURL: fxt.BaseURL,
Parameters: rp,
}

path, err := fxt.ParsePath(data)
Expand All @@ -319,11 +311,24 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVer
}

params, err := fxt.createParams(data.Params, apiVersion)

if err != nil {
return make([]byte, 0), err
}

if data.Method == "post" && !fxt.FixtureData.Meta.ExcludeMetadata {
now := time.Now().String()
metadata := fmt.Sprintf("metadata[_created_by_fixture]=%s", now)
params.AppendData([]string{metadata})
}

if data.IdempotencyKey != "" {
idempotencyKey, err := fxt.ParseQuery(data.IdempotencyKey)
if err != nil {
return nil, fmt.Errorf("error parsing idempotency_key field: %w", err)
}
params.SetIdempotency(idempotencyKey)
}

return req.MakeRequest(ctx, fxt.APIKey, path, params, true)
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/fixtures/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const testFixture = `
"name": "cust_bender",
"path": "/v1/customers",
"method": "post",
"idempotency_key": "create_cust_bender",
"params": {
"name": "Bender Bending Rodriguez",
"email": "bender@planex.com",
Expand Down Expand Up @@ -90,6 +91,18 @@ func TestMakeRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
switch url := req.URL.String(); url {
case customersPath:
if req.Header.Get("Idempotency-Key") == "" {
t.Errorf("Idempotency key not sent")
}

if err := req.ParseForm(); err != nil {
t.Error(err)
}

if req.Form.Get("metadata[_created_by_fixture]") == "" {
t.Error("Metadata not set")
}

res.Write([]byte(`{"id": "cust_12345", "foo": "bar"}`))
case chargePath:
res.Write([]byte(`{"charge": true, "id": "char_12345"}`))
Expand Down

0 comments on commit 3e42171

Please sign in to comment.