dc12782f939597b1d41e594c706e083ab6a94515
[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 PrivilegedAPI = privilegedAPIType === 'node' ? NodePrivilegedAPI: BrowserPrivilegedAPI;
69
70         beforeEach(() => {
71             MockRemoteAPI.reset(urlPrefix);
72             originalRemoteAPI = global.RemoteAPI;
73             global.RemoteAPI = MockRemoteAPI;
74             originalPrivilegedAPI = global.PrivilegedAPI;
75             global.PrivilegedAPI = PrivilegedAPI;
76         });
77
78         afterEach(() => {
79             global.RemoteAPI = originalRemoteAPI;
80             global.PrivilegedAPI = originalPrivilegedAPI;
81         });
82
83         return MockRemoteAPI.requests;
84     },
85     reset: function (urlPrefix)
86     {
87         if (urlPrefix)
88             MockRemoteAPI.urlPrefix = urlPrefix;
89         MockRemoteAPI.requests.length = 0;
90     },
91     requests: [],
92     _waitingPromise: null,
93     _waitingPromiseResolver: null,
94     urlPrefix: 'http://mockhost',
95 };
96
97 if (typeof module != 'undefined')
98     module.exports.MockRemoteAPI = MockRemoteAPI;