未验证 提交 8ad27ce3 编写于 作者: B binaryify 提交者: GitHub

Merge pull request #952 from YXL76/master

使用axios代替request
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)
......
......@@ -34,7 +34,7 @@ declare module 'NeteaseCloudMusicApi' {
export const enum SubAction {
sub = 1,
ubsub = 0,
unsub = 0,
}
export function activate_init_profile(
......
此差异已折叠。
......@@ -44,8 +44,7 @@
"axios": "^0.20.0",
"express": "^4.17.1",
"express-fileupload": "^1.1.9",
"pac-proxy-agent": "^4.0.0",
"request": "^2.88.0"
"pac-proxy-agent": "^4.0.0"
},
"devDependencies": {
"@types/node": "14.11.2",
......
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)
}
})
})
})
})
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)
}
})
})
})
})
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,21 @@ 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('登录错误')
}
},
)
})
.catch((err) => {
done(err)
})
})
})
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)
}
})
})
})
})
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)
}
})
})
})
})
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)
}
})
})
})
})
const encrypt = require('./crypto')
const request = require('request')
const axios = require('axios')
const queryString = require('querystring')
const PacProxyAgent = require('pac-proxy-agent')
const zlib = require('zlib')
const http = require('http')
const https = require('https')
// request.debug = true // 开启可看到更详细信息
......@@ -114,66 +115,47 @@ const createRequest = (method, url, data, options) => {
method: method,
url: url,
headers: headers,
body: queryString.stringify(data),
data: queryString.stringify(data),
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
}
if (options.crypto === 'eapi') settings.encoding = null
if (/\.pac$/i.test(options.proxy)) {
settings.agent = new PacProxyAgent(options.proxy)
settings.httpAgent = new PacProxyAgent(options.proxy)
settings.httpsAgent = new PacProxyAgent(options.proxy)
} else {
settings.proxy = options.proxy
}
request(settings, (err, res, body) => {
if (err) {
answer.status = 502
answer.body = { code: 502, msg: err.stack }
reject(answer)
} else {
axios(settings)
.then((res) => {
const body = res.data
answer.cookie = (res.headers['set-cookie'] || []).map((x) =>
x.replace(/\s*Domain=[^(;|$)]+;*/, ''),
)
try {
if (options.crypto === 'eapi') {
zlib.unzip(body, function (err, buffer) {
const _buffer = err ? body : buffer
try {
try {
answer.body = JSON.parse(encrypt.decrypt(_buffer).toString())
answer.status = answer.body.code || res.statusCode
} catch (e) {
answer.body = JSON.parse(_buffer.toString())
answer.status = res.statusCode
}
} catch (e) {
answer.body = _buffer.toString()
answer.status = res.statusCode
}
answer.status =
100 < answer.status && answer.status < 600 ? answer.status : 400
if (answer.status === 200) resolve(answer)
else reject(answer)
})
return false
} else {
answer.body = JSON.parse(body)
answer.status = answer.body.code || res.statusCode
if (answer.body.code === 502) {
answer.status = 200
}
answer.body = body
answer.status = answer.body.code || res.status
if (answer.body.code === 502) {
answer.status = 200
}
} catch (e) {
answer.body = body
answer.status = res.statusCode
answer.status = res.status
}
answer.status =
100 < answer.status && answer.status < 600 ? answer.status : 400
if (answer.status == 200) resolve(answer)
else reject(answer)
}
})
})
.catch((err) => {
answer.status = 502
answer.body = { code: 502, msg: err }
reject(answer)
})
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册