'use strict';
require('./support');

const expect = require('expect.js'),
  nock = require('nock'),
  handleRequest = require('../../server/worker-helper');

describe('CloudFlare Worker', () => {
  afterEach(() => {
    nock.cleanAll();
  });

  it('should give a status of 502 for a non-existent page', () => {
    const req = new Request('http://jsonp.test/?url=http://localhost:8001');
    return handleRequest(req).catch(err => {
      expect(err.code).to.be('ECONNREFUSED');
    });
  });

  it('should pass the JSON and set the CORS headers', async () => {
    const destHost = 'http://localhost:8001';
    const json = { message: 'test' };
    nock(destHost)
      .get('/')
      .reply(200, json);

    const req = new Request(`http://jsonp.test/?url=${destHost}`);
    const res = await handleRequest(req);

    expect(res.status).to.be(200);
    expect(res.headers.get('access-control-allow-origin')).to.eql('*');
    const data = await res.json();
    expect(data).to.eql(json);
  });
});

@@ -247,19 +247,13 @@
 it('
-should 
 give
+s
  a s
@@ -492,19 +492,14 @@
 it('
-should 
 pass
+es
  the
@@ -942,16 +942,16 @@
 json();%0A
-
     expe
@@ -978,13 +978,410 @@
 );%0A  %7D);
+%0A%0A  it('includes the status and headers', async () =%3E %7B%0A    const destHost = 'http://localhost:8001';%0A    nock(destHost)%0A      .get('/')%0A      .reply(202, '', %7B%0A        'x-foo': 'bar'%0A      %7D);%0A%0A    const req = new Request(%60http://jsonp.test/?url=$%7BdestHost%7D%60);%0A    const res = await handleRequest(req);%0A%0A    expect(res.status).to.be(202);%0A    expect(res.headers.get('x-foo')).to.eql('bar');%0A  %7D);
 %0A%7D);%0A

add a test for the worker to include the headers