Skip to content

Commit

Permalink
Merge pull request #205 from bhollis/tests
Browse files Browse the repository at this point in the history
Add tests for content type, github workflow
  • Loading branch information
bhollis authored Jul 7, 2023
2 parents 35c1f4c + 1188c3e commit 395e606
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/bin/bash -e

rm -rf ts-out

tsc

node --test

rm -rf build
mkdir -p build

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions src/content-type.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
}
9 changes: 9 additions & 0 deletions src/content-type.ts
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 395e606

Please sign in to comment.