diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2021-03-12 23:10:15 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-12 23:10:15 +0900 |
| commit | 936fcfb1c7cee448b8e5036205aaf8843f8d8171 (patch) | |
| tree | 5e8f7cb15f3064c60b2755b884e81f2226d6e5f2 /test/utils.ts | |
| parent | Merge pull request #7324 from syuilo/dependabot/npm_and_yarn/types/koa-2.13.1 (diff) | |
| download | sharkey-936fcfb1c7cee448b8e5036205aaf8843f8d8171.tar.gz sharkey-936fcfb1c7cee448b8e5036205aaf8843f8d8171.tar.bz2 sharkey-936fcfb1c7cee448b8e5036205aaf8843f8d8171.zip | |
リソースが取得できるかどうかのテストなど (#7316)
* Tests for Fetch resource
* Remove unnecessary copy
* Update koa-views, pug
Diffstat (limited to 'test/utils.ts')
| -rw-r--r-- | test/utils.ts | 61 |
1 files changed, 41 insertions, 20 deletions
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; + const status = res.status; + const body = res.status !== 204 ? await res.json().catch() : null; - return { - body, status - }; - } catch (e) { - return { - body: null, status: 500 - }; - } + return { + body, status + }; }; export const signup = async (params?: any): Promise<any> => { @@ -72,7 +70,7 @@ export const uploadFile = (user: any, path?: string): Promise<any> => { 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<any> => { export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> { 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<void> = async () => {}) { return (done: (err?: Error) => any) => { const p = childProcess.spawn('node', [__dirname + '/../index.js'], { |