From 57e5e0f5e57e7e61cc856a771062db306dd72bf2 Mon Sep 17 00:00:00 2001 From: YXL Date: Thu, 1 Oct 2020 23:23:18 +0800 Subject: [PATCH] test: use axios --- app.test.js | 2 -- test/album.test.js | 20 ++++++++++++-------- test/comment.test.js | 20 ++++++++++++-------- test/login.test.js | 26 ++++++++++++++------------ test/lyric.test.js | 20 ++++++++++++-------- test/music_url.test.js | 20 ++++++++++++-------- test/search.test.js | 20 ++++++++++++-------- 7 files changed, 74 insertions(+), 54 deletions(-) diff --git a/app.test.js b/app.test.js index 2a9e22a..94846b2 100644 --- a/app.test.js +++ b/app.test.js @@ -1,12 +1,10 @@ const fs = require('fs') const path = require('path') -const request = require('request') let app before(() => { app = require('./app.js') global.host = 'http://localhost:' + app.server.address().port - request.debug = false }) after((done) => { app.server.close(done) diff --git a/test/album.test.js b/test/album.test.js index 4709d85..2a6a1b9 100644 --- a/test/album.test.js +++ b/test/album.test.js @@ -1,5 +1,5 @@ const assert = require('assert') -const request = require('request') +const axios = require('axios') const host = global.host || 'http://localhost:3000' describe('测试获取歌手专辑列表是否正常', () => { @@ -8,14 +8,18 @@ describe('测试获取歌手专辑列表是否正常', () => { id: 32311, } - request.get({ url: `${host}/album`, qs: qs }, (err, res, body) => { - if (!err && res.statusCode == 200) { - body = JSON.parse(body) - assert(body.code === 200) + axios + .get(`${host}/album`, { + params: qs, + }) + .then(({ status, data }) => { + if (status == 200) { + assert(data.code === 200) + } done() - } else { + }) + .catch((err) => { done(err) - } - }) + }) }) }) diff --git a/test/comment.test.js b/test/comment.test.js index 1637068..612f670 100644 --- a/test/comment.test.js +++ b/test/comment.test.js @@ -1,5 +1,5 @@ const assert = require('assert') -const request = require('request') +const axios = require('axios') const host = global.host || 'http://localhost:3000' describe('测试获取评论是否正常', () => { @@ -8,14 +8,18 @@ describe('测试获取评论是否正常', () => { id: 32311, } - request.get({ url: `${host}/comment/album`, qs: qs }, (err, res, body) => { - if (!err && res.statusCode == 200) { - body = JSON.parse(body) - assert(body.code === 200) + axios + .get(`${host}/comment/album`, { + params: qs, + }) + .then(({ status, data }) => { + if (status == 200) { + assert(data.code === 200) + } done() - } else { + }) + .catch((err) => { done(err) - } - }) + }) }) }) diff --git a/test/login.test.js b/test/login.test.js index d1c3109..6e13a62 100644 --- a/test/login.test.js +++ b/test/login.test.js @@ -1,5 +1,5 @@ const assert = require('assert') -const request = require('request') +const axios = require('axios') const host = global.host || 'http://localhost:3000' console.log('注意: 测试登录需在 test/login.test.js 中填写账号密码!!!') @@ -12,18 +12,20 @@ describe('测试登录是否正常', () => { password: process.env.NCM_API_TEST_LOGIN_PASSWORD || password || '', } - request.get( - { url: `${host}/login/cellphone`, qs: qs }, - (err, res, body) => { - if (!err && res.statusCode == 200) { - body = JSON.parse(body) - assert(body.code === 200) - console.log('昵称:' + body.profile.nickname) + axios + .get(`${host}/login/cellphone`, { + params: qs, + }) + .then(({ status, data }) => { + if (status == 200) { + console.log('昵称:' + data.profile.nickname) + assert(data.code === 200) done() - } else { - done('登录错误') } - }, - ) + done('登录错误') + }) + .catch((err) => { + done(err) + }) }) }) diff --git a/test/lyric.test.js b/test/lyric.test.js index 7d3451b..41886e4 100644 --- a/test/lyric.test.js +++ b/test/lyric.test.js @@ -1,5 +1,5 @@ const assert = require('assert') -const request = require('request') +const axios = require('axios') const host = global.host || 'http://localhost:3000' describe('测试获取歌词是否正常', () => { @@ -8,14 +8,18 @@ describe('测试获取歌词是否正常', () => { id: 347230, } - request.get({ url: `${host}/lyric`, qs: qs }, (err, res, body) => { - if (!err && res.statusCode == 200) { - body = JSON.parse(body) - assert(typeof body.lrc !== 'undefined') + axios + .get(`${host}/lyric`, { + params: qs, + }) + .then(({ status, data }) => { + if (status == 200) { + assert(typeof data.lrc !== 'undefined') + } done() - } else { + }) + .catch((err) => { done(err) - } - }) + }) }) }) diff --git a/test/music_url.test.js b/test/music_url.test.js index 8661ac8..cf7777c 100644 --- a/test/music_url.test.js +++ b/test/music_url.test.js @@ -1,5 +1,5 @@ const assert = require('assert') -const request = require('request') +const axios = require('axios') const host = global.host || 'http://localhost:3000' describe('测试获取歌曲是否正常', () => { @@ -9,14 +9,18 @@ describe('测试获取歌曲是否正常', () => { br: 999000, } - request.get({ url: `${host}/song/url`, qs: qs }, (err, res, body) => { - if (!err && res.statusCode == 200) { - body = JSON.parse(body) - assert(!!body.data[0].url) + axios + .get(`${host}/song/url`, { + params: qs, + }) + .then(({ status, data }) => { + if (status == 200) { + assert(!!data.data[0].url) + } done() - } else { + }) + .catch((err) => { done(err) - } - }) + }) }) }) diff --git a/test/search.test.js b/test/search.test.js index 89c62e1..e80dea5 100644 --- a/test/search.test.js +++ b/test/search.test.js @@ -1,5 +1,5 @@ const assert = require('assert') -const request = require('request') +const axios = require('axios') const host = global.host || 'http://localhost:3000' describe('测试搜索是否正常', () => { @@ -8,14 +8,18 @@ describe('测试搜索是否正常', () => { keywords: '海阔天空', type: 1, } - request.get({ url: `${host}/search`, qs: qs }, (err, res, body) => { - if (!err && res.statusCode == 200) { - body = JSON.parse(body) - assert(body.result.songs[0].name === '海阔天空') + axios + .get(`${host}/search`, { + params: qs, + }) + .then(({ status, data }) => { + if (status == 200) { + assert(data.result.songs[0].name === '海阔天空') + } done() - } else { + }) + .catch((err) => { done(err) - } - }) + }) }) })