Skip to content

Commit

Permalink
fix: form data field encoding (#1333)
Browse files Browse the repository at this point in the history
Fixes #1331
  • Loading branch information
yury-s authored Jul 11, 2023
1 parent ed63ba4 commit 35e3c36
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ static JsonArray toProtocol(ElementHandle[] handles) {
}

static JsonArray toProtocol(Map<String, String> map) {
for (String value : map.values()) {
if (value == null) {
throw new PlaywrightException("Value cannot be null");
}
}
return toNameValueArray(map);
}

Expand All @@ -348,7 +353,11 @@ static JsonArray toNameValueArray(Map<String, ?> map) {
for (Map.Entry<String, ?> e : map.entrySet()) {
JsonObject item = new JsonObject();
item.addProperty("name", e.getKey());
item.add("value", gson().toJsonTree(e.getValue()));
if (e.getValue() instanceof FilePayload) {
item.add("value", gson().toJsonTree(e.getValue()));
} else {
item.addProperty("value", "" + e.getValue());
}
array.add(item);
}
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,27 @@ void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, Int
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")
.set("age", 30)
.set("isMale", true)
.set("file", "f.js")));

assertEquals("POST", req.get().method);
assertEquals(asList("application/x-www-form-urlencoded"), req.get().headers.get("content-type"));
String body = new String(req.get().postBody);
assertTrue(body.contains("firstName=John"));
assertTrue(body.contains("lastName=Doe"));
assertTrue(body.contains("file=f.js"));
assertTrue(body.contains("firstName=John"), body);
assertTrue(body.contains("lastName=Doe"), body);
assertTrue(body.contains("age=30"), body);
assertTrue(body.contains("isMale=true"), body);
assertTrue(body.contains("file=f.js"), body);
}

@Test
void shouldEncodeToApplicationJsonByDefault() throws ExecutionException, InterruptedException {
Map<String, Object> data = mapOf(
"firstName", "John",
"lastName", "Doe",
"age", 30,
"isMale", true,
"file", mapOf("name", "f.js")
);
Future<Server.Request> req = server.futureRequest("/empty.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ void shouldThrowForNonStringHeaderValues() {
PlaywrightException e = assertThrows(PlaywrightException.class, () -> {
browser.newContext(new Browser.NewContextOptions().setExtraHTTPHeaders(mapOf("foo", null)));
});
assertTrue(e.getMessage().contains("expected string, got undefined"));
assertTrue(e.getMessage().contains("Value cannot be null"));
}
}

0 comments on commit 35e3c36

Please sign in to comment.