diff --git a/package.json b/package.json index 6d591bb..7005105 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,8 @@ "koa-router": "^7.1.1", "koa-send": "^4.0.0", "koa-static": "^4.0.2", - "lodash": "^4.17.5", "md5": "^2.2.1", + "lodash": "^4.17.11", "mockjs": "^1.0.1-beta3", "moment": "^2.17.1", "mysql": "^2.11.1", @@ -60,7 +60,7 @@ "devDependencies": { "@types/chai": "^4.0.10", "@types/kcors": "^2.2.2", - "@types/koa": "2.0.43", + "@types/koa": "^2.0.43", "@types/koa-generic-session": "^1.0.2", "@types/koa-logger": "^3.1.0", "@types/koa-redis": "^3.0.2", @@ -83,7 +83,6 @@ "nodemon": "^1.11.0", "npm-run-all": "^4.0.2", "nyc": "^12.0.2", - "pm2": "^2.10.2", "pre-commit": "^1.2.2", "rimraf": "^2.6.2", "source-map-support": "^0.5.0", diff --git a/src/routes/mock.ts b/src/routes/mock.ts index 4a7e56e..e101329 100644 --- a/src/routes/mock.ts +++ b/src/routes/mock.ts @@ -5,6 +5,7 @@ import Tree from './utils/tree' import urlUtils from './utils/url' import * as querystring from 'querystring' import { Sequelize } from 'sequelize-typescript'; +import * as urlPkg from 'url' const attributes: any = { exclude: [] } const pt = require('node-print').pt @@ -137,9 +138,9 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => { let repository = await Repository.findById(repositoryId) let collaborators: Repository[] = (await repository.$get('collaborators')) as Repository[] - let itf + let itf: Interface - const matchedItfList = await Interface.findAll({ + let matchedItfList = await Interface.findAll({ attributes, where: { repositoryId: [repositoryId, ...collaborators.map(item => item.id)], @@ -150,16 +151,61 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => { } }) - if (matchedItfList) { - for (const item of matchedItfList) { - itf = item - let url = item.url - if (url.charAt(0) === '/') { - url = url.substring(1) + function getRelativeURLWithoutParams(url: string) { + if (url.indexOf('http://') > -1) { + url = url.substring('http://'.length) + } + if (url.indexOf('https://') > -1) { + url = url.substring('https://'.length) + } + if (url.indexOf('/') > -1) { + url = url.substring(url.indexOf('/') + 1) + } + if (url.indexOf('?') > -1) { + url = url.substring(0, url.indexOf('?')) + } + return url + } + + // matching by path + if (matchedItfList.length > 1) { + matchedItfList = matchedItfList.filter(x => { + const urlDoc = getRelativeURLWithoutParams(x.url) + const urlRequest = urlWithoutPrefixSlash + return urlDoc === urlRequest + }) + } + + // matching by params + if (matchedItfList.length > 1) { + matchedItfList = matchedItfList.filter(x => { + const params = { + ... ctx.request.query, + ...ctx.request.body, } - if (url === urlWithoutPrefixSlash) { - break + const parsedUrl = urlPkg.parse(x.url) + const pairs = parsedUrl.query.split('&').map(x => x.split('=')) + for (const p of pairs) { + const key = p[0] + const val = p[1] + if (params[key] == val) { + return true + } } + // for (let key in query) { + // } + return false + }) + } + + for (const item of matchedItfList) { + itf = item + let url = item.url + if (url.charAt(0) === '/') { + url = url.substring(1) + } + if (url === urlWithoutPrefixSlash) { + break } }