Skip to content

✨ Custom Headers #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions app/custom_headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!DOCTYPE html>
<html>
<head>
<title>RUM Integ Test</title>
<script src="./loader_custom_headers.js"></script>
<link
rel="icon"
type="image/png"
href="https://awsmedia.s3.amazonaws.com/favicon.ico"
/>

<script>
// Common to all test pages
function dispatch() {
cwr('dispatch');
}

function clearRequestResponse() {
document.getElementById('request_url').innerText = '';
document.getElementById('request_header').innerText = '';
document.getElementById('request_body').innerText = '';

document.getElementById('response_status').innerText = '';
document.getElementById('response_header').innerText = '';
document.getElementById('response_body').innerText = '';
}

function disable() {
cwr('disable');
}

function enable() {
cwr('enable');
}
</script>

<style>
table {
border-collapse: collapse;
margin-top: 10px;
margin-bottom: 10px;
}

td,
th {
border: 1px solid black;
text-align: left;
padding: 8px;
}
</style>
</head>
<body>
<p id="welcome">This application is used for RUM integ testing.</p>

<button id="disable" onclick="disable()">Disable</button>
<button id="enable" onclick="enable()">Enable</button>
<hr />
<button id="dispatch" onclick="dispatch()">Dispatch</button>
<button id="testButton">Test Button</button>

<hr />
<span id="request"></span>
<span id="response"></span>
<table>
<tr>
<td>Request URL</td>
<td id="request_url"></td>
</tr>
<tr>
<td>Request Header</td>
<td id="request_header"></td>
</tr>
<tr>
<td>Request Body</td>
<td id="request_body"></td>
</tr>
</table>
<table>
<tr>
<td>Response Status Code</td>
<td id="response_status"></td>
</tr>
<tr>
<td>Response Header</td>
<td id="response_header"></td>
</tr>
<tr>
<td>Response Body</td>
<td id="response_body"></td>
</tr>
</table>
<img src="blank.png" alt="sample" />
</body>
</html>
7 changes: 5 additions & 2 deletions src/dispatch/DataPlaneClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
AwsCredentialIdentityProvider,
AwsCredentialIdentity,
HttpResponse,
RequestPresigningArguments
RequestPresigningArguments,
HeaderBag
} from '@aws-sdk/types';
import { Sha256 } from '@aws-crypto/sha256-js';
import { HttpHandler, HttpRequest } from '@aws-sdk/protocol-http';
Expand Down Expand Up @@ -47,6 +48,7 @@ export declare type DataPlaneClientConfig = {
| AwsCredentialIdentityProvider
| AwsCredentialIdentity
| undefined;
headers?: HeaderBag;
};

export class DataPlaneClient {
Expand Down Expand Up @@ -118,7 +120,8 @@ export class DataPlaneClient {
port: Number(this.config.endpoint.port) || undefined,
headers: {
'content-type': contentType,
host: this.config.endpoint.host
host: this.config.endpoint.host,
...this.config.headers
},
hostname: this.config.endpoint.hostname,
path: `${path}/appmonitors/${putRumEventsRequest.AppMonitorDetails.id}`,
Expand Down
5 changes: 4 additions & 1 deletion src/dispatch/Dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Dispatch {
private buildClient: ClientBuilder;
private config: Config;
private disableCodes = ['403', '404'];
private headers: any;

constructor(
region: string,
Expand All @@ -52,6 +53,7 @@ export class Dispatch {
this.enabled = true;
this.buildClient = config.clientBuilder || this.defaultClientBuilder;
this.config = config;
this.headers = config.headers;
this.startDispatchTimer();
if (config.signing) {
this.rum = {
Expand Down Expand Up @@ -265,7 +267,8 @@ export class Dispatch {
beaconRequestHandler: new BeaconHttpHandler(),
endpoint,
region,
credentials
credentials,
headers: this.headers
});
};
}
28 changes: 28 additions & 0 deletions src/loader/loader-custom-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { loader } from './loader';
import { showRequestClientBuilder } from '../test-utils/mock-http-custom-headers';

const token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
loader(
'cwr',
'a1b2c3d4-c3f5-1a2b-b2b4-012345678910',
'1.0',
'us-west-2',
'./rum_javascript_telemetry.js',
{
userIdRetentionDays: 1,
dispatchInterval: 0,
allowCookies: false,
eventPluginsToLoad: [],
telemetries: [],
clientBuilder: showRequestClientBuilder,
signing: false,
endpoint:
'https://api-id.execute-api.region.amazonaws.com/api/dataplane',
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': 'a1b2c3d4e5f6',
'content-type': 'application/json'
}
}
);
4 changes: 3 additions & 1 deletion src/orchestration/Orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { EventCache } from '../event-cache/EventCache';
import { ClientBuilder, Dispatch } from '../dispatch/Dispatch';
import {
AwsCredentialIdentityProvider,
AwsCredentialIdentity
AwsCredentialIdentity,
HeaderBag
} from '@aws-sdk/types';
import { NavigationPlugin } from '../plugins/event-plugins/NavigationPlugin';
import { ResourcePlugin } from '../plugins/event-plugins/ResourcePlugin';
Expand Down Expand Up @@ -160,6 +161,7 @@ export interface Config {
useBeacon: boolean;
userIdRetentionDays: number;
alias?: string;
headers?: HeaderBag;
}

export interface PartialConfig
Expand Down
48 changes: 48 additions & 0 deletions src/test-utils/mock-http-custom-headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HttpHandler, HttpRequest, HttpResponse } from '@aws-sdk/protocol-http';
import { ClientBuilder } from '../dispatch/Dispatch';
import { DataPlaneClient } from '../dispatch/DataPlaneClient';
import { logRequestToPage, logResponseToPage } from './http-handler-utils';

/**
* Returns data plane service client with a mocked request handler.
*
* @param endpoint Service endpoint.
* @param region Service region.
* @param credentials AWS credentials.
*/
const token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
export const showRequestClientBuilder: ClientBuilder = (
endpoint,
region,
credentials
) => {
return new DataPlaneClient({
fetchRequestHandler: new ShowMockRequestHandler(),
beaconRequestHandler: new ShowMockRequestHandler(),
endpoint,
region,
credentials,
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': 'a1b2c3d4e5f6',
'content-type': 'application/json'
}
});
};

class ShowMockRequestHandler implements HttpHandler {
handle(request: HttpRequest): Promise<{ response: HttpResponse }> {
const response: HttpResponse = {
statusCode: 202,
headers: {
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: undefined
};
logRequestToPage(request);
logResponseToPage(response);
return Promise.resolve({ response });
}
}
3 changes: 2 additions & 1 deletion webpack/webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = merge(common, {
loader_remote_config: './src/loader/loader-remote-config.js',
loader_spa: './src/loader/loader-spa.js',
loader_custom_events: './src/loader/loader-custom-events.js',
loader_alias: './src/loader/loader-alias.js'
loader_alias: './src/loader/loader-alias.js',
loader_custom_headers: './src/loader/loader-custom-headers.js'
},
resolve: {
extensions: ['.ts', '.js', '.json']
Expand Down