From 7c5a696850096f6dae979cf010e8c9ba6c321efb Mon Sep 17 00:00:00 2001 From: WhyK Date: Fri, 18 Oct 2024 22:19:59 +0900 Subject: [PATCH] test: adopt postman-collection --- package.json | 4 +- pnpm-lock.yaml | 24 ++++ src/index.newman.ts | 133 +++++++++++++++++- ...y-cfw-drizzle-crud.postman_collection.json | 127 +++++++++-------- 4 files changed, 225 insertions(+), 63 deletions(-) diff --git a/package.json b/package.json index fbf69a2..68933f5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "dev": "wrangler dev src/index.ts", "deploy": "wrangler deploy --minify src/index.ts", "test": "vitest", - "test:api": "tsx src/index.newman.ts", + "test:api": "tsx src/index.newman.ts && pnpm format", "check": "biome check --unsafe .", "format": "pnpm check --write" }, @@ -20,7 +20,9 @@ "@cloudflare/vitest-pool-workers": "^0.5.2", "@cloudflare/workers-types": "^4.20240529.0", "@types/newman": "^5.3.6", + "@types/postman-collection": "^3.5.10", "newman": "^6.2.1", + "postman-collection": "^4.5.0", "tsx": "^4.19.1", "vitest": "^2.0.5", "wrangler": "^3.78.7" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 58b85e8..adddec2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,9 +24,15 @@ importers: '@types/newman': specifier: ^5.3.6 version: 5.3.6 + '@types/postman-collection': + specifier: ^3.5.10 + version: 3.5.10 newman: specifier: ^6.2.1 version: 6.2.1 + postman-collection: + specifier: ^4.5.0 + version: 4.5.0 tsx: specifier: ^4.19.1 version: 4.19.1 @@ -1296,6 +1302,10 @@ packages: resolution: {integrity: sha512-2BGDFcUwlK08CqZFUlIC8kwRJueVzPjZnnokWPtJCd9f2J06HBQpGL7t2P1Ud1NEsK9NHq9wdipUhWLOPj5s/Q==} engines: {node: '>=10'} + postman-collection@4.5.0: + resolution: {integrity: sha512-152JSW9pdbaoJihwjc7Q8lc3nPg/PC9lPTHdMk7SHnHhu/GBJB7b2yb9zG7Qua578+3PxkQ/HYBuXpDSvsf7GQ==} + engines: {node: '>=10'} + postman-request@2.88.1-postman.34: resolution: {integrity: sha512-GkolJ4cIzgamcwHRDkeZc/taFWO1u2HuGNML47K9ZAsFH2LdEkS5Yy8QanpzhjydzV3WWthl9v60J8E7SjKodQ==} engines: {node: '>= 6'} @@ -2741,6 +2751,20 @@ snapshots: semver: 7.5.4 uuid: 8.3.2 + postman-collection@4.5.0: + dependencies: + '@faker-js/faker': 5.5.3 + file-type: 3.9.0 + http-reasons: 0.1.0 + iconv-lite: 0.6.3 + liquid-json: 0.3.1 + lodash: 4.17.21 + mime-format: 2.0.1 + mime-types: 2.1.35 + postman-url-encoder: 3.0.5 + semver: 7.6.3 + uuid: 8.3.2 + postman-request@2.88.1-postman.34: dependencies: '@postman/form-data': 3.1.1 diff --git a/src/index.newman.ts b/src/index.newman.ts index 1c8eca6..ffd3568 100644 --- a/src/index.newman.ts +++ b/src/index.newman.ts @@ -1,6 +1,137 @@ +import { writeFileSync } from "node:fs"; import * as newman from "newman"; +import { Collection } from "postman-collection"; -// TODO: 前段処理でPostman CollectionをTypeScriptで記述して吐き出せるようにしたい(Postman Collection SDKを使う?) +const collection = new Collection({ + name: "verify-cfw-drizzle-crud", + item: [ + { + name: "GET", + event: [ + { + listen: "test", + script: { + exec: [ + 'pm.test("レスポンスボディのJSONが想定通りか", () => {\r', + " const jsonData = pm.response.json();\r", + " pm.expect(jsonData.status).to.eql(200);\r", + ' pm.expect(jsonData.method).to.eql("GET");\r', + ' pm.expect(jsonData.text).to.eql("OK!");\r', + ' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r', + "});", + ], + type: "text/javascript", + }, + }, + ], + request: { + method: "GET", + header: [], + url: { + protocol: "http", + host: ["localhost"], + port: "8787", + }, + }, + response: [], + }, + { + name: "POST", + event: [ + { + listen: "test", + script: { + exec: [ + 'pm.test("レスポンスボディのJSONが想定通りか", () => {\r', + " const jsonData = pm.response.json();\r", + " pm.expect(jsonData.status).to.eql(200);\r", + ' pm.expect(jsonData.method).to.eql("POST");\r', + ' pm.expect(jsonData.text).to.eql("OK!");\r', + ' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r', + "});", + ], + type: "text/javascript", + }, + }, + ], + request: { + method: "POST", + header: [], + url: { + protocol: "http", + host: ["localhost"], + port: "8787", + }, + }, + response: [], + }, + { + name: "PATCH", + event: [ + { + listen: "test", + script: { + exec: [ + 'pm.test("レスポンスボディのJSONが想定通りか", () => {\r', + " const jsonData = pm.response.json();\r", + " pm.expect(jsonData.status).to.eql(200);\r", + ' pm.expect(jsonData.method).to.eql("PATCH");\r', + ' pm.expect(jsonData.text).to.eql("OK!");\r', + ' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r', + "});", + ], + type: "text/javascript", + }, + }, + ], + request: { + method: "PATCH", + header: [], + url: { + protocol: "http", + host: ["localhost"], + port: "8787", + }, + }, + response: [], + }, + { + name: "DELETE", + event: [ + { + listen: "test", + script: { + exec: [ + 'pm.test("レスポンスボディのJSONが想定通りか", () => {\r', + " const jsonData = pm.response.json();\r", + " pm.expect(jsonData.status).to.eql(200);\r", + ' pm.expect(jsonData.method).to.eql("DELETE");\r', + ' pm.expect(jsonData.text).to.eql("OK!");\r', + ' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r', + "});", + ], + type: "text/javascript", + }, + }, + ], + request: { + method: "DELETE", + header: [], + url: { + protocol: "http", + host: ["localhost"], + port: "8787", + }, + }, + response: [], + }, + ], +}); + +writeFileSync( + "src/verify-cfw-drizzle-crud.postman_collection.json", + JSON.stringify(collection.toJSON()), +); newman.run( { diff --git a/src/verify-cfw-drizzle-crud.postman_collection.json b/src/verify-cfw-drizzle-crud.postman_collection.json index 2ede551..1b52723 100644 --- a/src/verify-cfw-drizzle-crud.postman_collection.json +++ b/src/verify-cfw-drizzle-crud.postman_collection.json @@ -1,17 +1,25 @@ { - "info": { - "_postman_id": "7e2a7dbb-8093-4fb9-b808-3cfcb0402670", - "name": "verify-cfw-drizzle-crud", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", - "_exporter_id": "11022225" - }, "item": [ { + "id": "dcef15a7-796f-453f-bc84-1d6fb8c0842a", "name": "GET", + "request": { + "url": { + "protocol": "http", + "port": "8787", + "host": ["localhost"], + "query": [], + "variable": [] + }, + "method": "GET" + }, + "response": [], "event": [ { "listen": "test", "script": { + "id": "27d74a00-02ec-462f-a26c-412f6efe7338", + "type": "text/javascript", "exec": [ "pm.test(\"レスポンスボディのJSONが想定通りか\", () => {\r", " const jsonData = pm.response.json();\r", @@ -20,30 +28,31 @@ " pm.expect(jsonData.text).to.eql(\"OK!\");\r", " pm.expect(jsonData.apiKey).to.eql(\"Example_0000\");\r", "});" - ], - "type": "text/javascript", - "packages": {} + ] } } - ], + ] + }, + { + "id": "6f0948e1-2c63-4237-af46-9fbf67fba696", + "name": "POST", "request": { - "method": "GET", - "header": [], "url": { - "raw": "http://localhost:8787", "protocol": "http", + "port": "8787", "host": ["localhost"], - "port": "8787" - } + "query": [], + "variable": [] + }, + "method": "POST" }, - "response": [] - }, - { - "name": "POST", + "response": [], "event": [ { "listen": "test", "script": { + "id": "daefa856-2e90-4126-93b3-943bdbcb59e8", + "type": "text/javascript", "exec": [ "pm.test(\"レスポンスボディのJSONが想定通りか\", () => {\r", " const jsonData = pm.response.json();\r", @@ -52,30 +61,31 @@ " pm.expect(jsonData.text).to.eql(\"OK!\");\r", " pm.expect(jsonData.apiKey).to.eql(\"Example_0000\");\r", "});" - ], - "type": "text/javascript", - "packages": {} + ] } } - ], + ] + }, + { + "id": "86f7873b-bede-45c0-a54b-663d960690d9", + "name": "PATCH", "request": { - "method": "POST", - "header": [], "url": { - "raw": "http://localhost:8787", "protocol": "http", + "port": "8787", "host": ["localhost"], - "port": "8787" - } + "query": [], + "variable": [] + }, + "method": "PATCH" }, - "response": [] - }, - { - "name": "PATCH", + "response": [], "event": [ { "listen": "test", "script": { + "id": "0d2a2d93-80fa-486e-bb77-ba575bc0281c", + "type": "text/javascript", "exec": [ "pm.test(\"レスポンスボディのJSONが想定通りか\", () => {\r", " const jsonData = pm.response.json();\r", @@ -84,30 +94,31 @@ " pm.expect(jsonData.text).to.eql(\"OK!\");\r", " pm.expect(jsonData.apiKey).to.eql(\"Example_0000\");\r", "});" - ], - "type": "text/javascript", - "packages": {} + ] } } - ], + ] + }, + { + "id": "28a9a396-7282-4bf3-a16d-93b2f3dfdcd1", + "name": "DELETE", "request": { - "method": "PATCH", - "header": [], "url": { - "raw": "http://localhost:8787", "protocol": "http", + "port": "8787", "host": ["localhost"], - "port": "8787" - } + "query": [], + "variable": [] + }, + "method": "DELETE" }, - "response": [] - }, - { - "name": "DELETE", + "response": [], "event": [ { "listen": "test", "script": { + "id": "0d7f70d0-abfa-430b-9382-7771294a40fc", + "type": "text/javascript", "exec": [ "pm.test(\"レスポンスボディのJSONが想定通りか\", () => {\r", " const jsonData = pm.response.json();\r", @@ -116,23 +127,17 @@ " pm.expect(jsonData.text).to.eql(\"OK!\");\r", " pm.expect(jsonData.apiKey).to.eql(\"Example_0000\");\r", "});" - ], - "type": "text/javascript", - "packages": {} + ] } } - ], - "request": { - "method": "DELETE", - "header": [], - "url": { - "raw": "http://localhost:8787", - "protocol": "http", - "host": ["localhost"], - "port": "8787" - } - }, - "response": [] + ] } - ] + ], + "event": [], + "variable": [], + "info": { + "_postman_id": "eb4c7377-ebfb-4ef1-82a7-81af732ffd67", + "name": "verify-cfw-drizzle-crud", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + } }