Skip to content

Commit 7a6e9f5

Browse files
authored
Only send idempotency-key on post requests (#1947)
No need to send this on requests where the server won't use the `idempotency-key` at all
2 parents c129cd8 + d64a64a commit 7a6e9f5

File tree

16 files changed

+15
-176
lines changed

16 files changed

+15
-176
lines changed

csharp/Svix.Tests/WiremockTests.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -326,27 +326,6 @@ public void OpWebhookModels()
326326
Assert.Equal(jsonString, JsonConvert.SerializeObject(loadedFromJson));
327327
}
328328

329-
[Fact]
330-
public void IdempotencyKeyIsSentForListRequest()
331-
{
332-
stub.Given(Request.Create().WithPath("/api/v1/app"))
333-
.RespondWith(
334-
Response
335-
.Create()
336-
.WithStatusCode(200)
337-
.WithBody("""{"data":[],"iterator":null,"prevIterator":null,"done":true}""")
338-
);
339-
client.Application.List();
340-
341-
Assert.Equal(1, stub.LogEntries.Count);
342-
Assert.True(stub.LogEntries[0].RequestMessage.Headers.ContainsKey("idempotency-key"));
343-
Assert.StartsWith(
344-
"auto_",
345-
stub.LogEntries[0].RequestMessage.Headers["idempotency-key"][0]
346-
);
347-
Assert.Equal(1, stub.LogEntries.Count);
348-
}
349-
350329
[Fact]
351330
public void IdempotencyKeyIsSentForCreateRequest()
352331
{

csharp/Svix/SvixHttpClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task<ApiResponse<T>> SendRequestAsync<T>(
5959
{
6060
headerParams = new Dictionary<string, string>();
6161
}
62-
if (!headerParams.ContainsKey("idempotency-key"))
62+
if (!headerParams.ContainsKey("idempotency-key") && method == HttpMethod.Post)
6363
{
6464
headerParams["idempotency-key"] = "auto_" + Guid.NewGuid().ToString();
6565
}

go/internal/svix_http_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func ExecuteRequest[ReqBody any, ResBody any](
8484
}
8585

8686
key, ok := headerParams["idempotency-key"]
87-
if !ok || key == "" {
87+
if (!ok || key == "") && strings.ToUpper(method) == "POST" {
8888
req.Header.Set("idempotency-key", "auto_"+uuid.New().String())
8989
}
9090

go/svix_http_client_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -237,34 +237,6 @@ func TestOctothorpeUrlParam(t *testing.T) {
237237
}
238238
}
239239

240-
func TestIdempotencyKeyIsSentForListRequest(t *testing.T) {
241-
svx := newMockClient()
242-
httpmock.Activate()
243-
defer httpmock.DeactivateAndReset()
244-
245-
httpmock.RegisterResponder("GET", "http://testapi.test/api/v1/app",
246-
func(r *http.Request) (*http.Response, error) {
247-
idempotencyKey := r.Header.Get("idempotency-key")
248-
if idempotencyKey == "" {
249-
t.Errorf("Expected idempotency-key header to be set")
250-
}
251-
if !strings.HasPrefix(idempotencyKey, "auto_") {
252-
t.Errorf("Expected idempotency-key to start with 'auto_', got: %s", idempotencyKey)
253-
}
254-
return httpmock.NewStringResponse(200, appListOut), nil
255-
},
256-
)
257-
258-
_, err := svx.Application.List(context.Background(), nil)
259-
if err != nil {
260-
t.Error(err)
261-
}
262-
263-
if httpmock.GetTotalCallCount() != 1 {
264-
t.Errorf("Expected 1 request, got %v", httpmock.GetTotalCallCount())
265-
}
266-
}
267-
268240
func TestIdempotencyKeyIsSentForCreateRequest(t *testing.T) {
269241
svx := newMockClient()
270242
httpmock.Activate()

java/lib/src/main/java/com/svix/SvixHttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public <Req, Res> Res executeRequest(
5555
defaultHeaders.forEach(reqBuilder::addHeader);
5656

5757
String idempotencyKey = headers == null ? null : headers.get("idempotency-key");
58-
if (idempotencyKey == null || idempotencyKey.isEmpty()) {
59-
reqBuilder.addHeader("idempotency-key", "auto_" + UUID.randomUUID().toString());
58+
if ((idempotencyKey == null || idempotencyKey.isEmpty()) && method.toUpperCase() == "POST") {
59+
reqBuilder.addHeader("idempotency-key", "auto_" + UUID.randomUUID().toString());
6060
}
6161

6262
// Add custom headers if present

java/lib/src/test/com/svix/test/WiremockTests.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -507,21 +507,6 @@ public void jsonEncodedMessageIn() throws Exception {
507507
.withRequestBody(equalTo(expectedBody)));
508508
}
509509

510-
@Test
511-
public void testIdempotencyKeyIsSentForListRequest() throws Exception {
512-
Svix svx = testClient();
513-
wireMockRule.stubFor(
514-
WireMock.get(urlEqualTo("/api/v1/app"))
515-
.willReturn(WireMock.ok().withBodyFile("ListResponseApplicationOut.json")));
516-
517-
svx.getApplication().list();
518-
519-
wireMockRule.verify(
520-
1,
521-
getRequestedFor(urlEqualTo("/api/v1/app"))
522-
.withHeader("idempotency-key", matching("auto_.*")));
523-
}
524-
525510
@Test
526511
public void testIdempotencyKeyIsSentForCreateRequest() throws Exception {
527512
Svix svx = testClient();

javascript/src/mockttp.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -419,20 +419,6 @@ describe("mockttp tests", () => {
419419
);
420420
});
421421

422-
test("test idempotency key is sent for list request", async () => {
423-
const endpointMock = await mockServer
424-
.forGet("/api/v1/app")
425-
.thenReply(200, ListResponseApplicationOut);
426-
const svx = new Svix("token", { serverUrl: mockServer.url });
427-
428-
await svx.application.list();
429-
430-
const requests = await endpointMock.getSeenRequests();
431-
expect(requests.length).toBe(1);
432-
const idempotencyKey = requests[0].headers["idempotency-key"] as string;
433-
expect(idempotencyKey.startsWith("auto_")).toBe(true);
434-
});
435-
436422
test("test idempotency key is sent for create request", async () => {
437423
const endpointMock = await mockServer
438424
.forPost("/api/v1/app")

javascript/src/request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ export class SvixRequest {
114114
url.searchParams.set(name, value);
115115
}
116116

117-
if (this.headerParams["idempotency-key"] === undefined) {
117+
if (
118+
this.headerParams["idempotency-key"] === undefined &&
119+
this.method.toUpperCase() === "POST"
120+
) {
118121
this.headerParams["idempotency-key"] = "auto_" + uuidv4();
119122
}
120123

kotlin/lib/src/main/kotlin/SvixHttpClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ internal constructor(
4343
}
4444
}
4545

46-
if (headers?.get("idempotency-key") == null) {
47-
val uuid = UUID.randomUUID().toString()
48-
reqBuilder.addHeader("idempotency-key", "auto_" + uuid)
46+
if (headers?.get("idempotency-key") == null && method == "POST") {
47+
val uuid = UUID.randomUUID().toString()
48+
reqBuilder.addHeader("idempotency-key", "auto_" + uuid)
4949
}
5050

5151
reqBuilder.addHeader("svix-req-id", Random.nextULong().toString())

kotlin/lib/src/test/com/svix/kotlin/WiremockTests.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -446,21 +446,6 @@ class WiremockTests {
446446
)
447447
}
448448

449-
@Test
450-
fun testIdempotencyKeyIsSentForListRequest() {
451-
val svx = testClient()
452-
wireMockServer.stubFor(
453-
get(urlEqualTo("/api/v1/app"))
454-
.willReturn(ok().withBodyFile("ListResponseApplicationOut.json")))
455-
456-
runBlocking { svx.application.list() }
457-
458-
wireMockServer.verify(
459-
1,
460-
getRequestedFor(urlEqualTo("/api/v1/app"))
461-
.withHeader("idempotency-key", matching("auto_.*")),
462-
)
463-
}
464449

465450
@Test
466451
fun testIdempotencyKeyIsSentForCreateRequest() {

0 commit comments

Comments
 (0)