"use strict";
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const issues_helpers_js_1 = require("../helpers/issues-helpers.js");
describe('Cors Local Network issues', () => {
    it('should display correct information for insecure contexts', async ({ devToolsPage, inspectedPage }) => {
        await inspectedPage.goToResource('empty.html');
        await (0, issues_helpers_js_1.navigateToIssuesTab)(devToolsPage);
        await devToolsPage.evaluate(() => {
            const issue = {
                code: 'CorsIssue',
                details: {
                    corsIssueDetails: {
                        clientSecurityState: {
                            initiatorIsSecureContext: false,
                            initiatorIPAddressSpace: 'Public',
                            localNetworkRequestPolicy: 'WarnFromInsecureToMorePrivate',
                        },
                        corsErrorStatus: { corsError: 'InsecureLocalNetwork', failedParameter: '' },
                        isWarning: true,
                        request: { requestId: 'request-1', url: 'http://localhost/' },
                        resourceIPAddressSpace: 'Local',
                    },
                },
            };
            // @ts-expect-error
            window.addIssueForTest(issue);
            const issue2 = {
                code: 'CorsIssue',
                details: {
                    corsIssueDetails: {
                        clientSecurityState: {
                            initiatorIsSecureContext: false,
                            initiatorIPAddressSpace: 'Unknown',
                            localNetworkRequestPolicy: 'WarnFromInsecureToMorePrivate',
                        },
                        corsErrorStatus: { corsError: 'InsecureLocalNetwork', failedParameter: '' },
                        isWarning: true,
                        request: { requestId: 'request-1', url: 'http://example.com/' },
                        resourceIPAddressSpace: 'Local',
                    },
                },
            };
            // @ts-expect-error
            window.addIssueForTest(issue2);
        });
        await (0, issues_helpers_js_1.expandIssue)(devToolsPage);
        const issueElement = await (0, issues_helpers_js_1.getIssueByTitle)('Ensure that local network requests are compatible with restrictions', devToolsPage);
        chai_1.assert.isOk(issueElement);
        const section = await (0, issues_helpers_js_1.getResourcesElement)('2 requests', issueElement, '.cors-issue-affected-resource-label', devToolsPage);
        await (0, issues_helpers_js_1.ensureResourceSectionIsExpanded)(section, devToolsPage);
        const expectedTableRows = [
            [
                'Request',
                'Status',
                'Resource Address',
                'Initiator Address',
                'Initiator Context',
            ],
            [
                'localhost/',
                'warning',
                'Local',
                'Public',
                'insecure',
            ],
            [
                'example.com/',
                'warning',
                'Local',
                'Unknown',
                'insecure',
            ],
        ];
        await (0, issues_helpers_js_1.waitForTableFromResourceSectionContents)(section.content, expectedTableRows, devToolsPage);
    });
    it('should display correct information for secure contexts', async ({ devToolsPage, inspectedPage }) => {
        await inspectedPage.goToResource('empty.html');
        await (0, issues_helpers_js_1.navigateToIssuesTab)(devToolsPage);
        await devToolsPage.evaluate(() => {
            const issue = {
                code: 'CorsIssue',
                details: {
                    corsIssueDetails: {
                        clientSecurityState: {
                            initiatorIsSecureContext: true,
                            initiatorIPAddressSpace: 'Public',
                            localNetworkRequestPolicy: 'WarnFromInsecureToMorePrivate',
                        },
                        corsErrorStatus: { corsError: 'InsecureLocalNetwork', failedParameter: '' },
                        isWarning: true,
                        request: { requestId: 'request-1', url: 'http://localhost/' },
                        resourceIPAddressSpace: 'Local',
                    },
                },
            };
            // @ts-expect-error
            window.addIssueForTest(issue);
            const issue2 = {
                code: 'CorsIssue',
                details: {
                    corsIssueDetails: {
                        clientSecurityState: {
                            initiatorIsSecureContext: true,
                            initiatorIPAddressSpace: 'Unknown',
                            localNetworkRequestPolicy: 'WarnFromInsecureToMorePrivate',
                        },
                        corsErrorStatus: { corsError: 'InsecureLocalNetwork', failedParameter: '' },
                        isWarning: true,
                        request: { requestId: 'request-1', url: 'http://example.com/' },
                        resourceIPAddressSpace: 'Local',
                    },
                },
            };
            // @ts-expect-error
            window.addIssueForTest(issue2);
        });
        await (0, issues_helpers_js_1.expandIssue)(devToolsPage);
        const issueElement = await (0, issues_helpers_js_1.getIssueByTitle)('Ensure that local network requests are compatible with restrictions', devToolsPage);
        chai_1.assert.isOk(issueElement);
        const section = await (0, issues_helpers_js_1.getResourcesElement)('2 requests', issueElement, '.cors-issue-affected-resource-label', devToolsPage);
        await (0, issues_helpers_js_1.ensureResourceSectionIsExpanded)(section, devToolsPage);
        const expectedTableRows = [
            [
                'Request',
                'Status',
                'Resource Address',
                'Initiator Address',
                'Initiator Context',
            ],
            [
                'localhost/',
                'warning',
                'Local',
                'Public',
                'secure',
            ],
            [
                'example.com/',
                'warning',
                'Local',
                'Unknown',
                'secure',
            ],
        ];
        await (0, issues_helpers_js_1.waitForTableFromResourceSectionContents)(section.content, expectedTableRows, devToolsPage);
    });
});
//# sourceMappingURL=cors-local-network-issues.test.js.map