mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: api testing test nits (#10180)
This commit is contained in:
parent
c30447216d
commit
d25b0f70bc
@ -214,7 +214,7 @@ export class APIResponse implements api.APIResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ok(): boolean {
|
ok(): boolean {
|
||||||
return this._initializer.status === 0 || (this._initializer.status >= 200 && this._initializer.status <= 299);
|
return this._initializer.status >= 200 && this._initializer.status <= 299;
|
||||||
}
|
}
|
||||||
|
|
||||||
url(): string {
|
url(): string {
|
||||||
|
@ -360,6 +360,7 @@ export class Response extends ChannelOwner<channels.ResponseChannel, channels.Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
ok(): boolean {
|
ok(): boolean {
|
||||||
|
// Status 0 is for file:// URLs
|
||||||
return this._initializer.status === 0 || (this._initializer.status >= 200 && this._initializer.status <= 299);
|
return this._initializer.status === 0 || (this._initializer.status >= 200 && this._initializer.status <= 299);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ export abstract class APIRequestContext extends SdkObject {
|
|||||||
return { error: `${fetchResponse.status} ${fetchResponse.statusText}` };
|
return { error: `${fetchResponse.status} ${fetchResponse.statusText}` };
|
||||||
return { fetchResponse: { ...fetchResponse, fetchUid } };
|
return { fetchResponse: { ...fetchResponse, fetchUid } };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return { error: String(e) };
|
return { error: e instanceof Error ? e.message : String(e) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,6 @@ it('get should work', async ({ context, server }) => {
|
|||||||
expect(response.status()).toBe(200);
|
expect(response.status()).toBe(200);
|
||||||
expect(response.statusText()).toBe('OK');
|
expect(response.statusText()).toBe('OK');
|
||||||
expect(response.ok()).toBeTruthy();
|
expect(response.ok()).toBeTruthy();
|
||||||
expect(response.url()).toBe(server.PREFIX + '/simple.json');
|
|
||||||
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
|
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
|
||||||
expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' });
|
expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' });
|
||||||
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
||||||
@ -60,7 +59,6 @@ it('fetch should work', async ({ context, server }) => {
|
|||||||
expect(response.status()).toBe(200);
|
expect(response.status()).toBe(200);
|
||||||
expect(response.statusText()).toBe('OK');
|
expect(response.statusText()).toBe('OK');
|
||||||
expect(response.ok()).toBeTruthy();
|
expect(response.ok()).toBeTruthy();
|
||||||
expect(response.url()).toBe(server.PREFIX + '/simple.json');
|
|
||||||
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
|
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
|
||||||
expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' });
|
expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' });
|
||||||
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
||||||
@ -94,7 +92,7 @@ it('should throw on network error when sending body', async ({ context, server }
|
|||||||
req.socket.destroy();
|
req.socket.destroy();
|
||||||
});
|
});
|
||||||
const error = await context.request.get(server.PREFIX + '/test').catch(e => e);
|
const error = await context.request.get(server.PREFIX + '/test').catch(e => e);
|
||||||
expect(error.message).toContain('Error: aborted');
|
expect(error.message).toContain(': aborted');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw on network error when sending body after redirect', async ({ context, server }) => {
|
it('should throw on network error when sending body after redirect', async ({ context, server }) => {
|
||||||
@ -109,7 +107,7 @@ it('should throw on network error when sending body after redirect', async ({ co
|
|||||||
req.socket.destroy();
|
req.socket.destroy();
|
||||||
});
|
});
|
||||||
const error = await context.request.get(server.PREFIX + '/redirect').catch(e => e);
|
const error = await context.request.get(server.PREFIX + '/redirect').catch(e => e);
|
||||||
expect(error.message).toContain('Error: aborted');
|
expect(error.message).toContain(': aborted');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add session cookies to request', async ({ context, server }) => {
|
it('should add session cookies to request', async ({ context, server }) => {
|
||||||
@ -130,22 +128,20 @@ it('should add session cookies to request', async ({ context, server }) => {
|
|||||||
expect(req.headers.cookie).toEqual('username=John Doe');
|
expect(req.headers.cookie).toEqual('username=John Doe');
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put']) {
|
for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put'] as const) {
|
||||||
it(`${method} should support queryParams`, async ({ context, server }) => {
|
it(`${method} should support queryParams`, async ({ context, server }) => {
|
||||||
let request;
|
|
||||||
const url = new URL(server.EMPTY_PAGE);
|
const url = new URL(server.EMPTY_PAGE);
|
||||||
url.searchParams.set('p1', 'v1');
|
url.searchParams.set('p1', 'v1');
|
||||||
url.searchParams.set('парам2', 'знач2');
|
url.searchParams.set('парам2', 'знач2');
|
||||||
server.setRoute(url.pathname + url.search, (req, res) => {
|
const [request] = await Promise.all([
|
||||||
request = req;
|
server.waitForRequest(url.pathname + url.search),
|
||||||
server.serveFile(req, res);
|
context.request[method](server.EMPTY_PAGE + '?p1=foo', {
|
||||||
});
|
|
||||||
await context.request[method](server.EMPTY_PAGE + '?p1=foo', {
|
|
||||||
params: {
|
params: {
|
||||||
'p1': 'v1',
|
'p1': 'v1',
|
||||||
'парам2': 'знач2',
|
'парам2': 'знач2',
|
||||||
}
|
}
|
||||||
});
|
}),
|
||||||
|
]);
|
||||||
const params = new URLSearchParams(request.url.substr(request.url.indexOf('?')));
|
const params = new URLSearchParams(request.url.substr(request.url.indexOf('?')));
|
||||||
expect(params.get('p1')).toEqual('v1');
|
expect(params.get('p1')).toEqual('v1');
|
||||||
expect(params.get('парам2')).toEqual('знач2');
|
expect(params.get('парам2')).toEqual('знач2');
|
||||||
|
@ -38,7 +38,7 @@ it.afterAll(() => {
|
|||||||
http.globalAgent = prevAgent;
|
http.globalAgent = prevAgent;
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put']) {
|
for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put'] as const) {
|
||||||
it(`${method} should work`, async ({ playwright, server }) => {
|
it(`${method} should work`, async ({ playwright, server }) => {
|
||||||
const request = await playwright.request.newContext();
|
const request = await playwright.request.newContext();
|
||||||
const response = await request[method](server.PREFIX + '/simple.json');
|
const response = await request[method](server.PREFIX + '/simple.json');
|
||||||
@ -46,13 +46,13 @@ for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put'])
|
|||||||
expect(response.status()).toBe(200);
|
expect(response.status()).toBe(200);
|
||||||
expect(response.statusText()).toBe('OK');
|
expect(response.statusText()).toBe('OK');
|
||||||
expect(response.ok()).toBeTruthy();
|
expect(response.ok()).toBeTruthy();
|
||||||
expect(response.url()).toBe(server.PREFIX + '/simple.json');
|
|
||||||
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
|
expect(response.headers()['content-type']).toBe('application/json; charset=utf-8');
|
||||||
expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' });
|
expect(response.headersArray()).toContainEqual({ name: 'Content-Type', value: 'application/json; charset=utf-8' });
|
||||||
expect(await response.text()).toBe(method === 'head' ? '' : '{"foo": "bar"}\n');
|
expect(await response.text()).toBe(method === 'head' ? '' : '{"foo": "bar"}\n');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
it(`should dispose global ${method} request`, async function({ playwright, context, server }) {
|
it(`should dispose global request`, async function({ playwright, server }) {
|
||||||
const request = await playwright.request.newContext();
|
const request = await playwright.request.newContext();
|
||||||
const response = await request.get(server.PREFIX + '/simple.json');
|
const response = await request.get(server.PREFIX + '/simple.json');
|
||||||
expect(await response.json()).toEqual({ foo: 'bar' });
|
expect(await response.json()).toEqual({ foo: 'bar' });
|
||||||
@ -60,7 +60,6 @@ for (const method of ['fetch', 'delete', 'get', 'head', 'patch', 'post', 'put'])
|
|||||||
const error = await response.body().catch(e => e);
|
const error = await response.body().catch(e => e);
|
||||||
expect(error.message).toContain('Response has been disposed');
|
expect(error.message).toContain('Response has been disposed');
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
it('should support global userAgent option', async ({ playwright, server }) => {
|
it('should support global userAgent option', async ({ playwright, server }) => {
|
||||||
const request = await playwright.request.newContext({ userAgent: 'My Agent' });
|
const request = await playwright.request.newContext({ userAgent: 'My Agent' });
|
||||||
@ -111,8 +110,8 @@ it('should support global httpCredentials option', async ({ playwright, server }
|
|||||||
it('should return error with wrong credentials', async ({ playwright, server }) => {
|
it('should return error with wrong credentials', async ({ playwright, server }) => {
|
||||||
server.setAuth('/empty.html', 'user', 'pass');
|
server.setAuth('/empty.html', 'user', 'pass');
|
||||||
const request = await playwright.request.newContext({ httpCredentials: { username: 'user', password: 'wrong' } });
|
const request = await playwright.request.newContext({ httpCredentials: { username: 'user', password: 'wrong' } });
|
||||||
const response2 = await request.get(server.EMPTY_PAGE);
|
const response = await request.get(server.EMPTY_PAGE);
|
||||||
expect(response2.status()).toBe(401);
|
expect(response.status()).toBe(401);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use socks proxy', async ({ playwright, server, socksPort }) => {
|
it('should use socks proxy', async ({ playwright, server, socksPort }) => {
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import url from 'url';
|
||||||
import { test as it, expect } from './pageTest';
|
import { test as it, expect } from './pageTest';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
@ -267,3 +268,12 @@ it('should behave the same way for headers and allHeaders', async ({ page, serve
|
|||||||
expect(allHeaders['name-b']).toEqual('v4');
|
expect(allHeaders['name-b']).toEqual('v4');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should provide a Response with a file URL', async ({ page, asset, isAndroid, browserName }) => {
|
||||||
|
it.skip(isAndroid, 'No files on Android');
|
||||||
|
it.fixme(browserName === 'firefox', 'Firefox does return null for file:// URLs');
|
||||||
|
|
||||||
|
const fileurl = url.pathToFileURL(asset('frames/two-frames.html')).href;
|
||||||
|
const response = await page.goto(fileurl);
|
||||||
|
expect(response.status()).toBe(0);
|
||||||
|
expect(response.ok()).toBe(true);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user