/*global beforeEach describe it*/
import { expect } from "chai";
import sinon from "sinon";

import streamResponse from "../../../src/lib/server/streamResponse";

describe("test/lib/server/streamResponse", () => {
  const mockZlib = {
    createDeflate: sinon.stub().returns("deflate gate!"),
    createGzip: sinon.stub().returns("gzip!")
  };

  const mockResponseStream = {
    pipe: sinon.stub()
  };
  mockResponseStream.pipe.returns(mockResponseStream);

  const mockCachedResponse = {
    status: 200,
    responseStream: mockResponseStream
  };

  const mockResponse = {
    writeHead: sinon.spy()
  };

  beforeEach(() => {
    mockZlib.createDeflate.reset();
    mockZlib.createGzip.reset();
    mockResponseStream.pipe.reset();
    mockResponse.writeHead.reset();
  });

  it("should deflate deflate supported", () => {
    const mockRequest = {
      headers: {
        "accept-encoding": "gzip, deflate"
      }
    };

    streamResponse(mockRequest, mockResponse, mockCachedResponse, mockZlib);

    expect(mockZlib.createDeflate.calledOnce).to.equal(true);
    expect(mockZlib.createGzip.callCount).to.equal(0);
    expect(mockResponseStream.pipe.calledWith("deflate gate!")).to.equal(true);

    const lastCallArgs = mockResponse.writeHead.lastCall.args;
    expect(lastCallArgs[0]).to.equal(200);
    expect(lastCallArgs[1]).to.deep.equal({"Content-Encoding": "deflate"});
    expect(mockResponseStream.pipe.calledWith(mockResponse)).to.equal(true);
  });

  it("should gzip if gzip supported and deflate is not", () => {
    const mockRequest = {
      headers: {
        "accept-encoding": "gzip"
      }
    };

    streamResponse(mockRequest, mockResponse, mockCachedResponse, mockZlib);

    expect(mockZlib.createGzip.calledOnce).to.equal(true);
    expect(mockZlib.createDeflate.callCount).to.equal(0);
    expect(mockResponseStream.pipe.calledWith("gzip!")).to.equal(true);

    const lastCallArgs = mockResponse.writeHead.lastCall.args;
    expect(lastCallArgs[0]).to.equal(200);
    expect(lastCallArgs[1]).to.deep.equal({"Content-Encoding": "gzip"});
    expect(mockResponseStream.pipe.calledWith(mockResponse)).to.equal(true);
  });

  it("should not deflate or gzip if neither are supported", () => {
    const mockRequest = {
      headers: {}
    };

    streamResponse(mockRequest, mockResponse, mockCachedResponse, mockZlib);

    expect(mockZlib.createDeflate.callCount).to.equal(0);
    expect(mockZlib.createGzip.callCount).to.equal(0);
    expect(mockResponseStream.pipe.calledWith("gzip!")).to.equal(false);
    expect(mockResponseStream.pipe.calledWith("deflate gate!")).to.equal(false);

    const lastCallArgs = mockResponse.writeHead.lastCall.args;
    expect(lastCallArgs[0]).to.equal(200);
    expect(lastCallArgs[1]).to.deep.equal({});
    expect(mockResponseStream.pipe.calledWith(mockResponse)).to.equal(true);
  });
});


@@ -604,16 +604,87 @@
 )%0A  %7D;%0A%0A
+  const other = %7B%0A    %22Content-Type%22: %22text/html; charset=utf-8%22%0A  %7D;%0A%0A
   before
@@ -692,24 +692,24 @@
 ach(() =%3E %7B%0A
-
     mockZlib
@@ -1450,16 +1450,26 @@
 deflate%22
+, ...other
 %7D);%0A    
@@ -2149,16 +2149,26 @@
 : %22gzip%22
+, ...other
 %7D);%0A    
@@ -2811,32 +2811,32 @@
 .to.equal(200);%0A
-
     expect(lastC
@@ -2862,16 +2862,24 @@
 .equal(%7B
+...other
 %7D);%0A    

fix tests