From 65819b406139e08c2b3b37abe4c2ece77b42f91c Mon Sep 17 00:00:00 2001 From: Nivedha Date: Thu, 23 Jul 2020 14:02:46 +0530 Subject: [PATCH] #1359 fix extra headers (#1368) * #1359 fix extra headers Signed-off-by: NivedhaSenthil * Bump up version to 1.0.15 Signed-off-by: NivedhaSenthil * #1359 fix tests Signed-off-by: NivedhaSenthil Co-authored-by: Vinay Shankar Shukla Co-authored-by: Zabil Cheriya Maliackal --- lib/handlers/fetchHandler.js | 9 ++++++- lib/taiko.js | 2 +- package-lock.json | 2 +- package.json | 2 +- test/functional-tests/specs/pageActions.spec | 4 +++ test/functional-tests/tests/htmlElementAPI.ts | 8 ++++++ test/unit-tests/handlers/fetchHandler.test.js | 26 +++++++++++++++++-- types/taiko/index.d.ts | 2 +- 8 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/handlers/fetchHandler.js b/lib/handlers/fetchHandler.js index 282f11764..074d658d8 100644 --- a/lib/handlers/fetchHandler.js +++ b/lib/handlers/fetchHandler.js @@ -48,6 +48,9 @@ const addExtraHeadersToRequest = (p) => { options.headers[headerName] = headers[headerName]; } } + if (options.headers) { + options.headers = headerArray(options.headers); + } return options; }; @@ -251,9 +254,13 @@ const resetInterceptors = () => { userEnabledIntercept = false; }; -const setHTTPHeaders = (headers, url) => { +const setHTTPHeaders = async (headers, url) => { let host = extractHostName(url); headersMap.set(host, headers); + if (!userEnabledIntercept) { + userEnabledIntercept = true; + await enableFetchIntercept(); + } }; module.exports = { diff --git a/lib/taiko.js b/lib/taiko.js index 35bd70683..6e9c2ad2c 100644 --- a/lib/taiko.js +++ b/lib/taiko.js @@ -909,7 +909,7 @@ module.exports.goto = async ( url = 'http://' + url; } if (options.headers) { - fetchHandler.setHTTPHeaders(options.headers, url); + await fetchHandler.setHTTPHeaders(options.headers, url); } options = setNavigationOptions(options); let response; diff --git a/package-lock.json b/package-lock.json index e83cf517e..06e7e30c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "taiko", - "version": "1.0.14", + "version": "1.0.15", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 1b793217f..fbaf83412 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "http://json.schemastore.org/package", "name": "taiko", - "version": "1.0.14", + "version": "1.0.15", "description": "Taiko is a Node.js library for automating Chromium based browsers", "main": "bin/taiko.js", "bin": { diff --git a/test/functional-tests/specs/pageActions.spec b/test/functional-tests/specs/pageActions.spec index 5d16787b8..570713f8e 100644 --- a/test/functional-tests/specs/pageActions.spec +++ b/test/functional-tests/specs/pageActions.spec @@ -99,3 +99,7 @@ * Navigate to relative path "./specs/data/samePageNavigation.html#gauge-navigation" * Navigate to relative path "./specs/data/samePageNavigation.html#gauge-navigation" +## Goto with basic authentication +* Navigate to "http://localhost:3001/basic_auth" with basic auth "admin" and "admin" +* Assert text "Congratulations! You must have the proper credentials." exists on the page. + diff --git a/test/functional-tests/tests/htmlElementAPI.ts b/test/functional-tests/tests/htmlElementAPI.ts index e75305e6a..7b032f8f0 100644 --- a/test/functional-tests/tests/htmlElementAPI.ts +++ b/test/functional-tests/tests/htmlElementAPI.ts @@ -36,6 +36,14 @@ export default class HtmlElementAPI { await goto(url); } + @Step('Navigate to with basic auth and ') + public async navigateWithBasicAuth(url, username, password) { + const encodedCredentials = Buffer.from(`${username}:${password}`).toString('base64'); + await goto(url, { + headers: { Authorization: `Basic ${encodedCredentials}` }, + }); + } + @Step('Ensure Drop down exists') public async dropdownExists(dropDownName) { const box = dropDown(dropDownName); diff --git a/test/unit-tests/handlers/fetchHandler.test.js b/test/unit-tests/handlers/fetchHandler.test.js index 2cebdb771..34846cb5c 100644 --- a/test/unit-tests/handlers/fetchHandler.test.js +++ b/test/unit-tests/handlers/fetchHandler.test.js @@ -10,6 +10,8 @@ describe('Fetch Handler', () => { before(() => { fetchHandler = rewire('../../../lib/handlers/fetchHandler'); let fetch = { + enable: () => {}, + requestPaused: () => {}, continueRequest: async (options) => { continueInterceptedRequestOptions = options; }, @@ -27,13 +29,33 @@ describe('Fetch Handler', () => { }); describe('http headers', () => { - let headersAndHost; + let headersAndHost, expectedHeaders; before(() => { headersAndHost = [ [{ header1: 'header1 value' }, 'https://example.com'], [{ header3: 'header2 value' }, 'https://another-example.com'], [{ header4: 'header3 value' }, 'file://path/to/some/file'], ]; + expectedHeaders = [ + [ + { + name: 'header1', + value: 'header1 value', + }, + ], + [ + { + name: 'header3', + value: 'header2 value', + }, + ], + [ + { + name: 'header4', + value: 'header3 value', + }, + ], + ]; headersAndHost.forEach((headerAndHost) => { fetchHandler.setHTTPHeaders(headerAndHost[0], headerAndHost[1]); }); @@ -41,7 +63,7 @@ describe('Fetch Handler', () => { it('should set appropriate headers for a host', () => { headersAndHost.forEach((headerAndHost, index) => { const hostUrl = headerAndHost[1]; - const headers = headerAndHost[0]; + const headers = expectedHeaders[index]; requestInterceptor({ requestId: index, request: { url: hostUrl, headers: {} }, diff --git a/types/taiko/index.d.ts b/types/taiko/index.d.ts index c38cc402f..6fc36c416 100644 --- a/types/taiko/index.d.ts +++ b/types/taiko/index.d.ts @@ -35,7 +35,7 @@ declare module 'taiko' { } export interface NavigationOptions extends BasicNavigationOptions, EventOptions { - headers?: Map; + headers?: object; waitForStart?: boolean; }