From 1188c3eb61c04d72b431596a0f4e891143192fc3 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 7 Jul 2023 12:40:32 -0700 Subject: [PATCH] Add tests for content type, github workflow --- .github/workflows/pr-build.yml | 26 ++++++++++++++++++++++++++ build.sh | 4 ++++ package.json | 2 ++ src/background.ts | 6 ++---- src/content-type.test.ts | 30 ++++++++++++++++++++++++++++++ src/content-type.ts | 9 +++++++++ yarn.lock | 5 +++++ 7 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/pr-build.yml create mode 100644 src/content-type.test.ts create mode 100644 src/content-type.ts diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml new file mode 100644 index 0000000..901d2ec --- /dev/null +++ b/.github/workflows/pr-build.yml @@ -0,0 +1,26 @@ +name: PR Build + +on: + pull_request: + types: [opened, synchronize, reopened] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: 20.x + cache: yarn + + - name: Install + run: yarn install --frozen-lockfile --prefer-offline + + - name: Lint + run: yarn lint + + - name: Build + run: yarn start diff --git a/build.sh b/build.sh index 4cf0771..b53adaa 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,11 @@ #!/bin/bash -e +rm -rf ts-out + tsc +node --test + rm -rf build mkdir -p build diff --git a/package.json b/package.json index 88bc97a..882ebc6 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "author": "Benjamin Hollis", "repository": "https://github.com/bhollis/jsonview", "license": "MIT", + "type": "module", "scripts": { "start": "./build.sh", "startWindows": "Powershell.exe -File ./buildWindows.ps1", @@ -115,6 +116,7 @@ }, "devDependencies": { "@types/chrome": "^0.0.235", + "@types/node": "^20.4.0", "@typescript-eslint/eslint-plugin": "^5.59.2", "@typescript-eslint/parser": "^5.59.2", "eslint": "^8.39.0", diff --git a/src/background.ts b/src/background.ts index e7d5f9b..0b2a009 100644 --- a/src/background.ts +++ b/src/background.ts @@ -6,9 +6,7 @@ * content script reformats the page. */ -// Look for JSON if the content type is "application/json", -// or "application/whatever+json" or "application/json; charset=utf-8" -const jsonContentType = /^application\/([\w!#$&.\-^+]+\+)?json($|;)/; +import { isJSONContentType } from "./content-type"; // Keep track globally of URLs that contain JSON content. const jsonUrls = new Set(); @@ -49,7 +47,7 @@ function detectJSON(event: chrome.webRequest.WebResponseHeadersDetails) { if ( header.name.toLowerCase() === "content-type" && header.value && - jsonContentType.test(header.value) + isJSONContentType(header.value) ) { jsonUrls.add(event.url); if (typeof browser !== "undefined" && "filterResponseData" in browser.webRequest) { diff --git a/src/content-type.test.ts b/src/content-type.test.ts new file mode 100644 index 0000000..ef36753 --- /dev/null +++ b/src/content-type.test.ts @@ -0,0 +1,30 @@ +import { strict as assert } from "node:assert"; +import test from "node:test"; +import { isJSONContentType } from "./content-type.js"; + +const jsonContentTypes = [ + "application/json", + "application/hal+json", + "application/jrd+json", + "application/ld+json", + "application/vnd.collection+json", + "application/vnd.XYZ-v1+json", + "application/json; charset=utf-8", + "application/json; charset=utf-8", + "application/hal+json; charset=utf-8", + "application/vnd.collection+json; charset=utf-8", + "application/json; charset=windows-1252", +]; +const notJsonContentTypes = ["text/html", "text/json", "text/text", ""]; + +for (const contentType of jsonContentTypes) { + test(`correctly identifies ${contentType} as JSON`, () => { + assert.ok(isJSONContentType(contentType)); + }); +} + +for (const contentType of notJsonContentTypes) { + test(`correctly identifies ${contentType} as NOT JSON`, () => { + assert.ok(!isJSONContentType(contentType)); + }); +} diff --git a/src/content-type.ts b/src/content-type.ts new file mode 100644 index 0000000..5fd5cfd --- /dev/null +++ b/src/content-type.ts @@ -0,0 +1,9 @@ +const jsonContentType = /^application\/([\w!#$&.\-^+]+\+)?json($|;)/; + +/** + * Look for JSON if the content type is "application/json", + * or "application/whatever+json" or "application/json; charset=utf-8" + */ +export function isJSONContentType(contentType: string) { + return jsonContentType.test(contentType); +} diff --git a/yarn.lock b/yarn.lock index 5273cbe..cebf5b7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -121,6 +121,11 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== +"@types/node@^20.4.0": + version "20.4.0" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.4.0.tgz#01d637d1891e419bc85763b46f42809cd2d5addb" + integrity sha512-jfT7iTf/4kOQ9S7CHV9BIyRaQqHu67mOjsIQBC3BKZvzvUB6zLxEwJ6sBE3ozcvP8kF6Uk5PXN0Q+c0dfhGX0g== + "@types/semver@^7.3.12": version "7.3.13" resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91"