Skip to content

Commit

Permalink
Merge pull request #1543 from AlexStacker/add-hook-request
Browse files Browse the repository at this point in the history
feat: add hook request
  • Loading branch information
hellosean1025 committed Dec 22, 2019
2 parents af3bbd1 + 1ffb7a9 commit 607925c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
21 changes: 20 additions & 1 deletion client/components/Postman/Postman.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const {
checkNameIsExistInArray
} = require('common/postmanLib.js');

const plugin = require('client/plugin.js');

const createContext = require('common/createContext')

const HTTP_METHOD = constants.HTTP_METHOD;
Expand Down Expand Up @@ -332,20 +334,37 @@ export default class Run extends Component {
let options = handleParams(this.state, this.handleValue),
result;


await plugin.emitHook('before_request', options, {
type: this.props.type,
caseId: options.caseId,
projectId: this.props.projectId,
interfaceId: this.props.interfaceId
});

try {
options.taskId = this.props.curUid;
result = await crossRequest(options, this.state.pre_script, this.state.after_script, createContext(
result = await crossRequest(options, options.pre_script || this.state.pre_script, options.after_script || this.state.after_script, createContext(
this.props.curUid,
this.props.projectId,
this.props.interfaceId
));

await plugin.emitHook('after_request', result, {
type: this.props.type,
caseId: options.caseId,
projectId: this.props.projectId,
interfaceId: this.props.interfaceId
});

result = {
header: result.res.header,
body: result.res.body,
status: result.res.status,
statusText: result.res.statusText,
runTime: result.runTime
};

} catch (data) {
result = {
header: data.header,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { initCrossRequest } from 'client/components/Postman/CheckCrossInstall.js
import produce from 'immer';
import {InsertCodeMap} from 'client/components/Postman/Postman.js'

const plugin = require('client/plugin.js');
const {
handleParams,
crossRequest,
Expand Down Expand Up @@ -311,6 +312,13 @@ class InterfaceColContent extends Component {
validRes: []
};

await plugin.emitHook('before_col_request', Object.assign({}, options, {
type: 'col',
caseId: options.caseId,
projectId: interfaceData.project_id,
interfaceId: interfaceData.interface_id
}));

try {
let data = await crossRequest(options, interfaceData.pre_script, interfaceData.after_script, createContext(
this.props.curUid,
Expand All @@ -328,6 +336,13 @@ class InterfaceColContent extends Component {
statusText: data.res.statusText
};

await plugin.emitHook('after_col_request', result, {
type: 'col',
caseId: options.caseId,
projectId: interfaceData.project_id,
interfaceId: interfaceData.interface_id
});

if (options.data && typeof options.data === 'object') {
requestParams = {
...requestParams,
Expand Down
46 changes: 46 additions & 0 deletions client/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,52 @@ hooks = {
mulit: true,
listener: []
},
/**
* 在运行页面或单个测试也里每次发送请求前调用
* 可以用插件针对某个接口的请求头或者数据进行修改或者记录
*/
before_request: {
type: 'listener',
mulit: true,
listener: []
},
/**
* 在运行页面或单个测试也里每次发送完成后调用
* 返回值为响应原始值 +
* {
* type: 'inter' | 'case',
* projectId: string,
* interfaceId: string
* }
*/
after_request: {
type: 'listener',
mulit: true,
listener: []
},
/**
* 在测试集里运行每次发送请求前调用
*/
before_col_request: {
type: 'listener',
mulit: true,
listener: []
},
/**
* 在测试集里运行每次发送请求后调用
* 返回值为响应原始值 +
* {
* type: 'col',
* caseId: string,
* projectId: string,
* interfaceId: string
* }
*/
after_col_request: {
type: 'listener',
mulit: true,
listener: []
},
/**
* header下拉菜单 menu 钩子
* @param HeaderMenu
Expand Down

0 comments on commit 607925c

Please sign in to comment.