Example #1
0
  it('should define an interface', function() {
    var User = assert.define('MyUser', function(user) {
      assert(user).is(Object);
    });

    assert.type({}, User);
    expect(() => assert.type(12345, User))
      .toThrowError('Expected an instance of MyUser, got 12345!\n' +
                    '  - 12345 is not instance of Object');
  });
Example #2
0
    return new Promise(function(resolve, reject) {
      var request, promise;
      var {method, url, params, data, headers, responseType} = config;
      assert.type(method, assert.string);
      assert.type(url, assert.string);

      connection = new (config.ConnectionClass || XHRConnection)();

      request = {
        method: method,
        url: url,
        data: serialize(data),
        responseType: responseType || 'text',
        params: objectToMap(params),
        headers: objectToMap(headers)
      };

      function setHeaders() {
        for (var key of request.headers.keys()) {
          connection.setRequestHeader(key, request.headers.get(key));
        }
      }

      function openConnection() {
        connection.open(request.method, request.url);
        connection.send(request.data);
        return connection;
      }

      function onResponse (response) {
        return http.intercept({
          req: request,
          res: response,
          interceptType: 'response'
        });
      }

      function onResponseError (reason) {
        return http.intercept({
          err: reason,
          req: request,
          interceptType: 'response'
        });
      }

      http.intercept({req: request, interceptType: 'request'}).
        then(setHeaders).
        then(openConnection).
        then(onResponse, onResponseError).
        then(resolve, reject);
    });
Example #3
0
  it('should define assert for an existing type', function() {
    class Type {}

    assert.define(Type, function(value) {
      assert(value).is(Function, Object);
    });

    assert.type({}, Type);
    assert.type(function() {}, Type);
    expect(() => assert.type('str', Type))
      .toThrowError('Expected an instance of Type, got "str"!\n' +
                    '  - "str" is not instance of Function\n' +
                    '  - "str" is not instance of Object');
  });
Example #4
0
File: Http.js Project: btford/http
//TODO (@jeffbcross): support responseType in options
function request(method, url, options) {
  var queryParams, requestData, connection;
  assert.type(method, assert.string);
  assert.type(url, assert.string);

  queryParams = (options && options.params || {});
  requestData = (options && options.data);
  connection = new (options && options.ConnectionClass || XHRConnection)();
  url = fullUrl(url, queryParams);

  connection.open(method, url);
  connection.send(serialize(requestData));

  return connection;
}
Example #5
0
  it('should pass when returns true', function() {
    Type.assert = function(value) {
      return true;
    };

    assert.type({}, Type);
  });
Example #6
0
  describe('arrayOf', function() {

    var Titles = assert.define('ListOfTitles', function(value) {
      assert(value).is(assert.arrayOf(assert.string, assert.number));
    });

    it('should pass', function () {
      assert.type(['one', 55, 'two'], Titles);
    });


    it('should fail when non-array given', function () {
      expect(() => assert.type('foo', Titles))
        .toThrowError('Expected an instance of ListOfTitles, got "foo"!\n' +
                      '  - "foo" is not instance of array of string/number\n' +
                      '    - "foo" is not instance of Array');
    });


    it('should fail when an invalid item in the array', function () {
      expect(() => assert.type(['aaa', true], Titles))
        .toThrowError('Expected an instance of ListOfTitles, got ["aaa", true]!\n' +
                      '  - ["aaa", true] is not instance of array of string/number\n' +
                      '    - true is not instance of string\n' +
                      '    - true is not instance of number');
    });
  });
Example #7
0
 expect(function() {
   assert.type({
     req: sampleRequest,
     err: {},
     interceptType: 'response'
   }, IInterceptResolution);
 }).not.toThrow();
Example #8
0
 expect(function() {
   assert.type({
     req: sampleRequest,
     res: {},
     interceptType: 'response'
   }, IInterceptResolution);
 }).toThrowError(/Expected an instance of IResponse/);
Example #9
0
  describe('structure', function() {

    var User = assert.define('MyUser', function(value) {
      assert(value).is(assert.structure({
        name: assert.string,
        age: assert.number
      }));
    });

    it('should pass', function () {
      assert.type({name: 'Vojta', age: 28}, User);
    });


    it('should fail when non-object given', function () {
      expect(() => assert.type(123, User))
        .toThrowError('Expected an instance of MyUser, got 123!\n' +
                      '  - 123 is not instance of object with properties name, age\n' +
                      '    - 123 is not instance of Object');
    });


    it('should fail when an invalid property', function () {
      expect(() => assert.type({name: 'Vojta', age: true}, User))
        .toThrowError('Expected an instance of MyUser, got {name: "Vojta", age: true}!\n' +
                      '  - {name: "Vojta", age: true} is not instance of object with properties name, age\n' +
                      '    - true is not instance of number');
    });
  });
