<commit_before>var rewire = require("rewire");
var Endpoint = rewire("../lib/endpoint");

var http2 = require("http2");
var sinon = require("sinon");
var stream = require("stream");
var EventEmitter = require("events");

describe("Endpoint", () => {
  describe("module references", () => {
    describe("h2Endpoint", () => {
      it("is the constructor for http2.protocol.Endpoint", () => {
        var h2Endpoint = Endpoint.__get__("h2Endpoint");

        expect(h2Endpoint).to.equal(http2.protocol.Endpoint);
      });
    });

    describe("tls", () => {
      it("is the tls module", () => {
        var tls = Endpoint.__get__("tls");

        expect(tls).to.equal(require('tls'));
      });
    });
  });

  describe("connect", () => {
    var tls = Endpoint.__get__("tls");

    beforeEach(() => {
      sinon.stub(tls, "connect");
      tls.connect.returns(new EventEmitter());
    });

    afterEach(() => {
      tls.connect.restore();
    });

    it("creates a TLS socket", () => {
      var endpoint = new Endpoint({});

      expect(tls.connect).to.be.calledOnce;
    });

    it("retains the socket as an ivar", () => {
      var endpoint = new Endpoint({});

      expect(endpoint._socket).to.equal(tls.connect.firstCall.returnValue);
    });

    it("passes the connection parameters", () => {
      var endpoint = new Endpoint({address: "localtest", port: 443});

      var connectParameters = tls.connect.firstCall.args[0];

      expect(connectParameters).to.have.property("host", "localtest")
      expect(connectParameters).to.have.property("port", 443);
    });

    it("passes the credential parameters", () => {
      var endpoint = new Endpoint({
        address: "localtest", port: 443,
        pfx: "pfxData", cert: "certData",
        key: "keyData", passphrase: "<PASSWORD>"
      });

      var connectParameters = tls.connect.firstCall.args[0];

      expect(connectParameters).to.have.property("pfx", "pfxData");
      expect(connectParameters).to.have.property("cert", "certData");
      expect(connectParameters).to.have.property("key", "keyData");
      expect(connectParameters).to.have.property("passphrase", "<PASSWORD>");
    });

    describe("secureConnect event", () => {
      var endpoint;
      beforeEach(() => {
        endpoint = new Endpoint({});
        sinon.stub(endpoint, "_connected");

        var socket = tls.connect.firstCall.returnValue;

        socket.emit("secureConnect");
      });

      it("calls Endpoint#connected", () => {
        expect(endpoint._connected).to.be.calledOnce;
      });

      it("uses correct `this` value for Endpoint#connected", () => {
        expect(endpoint._connected.firstCall.thisValue).to.equal(endpoint);
      });
    });
  });

  describe("connected", () => {
    var endpoint;
    var h2Endpoint, h2EndpointSpy;

    before(() => {
      h2EndpointSpy = sinon.spy(stream.PassThrough);
      h2Endpoint = Endpoint.__set__("h2Endpoint", h2EndpointSpy);
    });

    after(() => {
      h2Endpoint()
    });

    beforeEach(() => {
      sinon.stub(Endpoint.prototype, "_connect")
      endpoint = new Endpoint({});
      endpoint._socket = new stream.PassThrough();
    });

    afterEach(() => {
      Endpoint.prototype._connect.restore();
      h2EndpointSpy.reset()
    });

    it("creates an h2Endpoint", () => {
      endpoint._connected();

      expect(h2EndpointSpy).to.have.been.calledWithNew;
    });

    it("passes the correct parameters when creating the h2Endpoint", () => {
      endpoint._connected();

      // Empty bunyan logger
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("fatal");
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("error");
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("warn");
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("info");
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("debug");
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("trace");
      expect(h2EndpointSpy.firstCall.args[0]).to.have.property("child");

      expect(h2EndpointSpy.firstCall.args[1]).to.equal("CLIENT");
    });

    it("retains the h2Endpoint as an ivar", () => {<commit_msg>Test the fake logger behaves correctly<commit_after>var rewire = require("rewire");
var Endpoint = rewire("../lib/endpoint");

var http2 = require("http2");
var sinon = require("sinon");
var stream = require("stream");
var EventEmitter = require("events");

describe("Endpoint", () => {
  describe("module references", () => {
    describe("h2Endpoint", () => {
      it("is the constructor for http2.protocol.Endpoint", () => {
        var h2Endpoint = Endpoint.__get__("h2Endpoint");

        expect(h2Endpoint).to.equal(http2.protocol.Endpoint);
      });
    });

    describe("tls", () => {
      it("is the tls module", () => {
        var tls = Endpoint.__get__("tls");

        expect(tls).to.equal(require('tls'));
      });
    });
  });

  describe("connect", () => {
    var tls = Endpoint.__get__("tls");

    beforeEach(() => {
      sinon.stub(tls, "connect");
      tls.connect.returns(new EventEmitter());
    });

    afterEach(() => {
      tls.connect.restore();
    });

    it("creates a TLS socket", () => {
      var endpoint = new Endpoint({});

      expect(tls.connect).to.be.calledOnce;
    });

    it("retains the socket as an ivar", () => {
      var endpoint = new Endpoint({});

      expect(endpoint._socket).to.equal(tls.connect.firstCall.returnValue);
    });

    it("passes the connection parameters", () => {
      var endpoint = new Endpoint({address: "localtest", port: 443});

      var connectParameters = tls.connect.firstCall.args[0];

      expect(connectParameters).to.have.property("host", "localtest")
      expect(connectParameters).to.have.property("port", 443);
    });

    it("passes the credential parameters", () => {
      var endpoint = new Endpoint({
        address: "localtest", port: 443,
        pfx: "pfxData", cert: "certData",
        key: "keyData", passphrase: "<PASSWORD>"
      });

      var connectParameters = tls.connect.firstCall.args[0];

      expect(connectParameters).to.have.property("pfx", "pfxData");
      expect(connectParameters).to.have.property("cert", "certData");
      expect(connectParameters).to.have.property("key", "keyData");
      expect(connectParameters).to.have.property("passphrase", "<PASSWORD>");
    });

    describe("secureConnect event", () => {
      var endpoint;
      beforeEach(() => {
        endpoint = new Endpoint({});
        sinon.stub(endpoint, "_connected");

        var socket = tls.connect.firstCall.returnValue;

        socket.emit("secureConnect");
      });

      it("calls Endpoint#connected", () => {
        expect(endpoint._connected).to.be.calledOnce;
      });

      it("uses correct `this` value for Endpoint#connected", () => {
        expect(endpoint._connected.firstCall.thisValue).to.equal(endpoint);
      });
    });
  });

  describe("connected", () => {
    var endpoint;
    var h2Endpoint, h2EndpointSpy;

    before(() => {
      h2EndpointSpy = sinon.spy(stream.PassThrough);
      h2Endpoint = Endpoint.__set__("h2Endpoint", h2EndpointSpy);
    });

    after(() => {
      h2Endpoint()
    });

    beforeEach(() => {
      sinon.stub(Endpoint.prototype, "_connect")
      endpoint = new Endpoint({});
      endpoint._socket = new stream.PassThrough();
    });

    afterEach(() => {
      Endpoint.prototype._connect.restore();
      h2EndpointSpy.reset()
    });

    it("creates an h2Endpoint", () => {
      endpoint._connected();

      expect(h2EndpointSpy).to.have.been.calledWithNew;
    });

    it("passes the correct parameters when creating the h2Endpoint", () => {
      endpoint._connected();

      // Empty bunyan logger
      var logger = h2EndpointSpy.firstCall.args[0];
      expect(logger).to.have.property("fatal");
      expect(logger).to.have.property("error");
      expect(logger).to.have.property("warn");
      expect(logger).to.have.property("info");
      expect(logger).to.have.property("debug");
      expect(logger).to.have.property("trace");
      expect(logger).to.have.property("child");
      expect(logger.child()).to.equal(logger);

      expect(h2EndpointSpy.firstCall.args[1]).to.equal("CLIENT");
    });

    it("retains the h2Endpoint as an ivar", () => {
{'fraction_non_alphanumeric': 0.1800311638499341, 'fraction_numerical': 0.00982859882536258, 'mean_word_length': 3.07421875, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1152796', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2484, 'n_words': 411}