Skip to content

Commit cf3f967

Browse files
committed
pass-args
1 parent d68a8ae commit cf3f967

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

packages/synthetics-sdk-broken-links/src/broken_links.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ try {
101101
instantiateMetadata(synthetics_sdk_broken_links_package);
102102

103103
export async function runBrokenLinks(
104-
inputOptions: BrokenLinkCheckerOptions
104+
inputOptions: BrokenLinkCheckerOptions,
105+
args: {
106+
executionId: string | undefined;
107+
checkId: string | undefined;
108+
}
105109
): Promise<SyntheticResult> {
106110
// init
107111
const startTime = new Date().toISOString();
@@ -138,9 +142,9 @@ export async function runBrokenLinks(
138142
const storageParams: StorageParameters = {
139143
storageClient: storageClient,
140144
bucket: bucket,
141-
checkId: 'fake-check-id',
142-
executionId: 'fake-execution-id',
143-
screenshotNumber: 1,
145+
checkId: args.checkId || '_',
146+
executionId: args.executionId || '_',
147+
screenshotNumber: 1
144148
};
145149

146150
const followed_links: BrokenLinksResultV1_SyntheticLinkResult[] = [];

packages/synthetics-sdk-broken-links/src/handlers.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { Request, Response } from 'express';
1818
// Internal Project Files
1919
import { runBrokenLinks, BrokenLinkCheckerOptions } from './broken_links';
2020

21+
const syntheticExecutionIdHeader = 'Synthetic-Execution-Id';
22+
const checkIdHeader = 'Check-Id';
23+
2124
/**
2225
* Middleware for easy invocation of SyntheticSDK broken links, and may be used to
2326
* register a GoogleCloudFunction http function, or express js compatible handler.
@@ -29,5 +32,10 @@ import { runBrokenLinks, BrokenLinkCheckerOptions } from './broken_links';
2932
export function runBrokenLinksHandler(options: BrokenLinkCheckerOptions) {
3033
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3134
return async (req: Request, res: Response): Promise<any> =>
32-
res.send(await runBrokenLinks(options));
35+
res.send(
36+
await runBrokenLinks(options, {
37+
executionId: req.get(syntheticExecutionIdHeader),
38+
checkId: req.get(checkIdHeader),
39+
})
40+
);
3341
}

packages/synthetics-sdk-broken-links/test/unit/broken_links.spec.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('runBrokenLinks', async () => {
6060
screenshot_file: 'bucket/folder/file.png',
6161
screenshot_error: {} as BaseError,
6262
};
63+
const args = { checkId: 'test-check-id', executionId: 'test-execution-id' };
6364

6465
const mockedstorageFunc = proxyquire('../../src/storage_func', {
6566
'@google-cloud/synthetics-sdk-api': {
@@ -103,7 +104,7 @@ describe('runBrokenLinks', async () => {
103104
origin_uri: 'uri-does-not-start-with-http',
104105
};
105106

106-
const result = await runBrokenLinks(inputOptions);
107+
const result = await runBrokenLinks(inputOptions, args);
107108

108109
const genericResult = result.synthetic_generic_result_v1;
109110

@@ -137,7 +138,7 @@ describe('runBrokenLinks', async () => {
137138
screenshot_options: { capture_condition: CaptureCondition.NONE },
138139
};
139140

140-
const result = await mockedBlc.runBrokenLinks(inputOptions);
141+
const result = await mockedBlc.runBrokenLinks(inputOptions, args);
141142

142143
const broken_links_result = result?.synthetic_broken_links_result_v1;
143144
const origin_link = broken_links_result?.origin_link_result;
@@ -174,7 +175,7 @@ describe('runBrokenLinks', async () => {
174175
total_synthetic_timeout_millis: 31000,
175176
screenshot_options: { capture_condition: CaptureCondition.NONE },
176177
};
177-
const result = await mockedBlc.runBrokenLinks(inputOptions);
178+
const result = await mockedBlc.runBrokenLinks(inputOptions, args);
178179
const broken_links_result = result.synthetic_broken_links_result_v1;
179180

180181
const expectedOriginLinkResult: BrokenLinksResultV1_SyntheticLinkResult = {
@@ -210,7 +211,7 @@ describe('runBrokenLinks', async () => {
210211
screenshot_options: { capture_condition: CaptureCondition.NONE },
211212
};
212213

213-
const result = await runBrokenLinks(inputOptions);
214+
const result = await runBrokenLinks(inputOptions, args);
214215

215216
const broken_links_result = result?.synthetic_broken_links_result_v1;
216217
const origin_link = broken_links_result?.origin_link_result;
@@ -274,7 +275,7 @@ describe('runBrokenLinks', async () => {
274275
},
275276
};
276277

277-
const result = await mockedBlc.runBrokenLinks(inputOptions);
278+
const result = await mockedBlc.runBrokenLinks(inputOptions, args);
278279

279280
const broken_links_result = result?.synthetic_broken_links_result_v1;
280281
const options = broken_links_result?.options;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Standard Libraries
16+
import { expect } from 'chai';
17+
import sinon from 'sinon';
18+
19+
// External Dependency
20+
import httpMocks from 'node-mocks-http';
21+
const proxyquire = require('proxyquire');
22+
23+
import { runBrokenLinksHandler } from '../../src/handlers';
24+
import { BrokenLinkCheckerOptions } from '../../src/broken_links';
25+
26+
describe.only('Broken Links Synthetic Handler', async () => {
27+
it('has check id and execution id available', async () => {
28+
// Stub the runBrokenLinks function using Sinon
29+
const mockRunBrokenLinks = sinon.stub().callsFake(async (opts, args) => {
30+
return Promise.resolve({ mocked_response: 'is unimportant' });
31+
});
32+
const mockedBrokenLinks = proxyquire('../../src/handlers', {
33+
'./broken_links': { runBrokenLinks: mockRunBrokenLinks },
34+
});
35+
36+
// Options for the runBrokenLinksHandler
37+
const options: BrokenLinkCheckerOptions = {
38+
origin_uri: 'https://example.com',
39+
};
40+
41+
// Create mock request and response
42+
const req = httpMocks.createRequest({
43+
headers: {
44+
['Synthetic-Execution-Id']: 'test-execution-id',
45+
['Check-Id']: 'test-check-id',
46+
},
47+
});
48+
const res = httpMocks.createResponse();
49+
50+
// Call the middleware
51+
await mockedBrokenLinks.runBrokenLinksHandler(options)(req, res);
52+
53+
// Assertions with Sinon and Chai
54+
sinon.assert.calledWith(mockRunBrokenLinks, options, {
55+
executionId: 'test-execution-id',
56+
checkId: 'test-check-id',
57+
});
58+
expect(res.statusCode).to.equal(200);
59+
}).timeout(5000);
60+
});

0 commit comments

Comments
 (0)