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

Merge pull request #952 from YXL76/master

使用axios代替request
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const request = require('request')
let app let app
before(() => { before(() => {
app = require('./app.js') app = require('./app.js')
global.host = 'http://localhost:' + app.server.address().port global.host = 'http://localhost:' + app.server.address().port
request.debug = false
}) })
after((done) => { after((done) => {
app.server.close(done) app.server.close(done)
......
...@@ -34,7 +34,7 @@ declare module 'NeteaseCloudMusicApi' { ...@@ -34,7 +34,7 @@ declare module 'NeteaseCloudMusicApi' {
export const enum SubAction { export const enum SubAction {
sub = 1, sub = 1,
ubsub = 0, unsub = 0,
} }
export function activate_init_profile( export function activate_init_profile(
......
此差异已折叠。
...@@ -44,8 +44,7 @@ ...@@ -44,8 +44,7 @@
"axios": "^0.20.0", "axios": "^0.20.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-fileupload": "^1.1.9", "express-fileupload": "^1.1.9",
"pac-proxy-agent": "^4.0.0", "pac-proxy-agent": "^4.0.0"
"request": "^2.88.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "14.11.2", "@types/node": "14.11.2",
......
const assert = require('assert') const assert = require('assert')
const request = require('request') const axios = require('axios')
const host = global.host || 'http://localhost:3000' const host = global.host || 'http://localhost:3000'
describe('测试获取歌手专辑列表是否正常', () => { describe('测试获取歌手专辑列表是否正常', () => {
...@@ -8,14 +8,18 @@ describe('测试获取歌手专辑列表是否正常', () => { ...@@ -8,14 +8,18 @@ describe('测试获取歌手专辑列表是否正常', () => {
id: 32311, id: 32311,
} }
request.get({ url: `${host}/album`, qs: qs }, (err, res, body) => { axios
if (!err && res.statusCode == 200) { .get(`${host}/album`, {
body = JSON.parse(body) params: qs,
assert(body.code === 200) })
.then(({ status, data }) => {
if (status == 200) {
assert(data.code === 200)
}
done() done()
} else { })
.catch((err) => {
done(err) done(err)
} })
})
}) })
}) })
const assert = require('assert') const assert = require('assert')
const request = require('request') const axios = require('axios')
const host = global.host || 'http://localhost:3000' const host = global.host || 'http://localhost:3000'
describe('测试获取评论是否正常', () => { describe('测试获取评论是否正常', () => {
...@@ -8,14 +8,18 @@ describe('测试获取评论是否正常', () => { ...@@ -8,14 +8,18 @@ describe('测试获取评论是否正常', () => {
id: 32311, id: 32311,
} }
request.get({ url: `${host}/comment/album`, qs: qs }, (err, res, body) => { axios
if (!err && res.statusCode == 200) { .get(`${host}/comment/album`, {
body = JSON.parse(body) params: qs,
assert(body.code === 200) })
.then(({ status, data }) => {
if (status == 200) {
assert(data.code === 200)
}
done() done()
} else { })
.catch((err) => {
done(err) done(err)
} })
})
}) })
}) })
const assert = require('assert') const assert = require('assert')
const request = require('request') const axios = require('axios')
const host = global.host || 'http://localhost:3000' const host = global.host || 'http://localhost:3000'
console.log('注意: 测试登录需在 test/login.test.js 中填写账号密码!!!') console.log('注意: 测试登录需在 test/login.test.js 中填写账号密码!!!')
...@@ -12,18 +12,21 @@ describe('测试登录是否正常', () => { ...@@ -12,18 +12,21 @@ describe('测试登录是否正常', () => {
password: process.env.NCM_API_TEST_LOGIN_PASSWORD || password || '', password: process.env.NCM_API_TEST_LOGIN_PASSWORD || password || '',
} }
request.get( axios
{ url: `${host}/login/cellphone`, qs: qs }, .get(`${host}/login/cellphone`, {
(err, res, body) => { params: qs,
if (!err && res.statusCode == 200) { })
body = JSON.parse(body) .then(({ status, data }) => {
assert(body.code === 200) if (status == 200) {
console.log('昵称:' + body.profile.nickname) console.log('昵称:' + data.profile.nickname)
assert(data.code === 200)
done() done()
} else { } else {
done('登录错误') done('登录错误')
} }
}, })
) .catch((err) => {
done(err)
})
}) })
}) })
const assert = require('assert') const assert = require('assert')
const request = require('request') const axios = require('axios')
const host = global.host || 'http://localhost:3000' const host = global.host || 'http://localhost:3000'
describe('测试获取歌词是否正常', () => { describe('测试获取歌词是否正常', () => {
...@@ -8,14 +8,18 @@ describe('测试获取歌词是否正常', () => { ...@@ -8,14 +8,18 @@ describe('测试获取歌词是否正常', () => {
id: 347230, id: 347230,
} }
request.get({ url: `${host}/lyric`, qs: qs }, (err, res, body) => { axios
if (!err && res.statusCode == 200) { .get(`${host}/lyric`, {
body = JSON.parse(body) params: qs,
assert(typeof body.lrc !== 'undefined') })
.then(({ status, data }) => {
if (status == 200) {
assert(typeof data.lrc !== 'undefined')
}
done() done()
} else { })
.catch((err) => {
done(err) done(err)
} })
})
}) })
}) })
const assert = require('assert') const assert = require('assert')
const request = require('request') const axios = require('axios')
const host = global.host || 'http://localhost:3000' const host = global.host || 'http://localhost:3000'
describe('测试获取歌曲是否正常', () => { describe('测试获取歌曲是否正常', () => {
...@@ -9,14 +9,18 @@ describe('测试获取歌曲是否正常', () => { ...@@ -9,14 +9,18 @@ describe('测试获取歌曲是否正常', () => {
br: 999000, br: 999000,
} }
request.get({ url: `${host}/song/url`, qs: qs }, (err, res, body) => { axios
if (!err && res.statusCode == 200) { .get(`${host}/song/url`, {
body = JSON.parse(body) params: qs,
assert(!!body.data[0].url) })
.then(({ status, data }) => {
if (status == 200) {
assert(!!data.data[0].url)
}
done() done()
} else { })
.catch((err) => {
done(err) done(err)
} })
})
}) })
}) })
const assert = require('assert') const assert = require('assert')
const request = require('request') const axios = require('axios')
const host = global.host || 'http://localhost:3000' const host = global.host || 'http://localhost:3000'
describe('测试搜索是否正常', () => { describe('测试搜索是否正常', () => {
...@@ -8,14 +8,18 @@ describe('测试搜索是否正常', () => { ...@@ -8,14 +8,18 @@ describe('测试搜索是否正常', () => {
keywords: '海阔天空', keywords: '海阔天空',
type: 1, type: 1,
} }
request.get({ url: `${host}/search`, qs: qs }, (err, res, body) => { axios
if (!err && res.statusCode == 200) { .get(`${host}/search`, {
body = JSON.parse(body) params: qs,
assert(body.result.songs[0].name === '海阔天空') })
.then(({ status, data }) => {
if (status == 200) {
assert(data.result.songs[0].name === '海阔天空')
}
done() done()
} else { })
.catch((err) => {
done(err) done(err)
} })
})
}) })
}) })
const encrypt = require('./crypto') const encrypt = require('./crypto')
const request = require('request') const axios = require('axios')
const queryString = require('querystring') const queryString = require('querystring')
const PacProxyAgent = require('pac-proxy-agent') const PacProxyAgent = require('pac-proxy-agent')
const zlib = require('zlib') const http = require('http')
const https = require('https')
// request.debug = true // 开启可看到更详细信息 // request.debug = true // 开启可看到更详细信息
...@@ -114,66 +115,47 @@ const createRequest = (method, url, data, options) => { ...@@ -114,66 +115,47 @@ const createRequest = (method, url, data, options) => {
method: method, method: method,
url: url, url: url,
headers: headers, 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 (options.crypto === 'eapi') settings.encoding = null
if (/\.pac$/i.test(options.proxy)) { 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 { } else {
settings.proxy = options.proxy settings.proxy = options.proxy
} }
request(settings, (err, res, body) => { axios(settings)
if (err) { .then((res) => {
answer.status = 502 const body = res.data
answer.body = { code: 502, msg: err.stack }
reject(answer)
} else {
answer.cookie = (res.headers['set-cookie'] || []).map((x) => answer.cookie = (res.headers['set-cookie'] || []).map((x) =>
x.replace(/\s*Domain=[^(;|$)]+;*/, ''), x.replace(/\s*Domain=[^(;|$)]+;*/, ''),
) )
try { try {
if (options.crypto === 'eapi') { answer.body = body
zlib.unzip(body, function (err, buffer) { answer.status = answer.body.code || res.status
const _buffer = err ? body : buffer if (answer.body.code === 502) {
try { answer.status = 200
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
}
} }
} catch (e) { } catch (e) {
answer.body = body answer.body = body
answer.status = res.statusCode answer.status = res.status
} }
answer.status = answer.status =
100 < answer.status && answer.status < 600 ? answer.status : 400 100 < answer.status && answer.status < 600 ? answer.status : 400
if (answer.status == 200) resolve(answer) if (answer.status == 200) resolve(answer)
else reject(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.
先完成此消息的编辑!
想要评论请 注册