util_test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 'use strict';
  18. const assert = require('assert');
  19. const http = require('http');
  20. const error = require('../../lib/error');
  21. const util = require('../../http/util');
  22. const promise = require('../../lib/promise');
  23. describe('selenium-webdriver/http/util', function() {
  24. var server, baseUrl;
  25. var status, value, responseCode;
  26. function startServer(done) {
  27. if (server) return done();
  28. server = http.createServer(function(req, res) {
  29. var data = JSON.stringify({status: status, value: value});
  30. res.writeHead(responseCode, {
  31. 'Content-Type': 'application/json; charset=utf-8',
  32. 'Content-Length': Buffer.byteLength(data, 'utf8')
  33. });
  34. res.end(data);
  35. });
  36. server.listen(0, '127.0.0.1', function(e) {
  37. if (e) return done(e);
  38. var addr = server.address();
  39. baseUrl = 'http://' + addr.address + ':' + addr.port;
  40. done();
  41. });
  42. }
  43. function killServer(done) {
  44. if (!server) return done();
  45. server.close(done);
  46. server = null;
  47. }
  48. after(killServer);
  49. beforeEach(function(done) {
  50. status = 0;
  51. value = 'abc123';
  52. responseCode = 200;
  53. startServer(done);
  54. });
  55. describe('#getStatus', function() {
  56. it('should return value field on success', function() {
  57. return util.getStatus(baseUrl).then(function(response) {
  58. assert.equal('abc123', response);
  59. });
  60. });
  61. it('should fail if response object is not success', function() {
  62. status = 1;
  63. return util.getStatus(baseUrl).then(function() {
  64. throw Error('expected a failure');
  65. }, function(err) {
  66. assert.ok(err instanceof error.WebDriverError);
  67. assert.equal(err.code, error.WebDriverError.code);
  68. assert.equal(err.message, value);
  69. });
  70. });
  71. it('should fail if the server is not listening', function(done) {
  72. killServer(function(e) {
  73. if(e) return done(e);
  74. util.getStatus(baseUrl).then(function() {
  75. done(Error('expected a failure'));
  76. }, function() {
  77. // Expected.
  78. done();
  79. });
  80. });
  81. });
  82. it('should fail if HTTP status is not 200', function() {
  83. status = 1;
  84. responseCode = 404;
  85. return util.getStatus(baseUrl).then(function() {
  86. throw Error('expected a failure');
  87. }, function(err) {
  88. assert.ok(err instanceof error.WebDriverError);
  89. assert.equal(err.code, error.WebDriverError.code);
  90. assert.equal(err.message, value);
  91. });
  92. });
  93. });
  94. describe('#waitForServer', function() {
  95. it('resolves when server is ready', function() {
  96. status = 1;
  97. setTimeout(function() { status = 0; }, 50);
  98. return util.waitForServer(baseUrl, 100);
  99. });
  100. it('should fail if server does not become ready', function() {
  101. status = 1;
  102. return util.waitForServer(baseUrl, 50).
  103. then(function() {throw Error('Expected to time out')},
  104. function() {});
  105. });
  106. it('can cancel wait', function() {
  107. status = 1;
  108. let cancel = new Promise(resolve => {
  109. setTimeout(_ => resolve(), 50)
  110. });
  111. return util.waitForServer(baseUrl, 200, cancel)
  112. .then(
  113. () => { throw Error('Did not expect to succeed!'); },
  114. (e) => assert.ok(e instanceof promise.CancellationError));
  115. });
  116. });
  117. describe('#waitForUrl', function() {
  118. it('succeeds when URL returns 2xx', function() {
  119. responseCode = 404;
  120. setTimeout(function() { responseCode = 200; }, 50);
  121. return util.waitForUrl(baseUrl, 200);
  122. });
  123. it('fails if URL always returns 4xx', function() {
  124. responseCode = 404;
  125. return util.waitForUrl(baseUrl, 50)
  126. .then(() => assert.fail('Expected to time out'),
  127. () => true);
  128. });
  129. it('fails if cannot connect to server', function() {
  130. return new Promise((resolve, reject) => {
  131. killServer(function(e) {
  132. if (e) return reject(e);
  133. util.waitForUrl(baseUrl, 50).
  134. then(function() { reject(Error('Expected to time out')); },
  135. function() { resolve(); });
  136. });
  137. });
  138. });
  139. it('can cancel wait', function() {
  140. responseCode = 404;
  141. let cancel = new Promise(resolve => {
  142. setTimeout(_ => resolve(), 50);
  143. });
  144. return util.waitForUrl(baseUrl, 200, cancel)
  145. .then(
  146. () => { throw Error('Did not expect to succeed!'); },
  147. (e) => assert.ok(e instanceof promise.CancellationError));
  148. });
  149. });
  150. });