util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. /**
  18. * @fileoverview Various HTTP utilities.
  19. */
  20. 'use strict';
  21. const Executor = require('./index').Executor,
  22. HttpClient = require('./index').HttpClient,
  23. HttpRequest = require('./index').Request,
  24. Command = require('../lib/command').Command,
  25. CommandName = require('../lib/command').Name,
  26. error = require('../lib/error'),
  27. promise = require('../lib/promise');
  28. /**
  29. * Queries a WebDriver server for its current status.
  30. * @param {string} url Base URL of the server to query.
  31. * @return {!Promise<!Object>} A promise that resolves with
  32. * a hash of the server status.
  33. */
  34. function getStatus(url) {
  35. var client = new HttpClient(url);
  36. var executor = new Executor(client);
  37. var command = new Command(CommandName.GET_SERVER_STATUS);
  38. return executor.execute(command);
  39. }
  40. // PUBLIC API
  41. /**
  42. * Queries a WebDriver server for its current status.
  43. * @param {string} url Base URL of the server to query.
  44. * @return {!Promise<!Object>} A promise that resolves with
  45. * a hash of the server status.
  46. */
  47. exports.getStatus = getStatus;
  48. /**
  49. * Waits for a WebDriver server to be healthy and accepting requests.
  50. * @param {string} url Base URL of the server to query.
  51. * @param {number} timeout How long to wait for the server.
  52. * @param {Promise=} opt_cancelToken A promise used as a cancellation signal:
  53. * if resolved before the server is ready, the wait will be terminated
  54. * early with a {@link promise.CancellationError}.
  55. * @return {!Promise} A promise that will resolve when the server is ready, or
  56. * if the wait is cancelled.
  57. */
  58. exports.waitForServer = function(url, timeout, opt_cancelToken) {
  59. return new Promise((onResolve, onReject) => {
  60. let start = Date.now();
  61. let done = false;
  62. let resolve = (status) => {
  63. done = true;
  64. onResolve(status);
  65. };
  66. let reject = (err) => {
  67. done = true;
  68. onReject(err);
  69. };
  70. if (opt_cancelToken) {
  71. opt_cancelToken.then(_ => reject(new promise.CancellationError));
  72. }
  73. checkServerStatus();
  74. function checkServerStatus() {
  75. return getStatus(url).then(status => resolve(status), onError);
  76. }
  77. function onError(e) {
  78. // Some servers don't support the status command. If they are able to
  79. // response with an error, then can consider the server ready.
  80. if (e instanceof error.UnsupportedOperationError) {
  81. resolve({});
  82. return;
  83. }
  84. if (Date.now() - start > timeout) {
  85. reject(Error('Timed out waiting for the WebDriver server at ' + url));
  86. } else {
  87. setTimeout(function() {
  88. if (!done) {
  89. checkServerStatus();
  90. }
  91. }, 50);
  92. }
  93. }
  94. });
  95. };
  96. /**
  97. * Polls a URL with GET requests until it returns a 2xx response or the
  98. * timeout expires.
  99. * @param {string} url The URL to poll.
  100. * @param {number} timeout How long to wait, in milliseconds.
  101. * @param {Promise=} opt_cancelToken A promise used as a cancellation signal:
  102. * if resolved before the a 2xx response is received, the wait will be
  103. * terminated early with a {@link promise.CancellationError}.
  104. * @return {!Promise} A promise that will resolve when a 2xx is received from
  105. * the given URL, or if the wait is cancelled.
  106. */
  107. exports.waitForUrl = function(url, timeout, opt_cancelToken) {
  108. return new Promise((onResolve, onReject) => {
  109. let client = new HttpClient(url);
  110. let request = new HttpRequest('GET', '');
  111. let start = Date.now();
  112. let done = false;
  113. let resolve = () => {
  114. done = true;
  115. onResolve();
  116. };
  117. let reject = (err) => {
  118. done = true;
  119. onReject(err);
  120. };
  121. if (opt_cancelToken) {
  122. opt_cancelToken.then(_ => reject(new promise.CancellationError));
  123. }
  124. testUrl();
  125. function testUrl() {
  126. client.send(request).then(onResponse, onError);
  127. }
  128. function onError() {
  129. if (Date.now() - start > timeout) {
  130. reject(Error('Timed out waiting for the URL to return 2xx: ' + url));
  131. } else {
  132. setTimeout(function() {
  133. if (!done) {
  134. testUrl();
  135. }
  136. }, 50);
  137. }
  138. }
  139. function onResponse(response) {
  140. if (done) {
  141. return;
  142. }
  143. if (response.status > 199 && response.status < 300) {
  144. resolve();
  145. return;
  146. }
  147. onError();
  148. }
  149. });
  150. };