/** * Copyright (c) Microsoft Corporation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { contextTest as it, expect } from './config/browserTest'; it('should work', async ({context, server}) => { // @ts-expect-error const response = await context._fetch(server.PREFIX + '/simple.json'); expect(response.url()).toBe(server.PREFIX + '/simple.json'); expect(response.status()).toBe(200); expect(response.statusText()).toBe('OK'); expect(response.ok()).toBeTruthy(); expect(response.url()).toBe(server.PREFIX + '/simple.json'); expect(response.headers()['content-type']).toBe('application/json; charset=utf-8'); expect(await response.text()).toBe('{"foo": "bar"}\n'); }); it('should add session cookies to request', async ({context, server, isLinux}) => { await context.addCookies([{ name: 'username', value: 'John Doe', domain: isLinux ? '.my.localhost' : 'localhost', path: '/', expires: -1, httpOnly: false, secure: false, sameSite: 'Lax', }]); const [req] = await Promise.all([ server.waitForRequest('/simple.json'), // @ts-expect-error context._fetch(`http://${isLinux ? 'www.my.localhost' : 'localhost'}:${server.PORT}/simple.json`), ]); expect(req.headers.cookie).toEqual('username=John Doe'); }); it('should follow redirects', async ({context, server, isLinux}) => { server.setRedirect('/redirect1', '/redirect2'); server.setRedirect('/redirect2', '/simple.json'); await context.addCookies([{ name: 'username', value: 'John Doe', domain: isLinux ? '.my.localhost' : 'localhost', path: '/', expires: -1, httpOnly: false, secure: false, sameSite: 'Lax', }]); const [req, response] = await Promise.all([ server.waitForRequest('/simple.json'), // @ts-expect-error context._fetch(`http://${isLinux ? 'www.my.localhost' : 'localhost'}:${server.PORT}/redirect1`), ]); expect(req.headers.cookie).toEqual('username=John Doe'); expect(response.url()).toBe(`http://${isLinux ? 'www.my.localhost' : 'localhost'}:${server.PORT}/simple.json`); expect(await response.json()).toEqual({foo: 'bar'}); }); it('should add cookies from Set-Cookie header', async ({context, page, server}) => { server.setRoute('/setcookie.html', (req, res) => { res.setHeader('Set-Cookie', ['session=value', 'foo=bar; max-age=3600']); res.end(); }); // @ts-expect-error await context._fetch(server.PREFIX + '/setcookie.html'); const cookies = await context.cookies(); expect(new Set(cookies.map(c => ({ name: c.name, value: c.value })))).toEqual(new Set([ { name: 'session', value: 'value' }, { name: 'foo', value: 'bar' }, ])); await page.goto(server.EMPTY_PAGE); expect((await page.evaluate(() => document.cookie)).split(';').map(s => s.trim()).sort()).toEqual(['foo=bar', 'session=value']); }); it('should work with context level proxy', async ({browserOptions, browserType, contextOptions, server, proxyServer}) => { server.setRoute('/target.html', async (req, res) => { res.end('