Skip to content

Commit

Permalink
Merge pull request #292 from GSA/issue-702
Browse files Browse the repository at this point in the history
Move isLive to util and apply to sitemap and robots scans
  • Loading branch information
akuny authored Dec 15, 2023
2 parents 9a8eba8 + 86dbee6 commit 5d114dd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libs/core-scanner/src/pages/primary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('primary scanner', () => {
uswdsUsFlagInCss: 0,
uswdsStringInCss: 20,
uswdsPublicSansFont: 0,
uswdsSemanticVersion: 'v3.7.0',
uswdsSemanticVersion: 'v3.7.1',
uswdsVersion: 100,
uswdsCount: 197,
},
Expand Down
6 changes: 3 additions & 3 deletions libs/core-scanner/src/pages/robots-txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RobotsTxtScan } from 'entities/scan-data.entity';
import { RobotsTxtPageScans } from 'entities/scan-page.entity';

import { getHttpsUrl, getMIMEType } from '../util';
import { isLive } from '../util';

export const createRobotsTxtScanner = (logger: Logger, input: CoreInputDto) => {
const url = getHttpsUrl(input.url);
Expand Down Expand Up @@ -35,8 +36,7 @@ const buildRobotTxtResult = (
robotsText: string,
): RobotsTxtScan => {
const robotsUrl = new URL(robotsResponse.url());
const robotsStatus = robotsResponse.status();
const robotsLive = robotsStatus / 100 === 2;
const robotsLive = isLive(robotsResponse);
const robotsTxtDetected = robotsUrl.pathname === '/robots.txt' && robotsLive;

return {
Expand All @@ -45,7 +45,7 @@ const buildRobotTxtResult = (
robotsTxtTargetUrlRedirects:
robotsResponse.request().redirectChain().length > 0,
robotsTxtFinalUrlMimeType: getMIMEType(robotsResponse),
robotsTxtStatusCode: robotsStatus,
robotsTxtStatusCode: robotsResponse.status(),

robotsTxtDetected,
...(robotsTxtDetected
Expand Down
7 changes: 3 additions & 4 deletions libs/core-scanner/src/pages/sitemap-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CoreInputDto } from '@app/core-scanner/core.input.dto';
import { SitemapXmlScan } from 'entities/scan-data.entity';
import { SitemapXmlPageScans } from 'entities/scan-page.entity';

import { getHttpsUrl, getMIMEType } from '../util';
import { getHttpsUrl, getMIMEType, isLive } from '../util';

export const createSitemapXmlScanner = (
logger: Logger,
Expand Down Expand Up @@ -39,8 +39,7 @@ const buildSitemapResult = async (
sitemapPage: Page,
): Promise<SitemapXmlScan> => {
const sitemapUrl = new URL(sitemapResponse.url());
const sitemapStatus = sitemapResponse.status();
const sitemapLive = sitemapStatus / 100 === 2;
const sitemapLive = isLive(sitemapResponse);

const sitemapXmlDetected =
sitemapUrl.pathname === '/sitemap.xml' && sitemapLive;
Expand All @@ -51,7 +50,7 @@ const buildSitemapResult = async (
sitemapTargetUrlRedirects:
sitemapResponse.request().redirectChain().length > 0,
sitemapXmlFinalUrlMimeType: getMIMEType(sitemapResponse),
sitemapXmlStatusCode: sitemapStatus,
sitemapXmlStatusCode: sitemapResponse.status(),

sitemapXmlDetected,
...(sitemapXmlDetected
Expand Down
6 changes: 1 addition & 5 deletions libs/core-scanner/src/scans/url-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Page, HTTPRequest, HTTPResponse } from 'puppeteer';

import { CoreInputDto } from '@app/core-scanner/core.input.dto';
import { UrlScan } from 'entities/scan-data.entity';
import { isLive } from '../util';

import {
getBaseDomain,
Expand Down Expand Up @@ -43,8 +44,3 @@ const getFinalUrl = (page: Page) => {
const finalUrl = page.url();
return finalUrl;
};

const isLive = (res: HTTPResponse) => {
const http200FamilyCodes = [200, 201, 202, 203, 204, 205, 206];
return _.includes(http200FamilyCodes, res.status());
};
5 changes: 5 additions & 0 deletions libs/core-scanner/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ export const getTopLevelDomain = (url: string): string | null => {

return tldMatch ? tldMatch[1] : null;
};

export const isLive = (res: HTTPResponse): boolean => {
const http200FamilyCodes = [200, 201, 202, 203, 204, 205, 206];
return _.includes(http200FamilyCodes, res.status());
};

0 comments on commit 5d114dd

Please sign in to comment.