Extend create-analysis-test API to be able to create with confirming test group.
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / resources / mock-remote-api.js
1 const BrowserPrivilegedAPI = require('../../public/v3/privileged-api.js').PrivilegedAPI;
2 const NodePrivilegedAPI = require('../../tools/js/privileged-api').PrivilegedAPI;
3
4 var MockRemoteAPI = {
5     url: function (path)
6     {
7         return `${this.urlPrefix}${path}`;
8     },
9     getJSON: function (url)
10     {
11         return this.getJSONWithStatus(url);
12     },
13     getJSONWithStatus: function (url)
14     {
15         return this._addRequest(url, 'GET', null);
16     },
17     postJSON: function (url, data)
18     {
19         return this._addRequest(url, 'POST', data);
20     },
21     postJSONWithStatus: function (url, data)
22     {
23         return this._addRequest(url, 'POST', data);
24     },
25     postFormUrlencodedData: function (url, data)
26     {
27         return this._addRequest(url, 'POST', data);
28     },
29     _addRequest: function (url, method, data)
30     {
31         var request = {
32             url: url,
33             method: method,
34             data: data,
35             promise: null,
36             resolve: null,
37             reject: null,
38         };
39
40         request.promise = new Promise(function (resolve, reject) {
41             request.resolve = resolve;
42             request.reject = reject;
43         });
44
45         if (this._waitingPromise) {
46             this._waitingPromiseResolver();
47             this._waitingPromise = null;
48             this._waitingPromiseResolver = null;
49         }
50
51         MockRemoteAPI.requests.push(request);
52         return request.promise;
53     },
54     waitForRequest()
55     {
56         if (!this._waitingPromise) {
57             this._waitingPromise = new Promise(function (resolve, reject) {
58                 MockRemoteAPI._waitingPromiseResolver = resolve;
59             });
60         }
61         return this._waitingPromise;
62     },
63     inject: function (urlPrefix, privilegedAPIType='browser')
64     {
65         console.assert(privilegedAPIType === 'browser' || privilegedAPIType === 'node');
66         let originalRemoteAPI = global.RemoteAPI;
67         let originalPrivilegedAPI = global.PrivilegedAPI;
68         const useNodePrivilegedAPI = privilegedAPIType === 'node';
69         const PrivilegedAPI = useNodePrivilegedAPI ? NodePrivilegedAPI: BrowserPrivilegedAPI;
70
71         beforeEach(() => {
72             MockRemoteAPI.reset(urlPrefix);
73             originalRemoteAPI = global.RemoteAPI;
74             global.RemoteAPI = MockRemoteAPI;
75             originalPrivilegedAPI = global.PrivilegedAPI;
76             global.PrivilegedAPI = PrivilegedAPI;
77             if (!useNodePrivilegedAPI)
78                 PrivilegedAPI._token = null;
79         });
80
81         afterEach(() => {
82             global.RemoteAPI = originalRemoteAPI;
83             global.PrivilegedAPI = originalPrivilegedAPI;
84         });
85
86         return MockRemoteAPI.requests;
87     },
88     reset: function (urlPrefix)
89     {
90         if (urlPrefix)
91             MockRemoteAPI.urlPrefix = urlPrefix;
92         MockRemoteAPI.requests.length = 0;
93     },
94     requests: [],
95     _waitingPromise: null,
96     _waitingPromiseResolver: null,
97     urlPrefix: 'http://mockhost',
98 };
99
100 if (typeof module != 'undefined')
101     module.exports.MockRemoteAPI = MockRemoteAPI;