Skip to content

Commit c7abe07

Browse files
author
zhangfx
committed
return status 405 when method error of mock request #22
1 parent eedc535 commit c7abe07

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

server/app/controller/client.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const sleep = ms => cb => setTimeout(cb, ms)
66
const BASE_TYPES = [ 'string', 'number', 'boolean', 'object', 'array' ]
77

88
class ClientController extends AbstractController {
9-
async findApi (method) {
9+
async findApi () {
1010
const id = this.ctx.params[0]
1111
if (id.length < 5) {
1212
// hack方法,兼容老的存下url信息的api
1313
const url = `/client/${id}`
14-
return this.ctx.model.Api.findOne({ url, 'options.method': method }).exec()
14+
return this.ctx.model.Api.findOne({ url }).exec()
1515
}
16-
return this.ctx.model.Api.findOne({ _id: id, 'options.method': method }).exec()
16+
return this.ctx.model.Api.findOne({ _id: id }).exec()
1717
}
1818
async real () {
1919
const { _apiRealUrl, _apiMethod } = this.ctx.request.body
@@ -79,10 +79,14 @@ class ClientController extends AbstractController {
7979
}
8080
return false
8181
}
82-
async handleRequest (api) {
82+
async handleRequest (method, api) {
8383
if (!api) {
8484
return
8585
}
86+
if (api.options.method !== method) {
87+
this.ctx.status = 405
88+
return
89+
}
8690
if (await this.handleProxy(api)) {
8791
return
8892
}
@@ -103,28 +107,28 @@ class ClientController extends AbstractController {
103107
}
104108
// get/:id
105109
async show () {
106-
const document = await this.findApi('get')
107-
await this.handleRequest(document)
110+
const document = await this.findApi()
111+
await this.handleRequest('get', document)
108112
}
109113
// post /
110114
async create () {
111-
const document = await this.findApi('post')
112-
await this.handleRequest(document)
115+
const document = await this.findApi()
116+
await this.handleRequest('post', document)
113117
}
114118
// put
115119
async put () {
116-
const document = await this.findApi('put')
117-
await this.handleRequest(document)
120+
const document = await this.findApi()
121+
await this.handleRequest('put', document)
118122
}
119123
// patch
120124
async patch () {
121-
const document = await this.findApi('patch')
122-
await this.handleRequest(document)
125+
const document = await this.findApi()
126+
await this.handleRequest('patch', document)
123127
}
124128
// delete
125129
async delete () {
126-
const document = await this.findApi('delete')
127-
await this.handleRequest(document)
130+
const document = await this.findApi()
131+
await this.handleRequest('delete', document)
128132
}
129133
getPathParams (api) { // 获取RESTful风格Url参数
130134
const pathParams = {}

0 commit comments

Comments
 (0)