text: /**
   * Copyright 2015 The AMP HTML Authors. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS-IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import {assertHttpsUrl, parseQueryString, parseUrl, removeFragment}
      from '../../src/url';
  
  describe('url', () => {
  
    var currentPort = location.port;
  
    function compareParse(url, result) {
      // Using JSON string comparison because Chai's deeply equal
      // errors are impossible to debug.
      var parsed = JSON.stringify(parseUrl(url));
      var expected = JSON.stringify(result);
      expect(parsed).to.equal(expected);
    }
  
    it('should parse correctly', () => {
      compareParse('https://foo.com/abc?123#foo', {
        href: 'https://foo.com/abc?123#foo',
        protocol: 'https:',
        host: 'foo.com',
        hostname: 'foo.com',
        port: '',
        pathname: '/abc',
        search: '?123',
        hash: '#foo',
        origin: 'https://foo.com'
      });
    });
    it('should handle ports', () => {
      compareParse('https://foo.com:123/abc?123#foo', {
        href: 'https://foo.com:123/abc?123#foo',
        protocol: 'https:',
        host: 'foo.com:123',
        hostname: 'foo.com',
        port: '123',
        pathname: '/abc',
        search: '?123',
        hash: '#foo',
        origin: 'https://foo.com:123'
      });
    });
    it('should support http', () => {
      compareParse('http://foo.com:123/abc?123#foo', {
        href: 'http://foo.com:123/abc?123#foo',
        protocol: 'http:',
        host: 'foo.com:123',
        hostname: 'foo.com',
        port: '123',
        pathname: '/abc',
        search: '?123',
        hash: '#foo',
        origin: 'http://foo.com:123'
      });
    });
    it('should resolve relative urls', () => {
      compareParse('./abc?123#foo', {
        href: 'http://localhost:' + currentPort + '/abc?123#foo',
        protocol: 'http:',
        host: 'localhost:' + currentPort,
        hostname: 'localhost',
        port: currentPort,
        pathname: '/abc',
        search: '?123',
        hash: '#foo',
        origin: 'http://localhost:' + currentPort
      });
    });
    it('should resolve path relative urls', () => {
      compareParse('/abc?123#foo', {
        href: 'http://localhost:' + currentPort + '/abc?123#foo',
        protocol: 'http:',
        host: 'localhost:' + currentPort,
        hostname: 'localhost',
        port: currentPort,
        pathname: '/abc',
        search: '?123',
        hash: '#foo',
        origin: 'http://localhost:' + currentPort
      });
    });
    it('should handle URLs with just the domain', () => {
      compareParse('http://foo.com:123', {
        href: 'http://foo.com:123/',
        protocol: 'http:',
        host: 'foo.com:123',
        hostname: 'foo.com',
        port: '123',
        pathname: '/',
        search: '',
        hash: '',
        origin: 'http://foo.com:123'
      });
    });
  
  });
  
  
  describe('parseQueryString', () => {
    it('should return empty params when query string is empty or null', () => {
      expect(parseQueryString(null)).to.deep.equal({});
      expect(parseQueryString('')).to.deep.equal({});
    });
    it('should parse single key-value', () => {
      expect(parseQueryString('a=1')).to.deep.equal({
        'a': '1'
      });
    });
    it('should parse two key-values', () => {
      expect(parseQueryString('a=1&b=2')).to.deep.equal({
        'a': '1',
        'b': '2'
      });
    });
    it('should ignore leading ?', () => {
      expect(parseQueryString('?a=1&b=2')).to.deep.equal({
        'a': '1',
        'b': '2'
      });
    });
    it('should ignore leading #', () => {
      expect(parseQueryString('#a=1&b=2')).to.deep.equal({
        'a': '1',
        'b': '2'
      });
    });
    it('should parse empty value', () => {
      expect(parseQueryString('a=&b=2')).to.deep.equal({
        'a': '',
        'b': '2'
      });
      expect(parseQueryString('a&b=2')).to.deep.equal({
        'a': '',
        'b': '2'
      });
    });
    it('should decode names and values', () => {
      expect(parseQueryString('a%26=1%26&b=2')).to.deep.equal({
        'a&': '1&',
        'b': '2'
      });
    });
    it('should return last dupe', () => {
      expect(parseQueryString('a=1&b=2&a=3')).to.deep.equal({
        'a': '3',
        'b': '2'
      });
    });
  });
  
  describe('assertHttpsUrl', () => {
    var referenceElement = document.createElement('div');
    it('should allow https', () => {
      assertHttpsUrl('https://twitter.com', referenceElement);
    });
    it('should allow protocol relative', () => {
      assertHttpsUrl('//twitter.com', referenceElement);
    });
    it('should allow localhost with http', () => {
      assertHttpsUrl('http://localhost:8000/sfasd', referenceElement);
    });
    it('should allow localhost with http suffix', () => {
      assertHttpsUrl('http://iframe.localhost:8000/sfasd', referenceElement);
    });
  
    it('should fail on http', () => {
      expect(() => {
        assertHttpsUrl('http://twitter.com', referenceElement);
      }).to.throw(/source must start with/);
    });
    it('should fail on http with localhost in the name', () => {
      expect(() => {
        assertHttpsUrl('http://foolocalhost', referenceElement);
      }).to.throw(/source must start with/);
    });
  });
  
  describe('removeFragment', () => {
    it('should remove fragment', () => {
      expect(removeFragment('https://twitter.com/path#abc')).to.equal(
          'https://twitter.com/path');
    });
    it('should remove empty fragment', () => {
      expect(removeFragment('https://twitter.com/path#')).to.equal(
          'https://twitter.com/path');
    });
    it('should ignore when no fragment', () => {
      expect(removeFragment('https://twitter.com/path')).to.equal(
          'https://twitter.com/path');
    });
  });
  