1
|
|
|
var Factory = require('./Factory').Factory |
2
|
|
|
|
3
|
|
|
var HttpRequestOptions = function () { |
4
|
|
|
this.headers = [] |
5
|
|
|
this.method = 'GET' |
6
|
|
|
this.params = {} |
7
|
|
|
this.postData = '' |
8
|
|
|
this.rawOutput = false |
9
|
|
|
this.timeout = 90 |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
Object.defineProperty(HttpRequestOptions, 'name', { |
13
|
|
|
value: 'Net.HttpRequestOptions', |
14
|
|
|
writable: false |
15
|
|
|
}) |
16
|
|
|
|
17
|
|
|
var HttpRequestResult = function () { |
18
|
|
|
this.code = 200 |
19
|
|
|
this.data = undefined |
20
|
|
|
this.error = '' |
21
|
|
|
this.headers = [] |
22
|
|
|
this.raw_headers = '' |
23
|
|
|
this.text = '' |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
Object.defineProperty(HttpRequestResult, 'name', { |
27
|
|
|
value: 'Net.HttpRequestResult', |
28
|
|
|
writable: false |
29
|
|
|
}) |
30
|
|
|
|
31
|
|
|
var definition = { |
32
|
|
|
defaults: { |
33
|
|
|
http: { |
34
|
|
|
responses: [], |
35
|
|
|
roundRobin: false |
36
|
|
|
} |
37
|
|
|
}, |
38
|
|
|
state: { |
39
|
|
|
http: {}, |
40
|
|
|
log: { |
41
|
|
|
http: [] |
42
|
|
|
} |
43
|
|
|
}, |
44
|
|
|
properties: { |
45
|
|
|
HttpRequestOptions: HttpRequestOptions, |
46
|
|
|
HttpRequestResult: HttpRequestResult |
47
|
|
|
}, |
48
|
|
|
handlers: { |
49
|
|
|
httpRequest: function (self, url, callback, options) { |
50
|
|
|
if (!self._state.http.responses) { |
51
|
|
|
self._state.http.responses = self._settings.http.responses.slice() |
52
|
|
|
} |
53
|
|
|
var responses = self._state.http.responses |
54
|
|
|
var index = -1 |
55
|
|
|
for (var i = 0; i < responses.length; i++) { |
56
|
|
|
var cursor = responses[i] |
57
|
|
|
if (!cursor.matcher || cursor.matcher(url, options)) { |
58
|
|
|
index = i |
59
|
|
|
break |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
if (index === -1) { |
63
|
|
|
var message = 'Failed to find matching response for url ' + url + |
64
|
|
|
' and options ' + JSON.stringify(options) |
65
|
|
|
throw new Error(message) |
66
|
|
|
} |
67
|
|
|
var response = responses.splice(index, 1)[0] |
68
|
|
|
if (self._settings.http.roundRobin) { |
69
|
|
|
responses.push(response) |
70
|
|
|
} |
71
|
|
|
var structure = new HttpRequestResult() |
72
|
|
|
var properties = ['code', 'headers', 'data', 'raw_headers', 'text'] |
73
|
|
|
properties.forEach(function (property) { |
74
|
|
|
if (response.hasOwnProperty(property)) { |
75
|
|
|
structure[property] = response[property] |
76
|
|
|
} |
77
|
|
|
}) |
78
|
|
|
if (response.delay) { |
79
|
|
|
response.delay().then(callback.bind(null, structure)) |
80
|
|
|
} else { |
81
|
|
|
callback(structure) |
82
|
|
|
} |
83
|
|
|
}, |
84
|
|
|
httpRequestAsync: function (self, url, options) { |
85
|
|
|
return new Promise(function (resolve) { |
86
|
|
|
definition.handlers.httpRequest(self, url, resolve, options) |
87
|
|
|
}) |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
module.exports = { |
93
|
|
|
Net: Factory.create('Net', definition) |
94
|
|
|
} |
95
|
|
|
|