From 936fcfb1c7cee448b8e5036205aaf8843f8d8171 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Fri, 12 Mar 2021 23:10:15 +0900 Subject: リソースが取得できるかどうかのテストなど (#7316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Tests for Fetch resource * Remove unnecessary copy * Update koa-views, pug --- test/fetch-resource.ts | 170 +++++++++++++++++++++++++++++++++++++++++++++++++ test/utils.ts | 63 ++++++++++++------ 2 files changed, 212 insertions(+), 21 deletions(-) create mode 100644 test/fetch-resource.ts (limited to 'test') diff --git a/test/fetch-resource.ts b/test/fetch-resource.ts new file mode 100644 index 0000000000..72ef133ef8 --- /dev/null +++ b/test/fetch-resource.ts @@ -0,0 +1,170 @@ +/* + * Tests for Fetch resource + * + * How to run the tests: + * > TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true npx mocha test/fetch-resource.ts --require ts-node/register + * + * To specify test: + * > TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true npx mocha test/fetch-resource.ts --require ts-node/register -g 'test name' + */ + +process.env.NODE_ENV = 'test'; + +import * as assert from 'assert'; +import * as childProcess from 'child_process'; +import { async, launchServer, signup, post, request, simpleGet } from './utils'; + +// Request Accept +const ONLY_AP = 'application/activity+json'; +const PREFER_AP = 'application/activity+json, */*'; +const PREFER_HTML = 'text/html, */*'; +const UNSPECIFIED = '*/*'; + +// Response Contet-Type +const AP = 'application/activity+json; charset=utf-8'; +const HTML = 'text/html; charset=utf-8'; + +describe('Fetch resource', () => { + let p: childProcess.ChildProcess; + + let alice: any; + let alicesPost: any; + + before(launchServer(g => p = g, async () => { + alice = await signup({ username: 'alice' }); + alicesPost = await post(alice, { + text: 'test' + }); + })); + + after(() => { + p.kill(); + }); + + describe('Common', () => { + it('meta', async(async () => { + const res = await request('/meta', { + }); + + assert.strictEqual(res.status, 200); + })); + + it('GET root', async(async () => { + const res = await simpleGet('/', 'text/html'); + assert.strictEqual(res.status, 200); + })); + + it('GET docs', async(async () => { + const res = await simpleGet('/docs/ja-JP/about', 'text/html'); + assert.strictEqual(res.status, 200); + })); + + it('GET api-doc', async(async () => { + const res = await simpleGet('/api-doc', 'text/html'); + assert.strictEqual(res.status, 200); + })); + + it('GET api.json', async(async () => { + const res = await simpleGet('/api.json', 'application/json'); + assert.strictEqual(res.status, 200); + })); + }); + + describe('/@:username', () => { + it('Only AP => AP', async(async () => { + const res = await simpleGet(`/@${alice.username}`, ONLY_AP); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, AP); + })); + + it('Prefer AP => AP', async(async () => { + const res = await simpleGet(`/@${alice.username}`, PREFER_AP); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, AP); + })); + + it('Prefer HTML => HTML', async(async () => { + const res = await simpleGet(`/@${alice.username}`, PREFER_HTML); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, HTML); + })); + + it('Unspecified => HTML', async(async () => { + const res = await simpleGet(`/@${alice.username}`, UNSPECIFIED); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, HTML); + })); + }); + + describe('/users/:id', () => { + it('Only AP => AP', async(async () => { + const res = await simpleGet(`/users/${alice.id}`, ONLY_AP); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, AP); + })); + + it('Prefer AP => AP', async(async () => { + const res = await simpleGet(`/users/${alice.id}`, PREFER_AP); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, AP); + })); + + it('Prefer HTML => Redirect to /@:username', async(async () => { + const res = await simpleGet(`/users/${alice.id}`, PREFER_HTML); + assert.strictEqual(res.status, 302); + assert.strictEqual(res.location, `/@${alice.username}`); + })); + + it('Undecided => HTML', async(async () => { + const res = await simpleGet(`/users/${alice.id}`, UNSPECIFIED); + assert.strictEqual(res.status, 302); + assert.strictEqual(res.location, `/@${alice.username}`); + })); + }); + + describe('/notes/:id', () => { + it('Only AP => AP', async(async () => { + const res = await simpleGet(`/notes/${alicesPost.id}`, ONLY_AP); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, AP); + })); + + it('Prefer AP => AP', async(async () => { + const res = await simpleGet(`/notes/${alicesPost.id}`, PREFER_AP); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, AP); + })); + + it('Prefer HTML => HTML', async(async () => { + const res = await simpleGet(`/notes/${alicesPost.id}`, PREFER_HTML); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, HTML); + })); + + it('Unspecified => HTML', async(async () => { + const res = await simpleGet(`/notes/${alicesPost.id}`, UNSPECIFIED); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, HTML); + })); + }); + + describe('Feeds', () => { + it('RSS', async(async () => { + const res = await simpleGet(`/@${alice.username}.rss`, UNSPECIFIED); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, 'application/rss+xml; charset=utf-8'); + })); + + it('ATOM', async(async () => { + const res = await simpleGet(`/@${alice.username}.atom`, UNSPECIFIED); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, 'application/atom+xml; charset=utf-8'); + })); + + it('JSON', async(async () => { + const res = await simpleGet(`/@${alice.username}.json`, UNSPECIFIED); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.type, 'application/json; charset=utf-8'); + })); + }); +}); diff --git a/test/utils.ts b/test/utils.ts index 066bd33a56..55e877a8a2 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -3,6 +3,10 @@ import * as WebSocket from 'ws'; import fetch from 'node-fetch'; const FormData = require('form-data'); import * as childProcess from 'child_process'; +import * as http from 'http'; +import loadConfig from '../src/config/load'; + +const port = loadConfig().port; export const async = (fn: Function) => (done: Function) => { fn().then(() => { @@ -17,26 +21,20 @@ export const request = async (endpoint: string, params: any, me?: any): Promise< i: me.token } : {}; - try { - const res = await fetch('http://localhost:8080/api' + endpoint, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(Object.assign(auth, params)) - }); + const res = await fetch(`http://localhost:${port}/api${endpoint}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(Object.assign(auth, params)) + }); - const status = res.status; - const body = res.status !== 204 ? await res.json().catch() : null; - - return { - body, status - }; - } catch (e) { - return { - body: null, status: 500 - }; - } + const status = res.status; + const body = res.status !== 204 ? await res.json().catch() : null; + + return { + body, status + }; }; export const signup = async (params?: any): Promise => { @@ -72,7 +70,7 @@ export const uploadFile = (user: any, path?: string): Promise => { formData.append('i', user.token); formData.append('file', fs.createReadStream(path || __dirname + '/resources/Lenna.png')); - return fetch('http://localhost:8080/api/drive/files/create', { + return fetch(`http://localhost:${port}/api/drive/files/create`, { method: 'post', body: formData, timeout: 30 * 1000, @@ -87,7 +85,7 @@ export const uploadFile = (user: any, path?: string): Promise => { export function connectStream(user: any, channel: string, listener: (message: Record) => any, params?: any): Promise { return new Promise((res, rej) => { - const ws = new WebSocket(`ws://localhost:8080/streaming?i=${user.token}`); + const ws = new WebSocket(`ws://localhost:${port}/streaming?i=${user.token}`); ws.on('open', () => { ws.on('message', data => { @@ -112,6 +110,29 @@ export function connectStream(user: any, channel: string, listener: (message: Re }); } +export const simpleGet = async (path: string, accept: string): Promise<{ status?: number, type?: string, location?: string }> => { + // node-fetchだと3xxを取れない + return await new Promise((resolve, reject) => { + const req = http.request(`http://localhost:${port}${path}`, { + headers: { + Accept: accept + } + }, res => { + if (res.statusCode! >= 400) { + reject(res); + } else { + resolve({ + status: res.statusCode, + type: res.headers['content-type'], + location: res.headers.location, + }); + } + }); + + req.end(); + }); +}; + export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise = async () => {}) { return (done: (err?: Error) => any) => { const p = childProcess.spawn('node', [__dirname + '/../index.js'], { -- cgit v1.2.3-freya