all files / test/unit/ response.es6

100% Statements 0/0
100% Branches 0/0
100% Functions 22/22
100% Lines 0/0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 235 236 237 238 239 240 241 242 243 244 245                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import Response from '../../lib/response';
import Headers from '../../lib/headers';
import { default as chai, expect } from 'chai';
import { default as chaiAsPromised } from 'chai-as-promised';
 
chai.use(chaiAsPromised);
 
describe('Response', () => {
 
  describe('constructor', () => {
 
    it('should set this.ok to true if init.status is between 199 and 300', () => {
 
      expect(new Response('', {
        status: 200,
      }))
        .to.have.property('ok', true);
      expect(new Response('', {
        status: 300,
      }))
        .to.have.property('ok', false);
 
    });
 
    it('should set this.headers to an instance of Headers if it not one already', () => {
 
      const testHeadersInstance = new Headers({
        Cookie: 'test',
      });
 
      expect(new Response())
        .to.have.property('headers')
        .that.is.an.instanceof(Headers);
      expect(new Response('', {
        headers: testHeadersInstance
      }))
        .to.have.property('headers')
        .that.is.an.instanceof(Headers);
 
    });
 
    it('should set this.headerList to an this.headers.map if this.headers is an instance of Headers', () => {
 
      expect(new Response())
        .to.have.property('headerList')
        .that.eqls({});
 
    });
 
    it('should set this.url to init.url if init.url is a string', () => {
 
      const testUrl = 'http://example.com';
 
      expect(new Response('', {
        url: testUrl,
      }))
        .to.have.property('url')
        .that.eqls(testUrl);
 
    });
 
    it('should set this.url to the last url in init.urlList if init.urlList is an array', () => {
 
      const testUrlList = [
        'http://example.com',
        'http://example.com?redirect=true',
      ];
 
      expect(new Response('', {
        url: 'http://google.com',
        urlList: testUrlList,
      }))
        .to.have.property('url')
        .that.eqls(testUrlList[1]);
 
    });
 
    it('should decode urls', () => {
 
      const testUrl = 'http://example.com%3Fredirect%3Dtrue%23title';
      const testUrlList = [
        testUrl,
      ];
      const expectedUrl = 'http://example.com?redirect=true#title';
 
      expect(new Response('', {
        urlList: testUrlList,
      }))
        .to.have.property('url')
        .that.eqls(expectedUrl);
      expect(new Response('', {
        url: testUrl,
      }))
        .to.have.property('url')
        .that.eqls(expectedUrl);
 
    });
 
    it('should throw an error if init is defined but not an object', () => {
 
      expect(() => new Response('', true))
        .to.throw(Error, 'Response init must be an object');
 
    });
 
    it('should throw an error if init.status is not a number or undefined', () => {
 
      expect(() => new Response('', {
        status: true,
      }))
        .to.throw(Error, 'Response init.status must be a number');
      expect(() => new Response('', {
        status: 400,
      }))
        .to.not.throw();
 
    });
 
    it('should throw an error if init.type is not a string or undefined', () => {
 
      expect(() => new Response('', {
        type: true,
      }))
        .to.throw(Error, 'Response init.type must be a string');
      expect(() => new Response('', {
        type: 'default',
      }))
        .to.not.throw();
 
    });
 
    it('should throw an error if init.statusText is not a string or undefined', () => {
 
      expect(() => new Response('', {
        statusText: true,
      }))
        .to.throw(Error, 'Response init.statusText must be a string');
      expect(() => new Response('', {
        statusText: 'OK',
      }))
        .to.not.throw();
 
    });
 
    it('should throw an error if init.urlList is not an array or undefined', () => {
 
      expect(() => new Response('', {
        urlList: true,
      }))
        .to.throw(Error, 'Response init.urlList must be an array');
      expect(() => new Response('', {
        urlList: [
          'http://example.com'
        ],
      }))
        .to.not.throw();
 
    });
 
    it('should throw an error if init.terminationReason is not a string or undefined', () => {
 
      expect(() => new Response('', {
        terminationReason: true,
      }))
        .to.throw(Error, 'Response init.terminationReason must be a string');
      expect(() => new Response('', {
        terminationReason: 'timeout',
      }))
        .to.not.throw();
 
    });
 
    it('should throw an error if init.cacheState is not a string or undefined', () => {
 
      expect(() => new Response('', {
        cacheState: true,
      }))
        .to.throw(Error, 'Response init.cacheState must be a string');
      expect(() => new Response('', {
        cacheState: 'local',
      }))
        .to.not.throw();
 
    });
 
    it('should throw an error if init.httpsState is not a string or undefined', () => {
 
      expect(() => new Response('', {
        httpsState: true,
      }))
        .to.throw(Error, 'Response init.httpsState must be a string');
      expect(() => new Response('', {
        httpsState: 'local',
      }))
        .to.not.throw();
 
    });
 
  });
 
  describe('clone', () => {
 
    it('should return a new instance of Response with the same init', () => {
 
      const testResponse = new Response('', {
        status: 201,
        statusText: 'Created',
      });
 
      expect(testResponse.clone())
        .to.be.an.instanceOf(Response)
        .that.has.property('status', 201);
 
    });
 
  });
 
  describe('error', () => {
 
    it('should return a new Response representing an error', () => {
 
      const testResponse = new Response();
 
      expect(testResponse.error())
        .to.be.an.instanceOf(Response)
        .that.has.property('type', 'error');
 
    });
 
  });
 
  describe('redirect', () => {
 
    it('should throw an error', () => {
 
      const testResponse = new Response();
 
      expect(() => testResponse.redirect())
        .to.throw(Error, /Method has not been implemented/)
 
    });
 
  });
 
});