diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2020-04-09 23:42:23 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-09 23:42:23 +0900 |
| commit | d3c0f3c251e8371d78d953f32f7311a38f4a1bdb (patch) | |
| tree | ef746ec79b4cc53ad15da2680b2e5d6280d39867 /test | |
| parent | Create aiscript.ja-JP.md (diff) | |
| download | sharkey-d3c0f3c251e8371d78d953f32f7311a38f4a1bdb.tar.gz sharkey-d3c0f3c251e8371d78d953f32f7311a38f4a1bdb.tar.bz2 sharkey-d3c0f3c251e8371d78d953f32f7311a38f4a1bdb.zip | |
Use node-fetch instead of request (#6228)
* requestをnode-fetchになど
* format
* fix error
* t
* Fix test
Diffstat (limited to 'test')
| -rw-r--r-- | test/utils.ts | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/test/utils.ts b/test/utils.ts index af190254ec..066bd33a56 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,7 +1,7 @@ import * as fs from 'fs'; import * as WebSocket from 'ws'; -const fetch = require('node-fetch'); -import * as req from 'request'; +import fetch from 'node-fetch'; +const FormData = require('form-data'); import * as childProcess from 'child_process'; export const async = (fn: Function) => (done: Function) => { @@ -20,6 +20,9 @@ export const request = async (endpoint: string, params: any, me?: any): Promise< try { const res = await fetch('http://localhost:8080/api' + endpoint, { method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, body: JSON.stringify(Object.assign(auth, params)) }); @@ -64,18 +67,23 @@ export const react = async (user: any, note: any, reaction: string): Promise<any }, user); }; -export const uploadFile = (user: any, path?: string): Promise<any> => new Promise((ok, rej) => { - req.post({ - url: 'http://localhost:8080/api/drive/files/create', - formData: { - i: user.token, - file: fs.createReadStream(path || __dirname + '/resources/Lenna.png') - }, - json: true - }, (err, httpResponse, body) => { - ok(body); - }); -}); +export const uploadFile = (user: any, path?: string): Promise<any> => { + const formData = new FormData(); + formData.append('i', user.token); + formData.append('file', fs.createReadStream(path || __dirname + '/resources/Lenna.png')); + + return fetch('http://localhost:8080/api/drive/files/create', { + method: 'post', + body: formData, + timeout: 30 * 1000, + }).then(res => { + if (!res.ok) { + throw `${res.status} ${res.statusText}`; + } else { + return res.json(); + } + }); +}; export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> { return new Promise((res, rej) => { |