Skip to content

Commit

Permalink
Reflect Idempotency-Key header back in response
Browse files Browse the repository at this point in the history
Behave like the real Stripe API by reflecting an idempotency key that
was sent into stripe-mock back in the response body.
  • Loading branch information
brandur committed May 31, 2018
1 parent 56c5482 commit a23ef5c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server.go
Expand Up @@ -135,6 +135,13 @@ func (s *StubServer) HandleRequest(w http.ResponseWriter, r *http.Request) {
return
}

// We don't do anything with the idempotency key for now, but reflect it
// back into response headers like the Stripe API does.
idempotencyKey := r.Header.Get("Idempotency-Key")
if idempotencyKey != "" {
w.Header().Set("Idempotency-Key", idempotencyKey)
}

// Every response needs a Request-Id header except the invalid authorization
w.Header().Set("Request-Id", "req_123")

Expand Down
9 changes: 9 additions & 0 deletions server_test.go
Expand Up @@ -172,6 +172,15 @@ func TestStubServer_ErrorsOnMismatchedContentType(t *testing.T) {
errorInfo["message"])
}

func TestStubServer_ReflectsIdempotencyKey(t *testing.T) {
headers := getDefaultHeaders()
headers["Idempotency-Key"] = "my-key"

resp, _ := sendRequest(t, "POST", "/v1/charges",
"amount=123", headers)
assert.Equal(t, "my-key", resp.Header.Get("Idempotency-Key"))
}

func TestStubServer_RoutesRequest(t *testing.T) {
server := getStubServer(t)

Expand Down

0 comments on commit a23ef5c

Please sign in to comment.