Example #10
0
export function metadataPassCheck(
  contents,
  filename,
  { addonMetadata = null } = {}
) {
  if (!addonMetadata || typeof addonMetadata.guid === 'undefined') {
    assert.fail(null, null, 'Add-on metadata not found');
  }

  return [];
}
Example #11
0
File: Http.js Project: a8m/http
    return new Promise(function(resolve, reject) {
      var request, promise;
      var {method, url, params, data, headers, responseType} = config;
      assert.type(method, assert.string);
      assert.type(url, assert.string);

      connection = new (config.ConnectionClass || XHRConnection)();

      request = {
        method: method,
        url: url,
        data: serialize(data),
        responseType: responseType || 'text',
        params: objectToMap(params),
        headers: objectToMap(headers)
      };

      function onResponse (response) {
        return http.intercept({
          req: request,
          res: response,
          interceptType: 'response'
        });
      }

      function onResponseError (reason) {
        return http.intercept({
          err: reason,
          req: request,
          interceptType: 'response'
        });
      }

      http.intercept({req: request, interceptType: 'request'}).
        then(() => ({request: request, connection: connection})).
        then(http$$openConnection).
        then(http$$setRequestHeaders).
        then(http$$sendRequest).
        then(onResponse, onResponseError).
        then(resolve, reject);
    });
Example #12
0
/**
 * Serialize the parameters into a queryString, including the leading "?" or
 * "&". If isAppended is truthy, the queryString should begin with "&".
 */
function toQueryString (params, isAppended) {
  var queryString, orderedKeys, key, encodedKey, value;
  if (typeof isAppended !== 'undefined') {
    assert.type(isAppended, assert.boolean);
  }
  if (Object.keys(params).length === 0) return '';

  queryString = isAppended ? '&' : '?';
  orderedKeys = Object.keys(params).sort();

  while (key = orderedKeys.shift()) {
    encodedKey = encodeValue(key);
    queryString += encodedKey;
    queryString += '=';
    value = params[key];


    queryString += encodeValue(value, encodedKey);
    queryString += orderedKeys.length ? '&' : '';
  }

  return queryString;
}
Example #13
0
 it('should return an object containing a respond function', function() {
   var connection = new ConnectionMock();
   assert.type(
       ConnectionMockBackend.whenRequest('GET', '/users').respond,
       Function);
 });
Example #14
0
 it('should implement IConnection', function() {
   assert.type(ConnectionMock, IConnection);
 });
Example #15
0
 it('should create a new deferred', function() {
   assert.type(new ConnectionMock().deferred, Deferred);
 });
Example #16
0
 it('should return a new zone from PromiseBackend', function() {
   var forked = ConnectionMockBackend.forkZone();
   assert.type(forked, Zone);
   expect(forked.onZoneEnter.toString()).toContain('patchWithMock()');
 });
Example #17
0
 it('should add the request signature to a class-level map', function() {
   ConnectionMockBackend.whenRequest('GET', '/users');
   var responseMap = ConnectionMockBackend.requestResponseMap.get('GET').
       get('/users');
   assert.type(responseMap, ResponseMap);
 });
Example #18
0
 it('should pass', function () {
   assert.type({name: 'Vojta', age: 28}, User);
 });
Example #19
0
 expect(function() {
   assert.type(12345, Type);
 }).toThrowError('Expected an instance of Type, got 12345!\n' +
Example #20
0
 expect(() => assert.type('foo', Titles))
Example #21
0
 Type.assert = function(value) {
   assert.fail('not smart enough');
   assert.fail('not blue enough');
 };
Example #22
0
 expect(() => assert.type(123, Type))
Example #23
0
 it('should allow null', function() {
   assert.type(null, Type);
 });
Example #24
0
 expect(() => assert.type({name: 'Vojta', age: true}, User))
Example #25
0
 expect(() => assert.type(['aaa', true], Titles))
Example #26
0
 expect(() => assert.type(123, User))
Example #27
0
 function($http) {
   assert.type($http.ConnectionClass, IConnection);
 }));
Example #28
0
 expect(() => assert.type({}, Type))
Example #29
0
 it('should pass', function() {
   assert.type(new Type(), Type);
 });
Example #30
0
 var User = assert.define('MyUser', function(value) {
   assert(value).is(assert.structure({
     name: assert.string,
     age: assert.number
   }));
 });