REGRESSION(r230960): Browser tests under TimeSeriesChart fetchMeasurementSets all...
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / resources / mock-remote-api.js
1
2 var MockRemoteAPI = {
3     url: function (path)
4     {
5         return `${this.urlPrefix}${path}`;
6     },
7     getJSON: function (url)
8     {
9         return this.getJSONWithStatus(url);
10     },
11     getJSONWithStatus: function (url)
12     {
13         return this._addRequest(url, 'GET', null);
14     },
15     postJSON: function (url, data)
16     {
17         return this._addRequest(url, 'POST', data);
18     },
19     postJSONWithStatus: function (url, data)
20     {
21         return this._addRequest(url, 'POST', data);
22     },
23     postFormUrlencodedData: function (url, data)
24     {
25         return this._addRequest(url, 'POST', data);
26     },
27     _addRequest: function (url, method, data)
28     {
29         var request = {
30             url: url,
31             method: method,
32             data: data,
33             promise: null,
34             resolve: null,
35             reject: null,
36         };
37
38         request.promise = new Promise(function (resolve, reject) {
39             request.resolve = resolve;
40             request.reject = reject;
41         });
42
43         if (this._waitingPromise) {
44             this._waitingPromiseResolver();
45             this._waitingPromise = null;
46             this._waitingPromiseResolver = null;
47         }
48
49         MockRemoteAPI.requests.push(request);
50         return request.promise;
51     },
52     waitForRequest()
53     {
54         if (!this._waitingPromise) {
55             this._waitingPromise = new Promise(function (resolve, reject) {
56                 MockRemoteAPI._waitingPromiseResolver = resolve;
57             });
58         }
59         return this._waitingPromise;
60     },
61     inject: function (urlPrefix, privilegedAPI)
62     {
63         let originalRemoteAPI;
64         let originalPrivilegedAPI;
65
66         beforeEach(() => {
67             MockRemoteAPI.reset(urlPrefix);
68             originalRemoteAPI = global.RemoteAPI;
69             global.RemoteAPI = MockRemoteAPI;
70             originalPrivilegedAPI = global.PrivilegedAPI;
71             global.PrivilegedAPI = privilegedAPI;
72             if (privilegedAPI._token)
73                 privilegedAPI._token = null;
74         });
75
76         afterEach(() => {
77             global.RemoteAPI = originalRemoteAPI;
78             global.PrivilegedAPI = originalPrivilegedAPI;
79             if (privilegedAPI._token)
80                 privilegedAPI._token = null;
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;