wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
export class BaseConfig {
  set(options, notUndefined) {
    options = this.initOptions(options)
    for (const name in options) {
      if (Object.prototype.hasOwnProperty.call(options, name)) {
        if (!(this[name] instanceof Array)) {
          if (notUndefined === true) {
            this[name] = options[name] === undefined ? this[name] : options[name]
          } else {
            this[name] = options[name]
          }
        }
      }
    }
  }
 
  sets(types, options) {
    options = this.initOptions(options)
    if (types) {
      for (const name in types) {
        if (
          Object.prototype.hasOwnProperty.call(types, name) &&
          Object.prototype.hasOwnProperty.call(options, name)
        ) {
          options[name].forEach(o => {
            this[name].push(new types[name](o))
          })
        }
      }
    }
  }
 
  initOptions(options) {
    return options || {}
  }
 
  isValid() {
    return true
  }
}
 
export class KeyValue extends BaseConfig {
  constructor(options) {
    options = options || {}
    options.enable = options.enable === undefined ? true : options.enable
 
    super()
    this.name = undefined
    this.value = undefined
    this.type = undefined
    this.files = undefined
    this.enable = undefined
    this.uuid = undefined
    this.time = undefined
    this.contentType = undefined
    this.set(options)
  }
 
  isValid() {
    return (!!this.name || !!this.value) && this.type !== 'file'
  }
 
  isFile() {
    return (!!this.name || !!this.value) && this.type === 'file'
  }
}
 
export class Body extends BaseConfig {
  constructor(options) {
    super()
    this.type = 'KeyValue'
    this.raw = undefined
    this.kvs = []
    this.binary = []
    this.set(options)
    this.sets({ kvs: KeyValue }, { binary: KeyValue }, options)
  }
 
  isValid() {
    if (this.isKV()) {
      return this.kvs.some(kv => {
        return kv.isValid()
      })
    } else {
      return !!this.raw
    }
  }
 
  isKV() {
    return [BODY_TYPE.FORM_DATA, BODY_TYPE.WWW_FORM, BODY_TYPE.BINARY].indexOf(this.type) > 0
  }
}
 
export const BODY_TYPE = {
  KV: 'KeyValue',
  FORM_DATA: 'Form_Data',
  RAW: 'Raw',
  WWW_FORM: 'WWW_FORM',
  XML: 'XML',
  JSON: 'JSON'
}
 
export class Scenario extends BaseConfig {
  constructor(options = {}) {
    super()
    this.id = undefined
    this.name = undefined
    this.url = undefined
    this.variables = []
    this.headers = []
    this.requests = []
    this.environmentId = undefined
    this.dubboConfig = undefined
    this.environment = undefined
    this.enableCookieShare = false
    this.enable = true
    this.databaseConfigs = []
    this.tcpConfig = undefined
    this.set(options)
    this.sets(
      {
        variables: KeyValue,
        headers: KeyValue
      },
      options
    )
  }
 
  initOptions(options = {}) {
    options.databaseConfigs = options.databaseConfigs || []
    return options
  }
 
  clone() {
    const clone = new Scenario(this)
    return clone
  }
 
  isValid() {
    if (this.enable) {
      for (let i = 0; i < this.requests.length; i++) {
        const validator = this.requests[i].isValid(this.environmentId, this.environment)
        if (!validator.isValid) {
          return validator
        }
      }
    }
    return { isValid: true }
  }
 
  isReference() {
    return this.id.indexOf('#') !== -1
  }
}