aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/test_ARGV.rb
AgeCommit message (Expand)Author
2014-08-29Add a method for retrieving only flags from ARGVJack Nagel
2014-08-28Implement ARGV.named in terms of ARGV.options_onlyJack Nagel
2014-06-23Fix directory leak in test_argvJack Nagel
2014-06-20Remove ARGV.filter_for_dependenciesJack Nagel
2014-06-18Use a custom test class so we can avoid monkeypatchingJack Nagel
2014-06-11Use assert_emptyJack Nagel
2014-03-13Make debug an installer modeJack Nagel
2013-05-20Add test for ARGV.flag?Jack Nagel
2013-04-07Isolate ARGV extension testsJack Nagel
2013-01-22Add tests for ARGV.filter_for_dependenciesJack Nagel
2012-07-04tests: clean up whitespaceJack Nagel
2012-07-04Prune some requires from test filesJack Nagel
2012-04-16tests: setup and teardown cleanupsJack Nagel
2012-03-06Proper single character switch handlingMax Howell
2012-02-04Adjust ARGV testsJack Nagel
2010-04-06Move ARGV tests to separate file.Adam Vandenberg
131' href='#n131'>131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
describe('$httpBackend', function() {

  var $backend, $browser, callbacks,
      xhr, fakeBody, callback;

  // TODO(vojta): should be replaced by $defer mock
  function fakeTimeout(fn, delay) {
    fakeTimeout.fns.push(fn);
    fakeTimeout.delays.push(delay);
  }

  fakeTimeout.fns = [];
  fakeTimeout.delays = [];
  fakeTimeout.flush = function() {
    var len = fakeTimeout.fns.length;
    fakeTimeout.delays = [];
    while (len--) fakeTimeout.fns.shift()();
  };


  beforeEach(inject(function($injector) {
    callbacks = {};
    $browser = $injector.get('$browser');
    fakeBody = {removeChild: jasmine.createSpy('body.removeChild')};
    $backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeBody);
    callback = jasmine.createSpy('done');
  }));


  it('should do basics - open async xhr and send data', function() {
    $backend('GET', '/some-url', 'some-data', noop);
    xhr = MockXhr.$$lastInstance;

    expect(xhr.$$method).toBe('GET');
    expect(xhr.$$url).toBe('/some-url');
    expect(xhr.$$data).toBe('some-data');
    expect(xhr.$$async).toBe(true);
  });


  it('should normalize IE\'s 1223 status code into 204', function() {
    callback.andCallFake(function(status) {
      expect(status).toBe(204);
    });

    $backend('GET', 'URL', null, callback);
    xhr = MockXhr.$$lastInstance;

    xhr.status = 1223;
    xhr.readyState = 4;
    xhr.onreadystatechange();

    expect(callback).toHaveBeenCalledOnce();
  });


  it('should set only the requested headers', function() {
    $backend('POST', 'URL', null, noop, {'X-header1': 'value1', 'X-header2': 'value2'});
    xhr = MockXhr.$$lastInstance;

    expect(xhr.$$headers).toEqual({
      'X-header1': 'value1',
      'X-header2': 'value2'
    });
  });


  it('should return raw xhr object', function() {
    expect($backend('GET', '/url', null, noop)).toBe(MockXhr.$$lastInstance);
  });


  it('should abort request on timeout', function() {
    callback.andCallFake(function(status, response) {
      expect(status).toBe(-1);
    });

    $backend('GET', '/url', null, callback, {}, 2000);
    xhr = MockXhr.$$lastInstance;
    spyOn(xhr, 'abort');

    expect(fakeTimeout.delays[0]).toBe(2000);

    fakeTimeout.flush();
    expect(xhr.abort).toHaveBeenCalledOnce();

    xhr.status = 0;
    xhr.readyState = 4;
    xhr.onreadystatechange();
    expect(callback).toHaveBeenCalledOnce();
  });


  it('should be async even if xhr.send() is sync', function() {
    // IE6, IE7 is sync when serving from cache
    function SyncXhr() {
      xhr = this;
      this.open = this.setRequestHeader = noop;
      this.send = function() {
        this.status = 200;
        this.responseText = 'response';
        this.readyState = 4;
      };
    }

    callback.andCallFake(function(status, response) {
      expect(status).toBe(200);
      expect(response).toBe('response');
    });

    $backend = createHttpBackend($browser, SyncXhr, fakeTimeout);
    $backend('GET', '/url', null, callback);
    expect(callback).not.toHaveBeenCalled();

    fakeTimeout.flush();
    expect(callback).toHaveBeenCalledOnce();

    (xhr.onreadystatechange || noop)();
    expect(callback).toHaveBeenCalledOnce();
  });


  describe('JSONP', function() {

    it('should add script tag for JSONP request', function() {
      callback.andCallFake(function(status, response) {
        expect(status).toBe(200);
        expect(response).toBe('some-data');
      });

      $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
      expect($browser.$$scripts.length).toBe(1);

      var script = $browser.$$scripts.shift(),
          url = script.url.split('?cb=');

      expect(url[0]).toBe('http://example.org/path');
      callbacks[url[1]]('some-data');
      script.done();

      expect(callback).toHaveBeenCalledOnce();
    });


    it('should clean up the callback and remove the script', function() {
      $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
      expect($browser.$$scripts.length).toBe(1);

      var script = $browser.$$scripts.shift(),
          callbackId = script.url.split('?cb=')[1];

      callbacks[callbackId]('some-data');
      script.done();

      expect(callbacks[callbackId]).toBeUndefined();
      expect(fakeBody.removeChild).toHaveBeenCalledOnce();
      expect(fakeBody.removeChild).toHaveBeenCalledWith(script);
    });


    it('should call callback with status -2 when script fails to load', function() {
      callback.andCallFake(function(status, response) {
        expect(status).toBe(-2);
        expect(response).toBeUndefined();
      });

      $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
      expect($browser.$$scripts.length).toBe(1);

      $browser.$$scripts.shift().done();
      expect(callback).toHaveBeenCalledOnce();
    });


    // TODO(vojta): test whether it fires "async-start"
    // TODO(vojta): test whether it fires "async-end" on both success and error
  });

  describe('file protocol', function() {

    function respond(status, content) {
      xhr = MockXhr.$$lastInstance;
      xhr.status = status;
      xhr.responseText = content;
      xhr.readyState = 4;
      xhr.onreadystatechange();
    }


    it('should convert 0 to 200 if content', function() {
      $backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');

      $backend('GET', 'file:///whatever/index.html', null, callback);
      respond(0, 'SOME CONTENT');

      expect(callback).toHaveBeenCalled();
      expect(callback.mostRecentCall.args[0]).toBe(200);
    });


    it('should convert 0 to 200 if content - relative url', function() {
      $backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');

      $backend('GET', '/whatever/index.html', null, callback);
      respond(0, 'SOME CONTENT');

      expect(callback).toHaveBeenCalled();
      expect(callback.mostRecentCall.args[0]).toBe(200);
    });


    it('should convert 0 to 404 if no content', function() {
      $backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');

      $backend('GET', 'file:///whatever/index.html', null, callback);
      respond(0, '');

      expect(callback).toHaveBeenCalled();
      expect(callback.mostRecentCall.args[0]).toBe(404);
    });


    it('should convert 0 to 200 if content - relative url', function() {
      $backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');

      $backend('GET', '/whatever/index.html', null, callback);
      respond(0, '');

      expect(callback).toHaveBeenCalled();
      expect(callback.mostRecentCall.args[0]).toBe(404);
    });
  });
});