Skip to content

Commit b260125

Browse files
authored
fix: do not modify fetch options.method (#1232)
1 parent f8fc106 commit b260125

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

playwright/src/main/java/com/microsoft/playwright/impl/APIRequestContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public String storageState(StorageStateOptions options) {
193193
}
194194

195195
private static RequestOptionsImpl ensureOptions(RequestOptions options, String method) {
196-
RequestOptionsImpl impl = (RequestOptionsImpl) options;
196+
RequestOptionsImpl impl = Utils.clone((RequestOptionsImpl) options);
197197
if (impl == null) {
198198
impl = new RequestOptionsImpl();
199199
}

playwright/src/main/java/com/microsoft/playwright/impl/Utils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.gson.JsonArray;
2020
import com.google.gson.JsonObject;
21+
import com.microsoft.playwright.ElementHandle;
2122
import com.microsoft.playwright.PlaywrightException;
2223
import com.microsoft.playwright.options.FilePayload;
2324
import com.microsoft.playwright.options.HttpHeader;
@@ -80,6 +81,14 @@ static <F, T> T convertType(F f, Class<T> t) {
8081
}
8182
}
8283

84+
static <T> T clone(T f) {
85+
if (f == null) {
86+
return f;
87+
}
88+
return convertType(f, (Class<T>) f.getClass());
89+
}
90+
91+
8392
static Set<Character> escapeGlobChars = new HashSet<>(Arrays.asList('/', '$', '^', '+', '.', '(', ')', '=', '!', '|'));
8493

8594
static String globToRegex(String glob) {

playwright/src/test/java/com/microsoft/playwright/TestGlobalFetch.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,35 @@ void shouldThrowAnErrorWhenMaxRedirectsIsLessThan0() {
380380
}
381381
request.dispose();
382382
}
383+
384+
@Test
385+
void shouldNotModifyRequestMethodInOptions() {
386+
APIRequestContext request = playwright.request().newContext();
387+
server.setRoute("/empty.html", exchange -> {
388+
exchange.getResponseHeaders().set("Content-type", "text/plain");
389+
exchange.sendResponseHeaders(200, 0);
390+
try (Writer writer = new OutputStreamWriter(exchange.getResponseBody())) {
391+
writer.write(exchange.getRequestMethod());
392+
}
393+
});
394+
RequestOptions options = RequestOptions.create();
395+
options.setTimeout(10000);
396+
{
397+
APIResponse response = request.fetch(server.EMPTY_PAGE, options);
398+
assertTrue(response.ok());
399+
assertEquals("GET", response.text());
400+
}
401+
{
402+
APIResponse response = request.delete(server.EMPTY_PAGE, options);
403+
assertTrue(response.ok());
404+
assertEquals("DELETE", response.text());
405+
}
406+
{
407+
APIResponse response = request.put(server.EMPTY_PAGE, options);
408+
assertTrue(response.ok());
409+
assertEquals("PUT", response.text());
410+
}
411+
request.dispose();
412+
}
413+
383414
}

0 commit comments

Comments
 (0)