Skip to content

Commit

Permalink
#1359 fix extra headers (#1368)
Browse files Browse the repository at this point in the history
* #1359 fix extra headers

Signed-off-by: NivedhaSenthil <[email protected]>

* Bump up version to 1.0.15

Signed-off-by: NivedhaSenthil <[email protected]>

* #1359 fix tests

Signed-off-by: NivedhaSenthil <[email protected]>

Co-authored-by: Vinay Shankar Shukla <[email protected]>
Co-authored-by: Zabil Cheriya Maliackal <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2020
1 parent c3b74c2 commit 65819b4
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/handlers/fetchHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const addExtraHeadersToRequest = (p) => {
options.headers[headerName] = headers[headerName];
}
}
if (options.headers) {
options.headers = headerArray(options.headers);
}
return options;
};

Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion lib/taiko.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions test/functional-tests/specs/pageActions.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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.

8 changes: 8 additions & 0 deletions test/functional-tests/tests/htmlElementAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export default class HtmlElementAPI {
await goto(url);
}

@Step('Navigate to <url> with basic auth <username> and <password>')
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 <dropDownName> exists')
public async dropdownExists(dropDownName) {
const box = dropDown(dropDownName);
Expand Down
26 changes: 24 additions & 2 deletions test/unit-tests/handlers/fetchHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('Fetch Handler', () => {
before(() => {
fetchHandler = rewire('../../../lib/handlers/fetchHandler');
let fetch = {
enable: () => {},
requestPaused: () => {},
continueRequest: async (options) => {
continueInterceptedRequestOptions = options;
},
Expand All @@ -27,21 +29,41 @@ 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]);
});
});
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: {} },
Expand Down
2 changes: 1 addition & 1 deletion types/taiko/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ declare module 'taiko' {
}

export interface NavigationOptions extends BasicNavigationOptions, EventOptions {
headers?: Map<string, string>;
headers?: object;
waitForStart?: boolean;
}

Expand Down

0 comments on commit 65819b4

Please sign in to comment.