Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ds147000
/
react-native-project-storage
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 276f0a77
authored
Jun 02, 2020
by
GGbong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
集成mock模拟测试
1 parent
47eff67f
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
128 additions
and
36 deletions
mock/index.js
mock/router/index.js
mock/router/module/user.js
package-lock.json
package.json
src/config/app.js
src/libs/fetch.js
src/libs/http-request.js
mock/index.js
0 → 100644
View file @
276f0a7
const
http
=
require
(
'http'
)
const
{
APP
}
=
require
(
'../src/config/app'
)
const
routes
=
require
(
'./router'
)
const
axios
=
require
(
'axios'
)
const
publicHeaders
=
[
{
'access-control-allow-origin'
:
'*'
,
'access-control-allow-methods'
:
'*'
,
'access-control-allow-headers'
:
'*'
,
'access-control-request-headers'
:
'*'
,
'access-control-expose-headers'
:
'*'
}
]
const
server
=
http
.
createServer
(
async
(
req
,
res
)
=>
{
let
payload
=
[]
req
.
on
(
'data'
,
chunk
=>
payload
.
push
(
chunk
))
req
.
on
(
'end'
,
async
()
=>
{
payload
=
Buffer
.
concat
(
payload
).
toString
(
'utf8'
)
payload
=
JSON
.
parse
(
payload
||
'{}'
)
const
index
=
routes
.
findIndex
(
e
=>
new
RegExp
(
e
.
url
).
test
(
req
.
url
)
&&
e
.
method
.
toUpperCase
()
===
req
.
method
.
toUpperCase
()
)
// 访问远程服务器
if
(
index
===
-
1
)
{
const
{
status
,
headers
,
data
}
=
await
request
({...
req
,
data
:
payload
})
res
.
statusCode
=
status
Object
.
keys
(
headers
).
forEach
(
key
=>
res
.
setHeader
(
key
,
headers
[
key
]))
res
.
write
(
handleData
(
data
))
res
.
end
()
}
else
{
res
.
statusCode
=
200
Object
.
keys
(
publicHeaders
).
forEach
(
key
=>
res
.
setHeader
(
key
,
publicHeaders
[
key
]))
const
data
=
routes
[
index
].
controller
(
req
)
res
.
write
(
handleData
(
data
))
res
.
end
()
}
})
})
const
request
=
async
({
url
,
method
,
headers
,
data
})
=>
{
url
=
APP
.
baseUrl
.
proxy
+
url
headers
=
{...
headers
}
if
(
headers
.
host
)
delete
headers
.
host
return
axios
({
url
,
method
,
headers
,
data
})
.
then
(
res
=>
res
)
.
catch
(
err
=>
Promise
.
resolve
(
err
.
response
)
)
}
const
handleData
=
data
=>
{
if
(
typeof
data
===
'object'
)
return
JSON
.
stringify
(
data
)
else
return
data
}
server
.
listen
(
APP
.
baseUrl
.
dev
.
prot
,
APP
.
baseUrl
.
dev
.
host
,
()
=>
{
console
.
log
(
'mock启动成功'
)
})
mock/router/index.js
0 → 100644
View file @
276f0a7
const
user
=
require
(
'./module/user'
)
module
.
exports
=
[
...
user
,
]
\ No newline at end of file
mock/router/module/user.js
0 → 100644
View file @
276f0a7
module
.
exports
=
[
{
url
:
/url/
,
method
:
'get'
,
controller
:
req
=>
{
return
{
code
:
200
,
data
:
[
1
,
2
,
3
,
4
]
}
}
}
]
\ No newline at end of file
package-lock.json
View file @
276f0a7
{
{
"name"
:
"
rn_basis
"
,
"name"
:
"
xbd-app
"
,
"version"
:
"0.0.1"
,
"version"
:
"0.0.1"
,
"lockfileVersion"
:
1
,
"lockfileVersion"
:
1
,
"requires"
:
true
,
"requires"
:
true
,
...
@@ -3980,16 +3980,6 @@
...
@@ -3980,16 +3980,6 @@
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/fs.realpath/-/fs.realpath-1.0.0.tgz"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/fs.realpath/-/fs.realpath-1.0.0.tgz"
,
"integrity"
:
"sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
"integrity"
:
"sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
},
},
"fsevents"
:
{
"version"
:
"1.2.13"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/fsevents/-/fsevents-1.2.13.tgz"
,
"integrity"
:
"sha1-8yXLBFVZJCi88Rs4M3DvcOO/zDg="
,
"optional"
:
true
,
"requires"
:
{
"bindings"
:
"^1.5.0"
,
"nan"
:
"^2.12.1"
}
},
"function-bind"
:
{
"function-bind"
:
{
"version"
:
"1.1.1"
,
"version"
:
"1.1.1"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/function-bind/-/function-bind-1.1.1.tgz"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/function-bind/-/function-bind-1.1.1.tgz"
,
...
@@ -5094,6 +5084,18 @@
...
@@ -5094,6 +5084,18 @@
"micromatch"
:
"^3.1.10"
,
"micromatch"
:
"^3.1.10"
,
"sane"
:
"^4.0.3"
,
"sane"
:
"^4.0.3"
,
"walker"
:
"^1.0.7"
"walker"
:
"^1.0.7"
},
"dependencies"
:
{
"fsevents"
:
{
"version"
:
"1.2.13"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/fsevents/-/fsevents-1.2.13.tgz"
,
"integrity"
:
"sha1-8yXLBFVZJCi88Rs4M3DvcOO/zDg="
,
"optional"
:
true
,
"requires"
:
{
"bindings"
:
"^1.5.0"
,
"nan"
:
"^2.12.1"
}
}
}
}
},
},
"jest-jasmine2"
:
{
"jest-jasmine2"
:
{
...
@@ -6907,15 +6909,6 @@
...
@@ -6907,15 +6909,6 @@
"minimist"
:
"^1.2.5"
"minimist"
:
"^1.2.5"
}
}
},
},
"mockjs"
:
{
"version"
:
"1.1.0"
,
"resolved"
:
"https://registry.npm.taobao.org/mockjs/download/mockjs-1.1.0.tgz"
,
"integrity"
:
"sha1-5qDDeOkZBtuv8gkRzAJzs8fXWwY="
,
"dev"
:
true
,
"requires"
:
{
"commander"
:
"*"
}
},
"ms"
:
{
"ms"
:
{
"version"
:
"2.1.1"
,
"version"
:
"2.1.1"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/ms/-/ms-2.1.1.tgz"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/ms/-/ms-2.1.1.tgz"
,
...
@@ -7676,9 +7669,9 @@
...
@@ -7676,9 +7669,9 @@
"dev"
:
true
"dev"
:
true
},
},
"qs"
:
{
"qs"
:
{
"version"
:
"6.
5.2
"
,
"version"
:
"6.
9.4
"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/qs/-/qs-6.
5.2
.tgz"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/qs/-/qs-6.
9.4
.tgz"
,
"integrity"
:
"sha1-
yzroBuh0BERYTvFUzo7pjUA/PjY
="
,
"integrity"
:
"sha1-
kJCykNH5FyjTwi5UhDykSupatoc
="
,
"dev"
:
true
"dev"
:
true
},
},
"query-string"
:
{
"query-string"
:
{
...
@@ -8217,6 +8210,14 @@
...
@@ -8217,6 +8210,14 @@
"tough-cookie"
:
"~2.5.0"
,
"tough-cookie"
:
"~2.5.0"
,
"tunnel-agent"
:
"^0.6.0"
,
"tunnel-agent"
:
"^0.6.0"
,
"uuid"
:
"^3.3.2"
"uuid"
:
"^3.3.2"
},
"dependencies"
:
{
"qs"
:
{
"version"
:
"6.5.2"
,
"resolved"
:
"http://39.108.128.154:8089/repository/xbd-npm/qs/-/qs-6.5.2.tgz"
,
"integrity"
:
"sha1-yzroBuh0BERYTvFUzo7pjUA/PjY="
,
"dev"
:
true
}
}
}
},
},
"request-promise-core"
:
{
"request-promise-core"
:
{
...
...
package.json
View file @
276f0a7
...
@@ -7,7 +7,8 @@
...
@@ -7,7 +7,8 @@
"ios"
:
"react-native run-ios"
,
"ios"
:
"react-native run-ios"
,
"start"
:
"react-native start"
,
"start"
:
"react-native start"
,
"test"
:
"jest"
,
"test"
:
"jest"
,
"lint"
:
"eslint ."
"lint"
:
"eslint ."
,
"mock"
:
"npx nodemon mock/index.js"
},
},
"dependencies"
:
{
"dependencies"
:
{
"@react-native-community/masked-view"
:
"^0.1.10"
,
"@react-native-community/masked-view"
:
"^0.1.10"
,
...
@@ -35,7 +36,7 @@
...
@@ -35,7 +36,7 @@
"eslint"
:
"^6.5.1"
,
"eslint"
:
"^6.5.1"
,
"jest"
:
"^24.9.0"
,
"jest"
:
"^24.9.0"
,
"metro-react-native-babel-preset"
:
"^0.58.0"
,
"metro-react-native-babel-preset"
:
"^0.58.0"
,
"
mockjs"
:
"^1.1.0
"
,
"
qs"
:
"^6.9.4
"
,
"react-test-renderer"
:
"16.11.0"
"react-test-renderer"
:
"16.11.0"
},
},
"jest"
:
{
"jest"
:
{
...
...
src/config/app.js
View file @
276f0a7
export
const
APP
=
{
const
APP
=
{
/** @description token存储的名称 */
/** @description token存储的名称 */
tokenName
:
'jwt'
,
tokenName
:
'jwt'
,
...
@@ -10,8 +10,13 @@ export const APP = {
...
@@ -10,8 +10,13 @@ export const APP = {
/**@description API基础请求地址*/
/**@description API基础请求地址*/
baseUrl
:
{
baseUrl
:
{
dev
:
'http://gateway-dev.b2bwings.com/'
,
dev
:
{
pro
:
'https://gateway.b2bwings.com/'
prot
:
9000
,
host
:
'192.168.1.141'
,
// 替换为本地IP
origin
:
()
=>
`http://
${
APP
.
baseUrl
.
dev
.
host
}
:
${
APP
.
baseUrl
.
dev
.
prot
}
`
},
proxy
:
'http://gateway-dev.b2bwings.com'
,
pro
:
'https://gateway.b2bwings.com'
},
},
/**@description 应用的登陆API的PATH*/
/**@description 应用的登陆API的PATH*/
...
@@ -28,4 +33,6 @@ export const APP = {
...
@@ -28,4 +33,6 @@ export const APP = {
activeTintColor
:
'#6E4DB3'
,
activeTintColor
:
'#6E4DB3'
,
inactiveTintColor
:
'gray'
inactiveTintColor
:
'gray'
}
}
}
}
\ No newline at end of file
exports
.
APP
=
APP
\ No newline at end of file
src/libs/fetch.js
View file @
276f0a7
import
http
from
'./http-request'
import
http
from
'./http-request'
import
{
APP
}
from
'../config'
import
{
APP
}
from
'../config'
console
.
log
(
APP
.
baseUrl
.
dev
.
origin
())
const
FETCH
=
new
http
(
process
.
env
.
NODE_ENV
===
'development'
?
const
FETCH
=
new
http
(
process
.
env
.
NODE_ENV
===
'development'
?
APP
.
baseUrl
.
dev
:
APP
.
baseUrl
.
dev
.
origin
()
:
APP
.
baseUrl
.
pro
APP
.
baseUrl
.
pro
)
)
export
{
FETCH
}
export
{
FETCH
}
\ No newline at end of file
\ No newline at end of file
src/libs/http-request.js
View file @
276f0a7
...
@@ -23,7 +23,7 @@ class HttpRequest {
...
@@ -23,7 +23,7 @@ class HttpRequest {
instance
.
interceptors
.
request
.
use
(
config
=>
{
instance
.
interceptors
.
request
.
use
(
config
=>
{
return
config
return
config
},
error
=>
{
},
error
=>
{
if
(
process
.
env
.
NODE_ENV
===
'development'
)
console
.
error
(
error
)
if
(
process
.
env
.
NODE_ENV
===
'development'
)
console
.
warn
(
error
)
return
Promise
.
reject
(
error
)
return
Promise
.
reject
(
error
)
})
})
...
@@ -34,7 +34,7 @@ class HttpRequest {
...
@@ -34,7 +34,7 @@ class HttpRequest {
},
error
=>
{
},
error
=>
{
if
(
process
.
env
.
NODE_ENV
===
'development'
)
console
.
error
(
error
)
if
(
process
.
env
.
NODE_ENV
===
'development'
)
console
.
warn
(
error
)
return
Promise
.
reject
(
error
.
response
?
`发生错误,错误代码:
${
error
.
response
.
status
}
;详情:
${
error
.
response
.
message
}
`
:
error
.
message
+
""
)
return
Promise
.
reject
(
error
.
response
?
`发生错误,错误代码:
${
error
.
response
.
status
}
;详情:
${
error
.
response
.
message
}
`
:
error
.
message
+
""
)
})
})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment