Skip to content

Commit 6019584

Browse files
committed
fix(provider-webdriver): send an empty JSON object on bodyless POSTs
The W3C WebDriver protocol makes every POST body a JSON object, and a gateway in front of the driver may enforce it: AWS Device Farm's remote access endpoint refused a bodyless POST /back with "Value null at 'payload' failed to satisfy constraint: Member must not be null", so back failed on every Device Farm session while Appium itself accepted the request. The transport now sends {} for a POST with no parameters; DELETE and GET stay bodyless.
1 parent 2f933e5 commit 6019584

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎packages/provider-webdriver/src/webdriver-transport.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,43 @@ test('cancels a retry delay when the request binding aborts', async () => {
112112
assert.equal(calls, 1);
113113
});
114114

115+
// AWS Device Farm's remote access endpoint sits in front of Appium and validates the request
116+
// against the W3C protocol, where every POST body is a JSON object: a `POST /back` with no body
117+
// came back as `Value null at 'payload' failed to satisfy constraint: Member must not be null`.
118+
test('a POST without parameters carries an empty JSON object body', async () => {
119+
const seen: { method: string | undefined; contentType: string | undefined; body: string }[] = [];
120+
const driver = await localDriver((request, response) => {
121+
let body = '';
122+
request.on('data', (chunk: Buffer) => {
123+
body += chunk.toString();
124+
});
125+
request.on('end', () => {
126+
seen.push({ method: request.method, contentType: request.headers['content-type'], body });
127+
response.writeHead(200, { 'Content-Type': 'application/json' });
128+
response.end(JSON.stringify({ value: null }));
129+
});
130+
});
131+
try {
132+
const transport = new WebDriverTransport({
133+
clientVersion: '0.0.0-test',
134+
endpoint: driver.endpoint,
135+
requestPolicy: { timeoutMs: 5_000, retryAttempts: 0 },
136+
});
137+
138+
await transport.requestValue('POST', '/session/wd-1/back');
139+
await transport.requestValue('DELETE', '/session/wd-1/actions');
140+
await transport.requestValue('GET', '/session/wd-1/source');
141+
142+
assert.deepEqual(seen, [
143+
{ method: 'POST', contentType: 'application/json', body: '{}' },
144+
{ method: 'DELETE', contentType: undefined, body: '' },
145+
{ method: 'GET', contentType: undefined, body: '' },
146+
]);
147+
} finally {
148+
await driver.close();
149+
}
150+
});
151+
115152
/** A 127.0.0.1 port nothing listens on: bound, then released. */
116153
async function refusedPort(): Promise<number> {
117154
const server = net.createServer();

‎packages/provider-webdriver/src/webdriver-transport.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ type ResolvedWebDriverRequestPolicy = Required<
117117
*/
118118
const IDEMPOTENT_METHODS: ReadonlySet<string> = new Set(['GET', 'HEAD']);
119119

120+
/**
121+
* What a POST without parameters carries. The W3C WebDriver protocol makes every POST body a JSON
122+
* object, and a gateway in front of the driver may enforce it: AWS Device Farm's remote access
123+
* endpoint refuses a bodyless `POST /back` with `Value null at 'payload'`, while Appium itself
124+
* accepts either.
125+
*/
126+
const EMPTY_POST_BODY: Readonly<Record<string, never>> = Object.freeze({});
127+
120128
/** A successful WebDriver answer: its HTTP status and the unwrapped W3C `value`. */
121129
export type WebDriverAnswer = { status: number; value: unknown };
122130

@@ -224,6 +232,7 @@ export class WebDriverTransport {
224232
requestSignal?: AbortSignal,
225233
): Promise<Pick<Response, 'ok' | 'status'> & { text: string }> {
226234
const url = new URL(trimLeadingSlash(path), this.endpoint);
235+
const payload = body === undefined && method === 'POST' ? EMPTY_POST_BODY : body;
227236
const timeoutSignal = AbortSignal.timeout(timeoutMs);
228237
const signal = requestSignal ? AbortSignal.any([requestSignal, timeoutSignal]) : timeoutSignal;
229238
const failure = { method, path, timeoutMs, timeoutSignal, requestSignal };
@@ -233,10 +242,10 @@ export class WebDriverTransport {
233242
method,
234243
headers: {
235244
Accept: 'application/json',
236-
...(body === undefined ? {} : { 'Content-Type': 'application/json' }),
245+
...(payload === undefined ? {} : { 'Content-Type': 'application/json' }),
237246
...this.headers,
238247
},
239-
body: body === undefined ? undefined : JSON.stringify(body),
248+
body: payload === undefined ? undefined : JSON.stringify(payload),
240249
signal,
241250
});
242251
} catch (error) {

0 commit comments

Comments
 (0)