'buildbot-syncer.js' should be able to determine force build argument from a list...
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / resources / mock-remote-api.js
1 var assert = require('assert');
2
3 if (!assert.notReached)
4     assert.notReached = function () { assert(false, 'This code path should not be reached'); }
5
6 var MockRemoteAPI = {
7     url: function (path)
8     {
9         return `${this.urlPrefix}${path}`;
10     },
11     getJSON: function (url)
12     {
13         return this.getJSONWithStatus(url);
14     },
15     getJSONWithStatus: function (url)
16     {
17         return this._addRequest(url, 'GET', null);
18     },
19     postJSON: function (url, data)
20     {
21         return this._addRequest(url, 'POST', data);
22     },
23     postJSONWithStatus: function (url, data)
24     {
25         return this._addRequest(url, 'POST', data);
26     },
27     postFormUrlencodedData: function (url, data)
28     {
29         return this._addRequest(url, 'POST', data);
30     },
31     _addRequest: function (url, method, data)
32     {
33         var request = {
34             url: url,
35             method: method,
36             data: data,
37             promise: null,
38             resolve: null,
39             reject: null,
40         };
41
42         request.promise = new Promise(function (resolve, reject) {
43             request.resolve = resolve;
44             request.reject = reject;
45         });
46
47         if (this._waitingPromise) {
48             this._waitingPromiseResolver();
49             this._waitingPromise = null;
50             this._waitingPromiseResolver = null;
51         }
52
53         MockRemoteAPI.requests.push(request);
54         return request.promise;
55     },
56     waitForRequest()
57     {
58         if (!this._waitingPromise) {
59             this._waitingPromise = new Promise(function (resolve, reject) {
60                 MockRemoteAPI._waitingPromiseResolver = resolve;
61             });
62         }
63         return this._waitingPromise;
64     },
65     inject: function (urlPrefix)
66     {
67         var originalRemoteAPI = global.RemoteAPI;
68
69         beforeEach(function () {
70             MockRemoteAPI.reset(urlPrefix);
71             originalRemoteAPI = global.RemoteAPI;
72             global.RemoteAPI = MockRemoteAPI;
73         });
74
75         afterEach(function () {        
76             global.RemoteAPI = originalRemoteAPI;
77         });
78
79         return MockRemoteAPI.requests;
80     },
81     reset: function (urlPrefix)
82     {
83         if (urlPrefix)
84             MockRemoteAPI.urlPrefix = urlPrefix;
85         MockRemoteAPI.requests.length = 0;
86     },
87     requests: [],
88     _waitingPromise: null,
89     _waitingPromiseResolver: null,
90     urlPrefix: 'http://mockhost',
91 };
92
93 if (typeof module != 'undefined')
94     module.exports.MockRemoteAPI = MockRemoteAPI;