{"size": 6442, "ext": "js", "max_stars_repo_path": "node_modules/reload/test/api/https/https.js", "max_stars_repo_name": "nadavlotan6/HH-Agent", "max_stars_repo_stars_event_min_datetime": "2021-02-28T16:37:36.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-28T16:37:36.000Z", "max_issues_repo_path": "node_modules/reload/test/api/https/https.js", "max_issues_repo_name": "MSProgrammer/MERN-WebRTC", "max_issues_repo_issues_event_min_datetime": "2020-07-18T02:23:19.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T19:35:35.000Z", "max_forks_repo_path": "node_modules/reload/test/api/https/https.js", "max_forks_repo_name": "MSProgrammer/MERN-WebRTC", "max_forks_repo_forks_event_min_datetime": "2018-12-19T14:13:58.000Z", "max_forks_repo_forks_event_max_datetime": "2020-03-08T04:29:41.000Z"}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": 2, "avg_line_length": 32.3718592965, "max_line_length": 158, "alphanum_fraction": 0.6362930767}
/* global it, describe */ // npm standard ignore for describe and it

const fs = require('fs')
const path = require('path')
const assert = require('assert')
const sinon = require('sinon')
const express = require('express')

const reload = require('../../../index')

const helperFunction = require('../../helper')

const testKeyLocation = path.join(__dirname, '/test-only.key')
const testCrtLocation = path.join(__dirname, '/test-only.crt')
const testCaPemLocation = path.join(__dirname, '/test-only-ca.pem')

const testP12Location = path.join(__dirname, '/test-only.pfx')

const testKey = fs.readFileSync(testKeyLocation)
const testCrt = fs.readFileSync(testCrtLocation)
const testCaPem = fs.readFileSync(testCaPemLocation)

const testP12 = fs.readFileSync(testP12Location)

console.log(typeof testKey)

describe('HTTPS', function () {
  it('Should error if `certAndKey` and `p12` are not defined in HTTPS options', async () => {
    sinon.stub(console, 'log').returns(void 0)
    sinon.stub(console, 'error').returns(void 0)

    var app = express()

    try {
      await reload(app, { https: { } })
    } catch (err) {
      console.log.restore()
      console.error.restore()

      assert.strictEqual(err.message, 'Could not initialize reload HTTPS setup incorrectly. Make sure to define a `p12` or `certAndKey` in the HTTPS options')
    }
  })

  describe('HTTPS - Cert and Key', function () {
    it('Should create (default) `/reload/reload.js` route for reload file', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { certAndKey: { key: testKeyLocation, cert: testCrtLocation } } })
      } catch (err) {
        console.error('IN ERROR', err)
      }

      var response = await helperFunction.makeRequest('/reload/reload.js', app, { https: { key: testKey, cert: testCrt, ca: testCaPem } })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(response.statusCode, 200)
    })

    it('Should accept HTTPS key as file contents', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { certAndKey: { key: testKey, cert: testCrtLocation } } })
      } catch (err) {
        console.error('IN ERROR', err)
      }

      var response = await helperFunction.makeRequest('/reload/reload.js', app, { https: { key: testKey, cert: testCrt, ca: testCaPem } })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(response.statusCode, 200)
    })

    it('Should accept HTTPS cert as file contents', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { certAndKey: { key: testKey, cert: testCrt } } })
      } catch (err) {
        console.error('IN ERROR', err)
      }

      var response = await helperFunction.makeRequest('/reload/reload.js', app, { https: { key: testKey, cert: testCrt, ca: testCaPem } })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(response.statusCode, 200)
    })

    it('Should error with bad key file', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { certAndKey: { key: path.join('/somethingElse', testKeyLocation), cert: testCrtLocation } } })
      } catch (err) {
        return assert(true)
      }

      await helperFunction.closeReloadSocket(reloadReturned)
      return assert(false)
    })

    it('Should error with bad cert file', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { certAndKey: { key: testKeyLocation, cert: path.join('/somethingElse', testCrtLocation) } } })
      } catch (err) {
        return assert(true)
      }

      await helperFunction.closeReloadSocket(reloadReturned)
      return assert(false)
    })

    it('Should create secure WebSocket on default port', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { certAndKey: { key: testKeyLocation, cert: testCrtLocation } } })
      } catch (err) {
        assert(false, err)
      }

      var result = await helperFunction.testWebSocket(9856, true, { key: testKey, cert: testCrt, ca: testCaPem })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(result, true)
    })
  })

  describe('HTTPS - PFX', function () {
    it('Should create (default) `/reload/reload.js` route for reload file', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { p12: { p12Path: testP12Location, passphrase: '1234' } } })
      } catch (err) {
        assert(false, err)
      }

      var response = await helperFunction.makeRequest('/reload/reload.js', app, { https: { key: testKey, cert: testCrt, ca: testCaPem } })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(response.statusCode, 200)
    })

    it('Should accept pfx as file contents', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { p12: { p12Path: testP12, passphrase: '1234' } } })
      } catch (err) {
        assert(false, err)
      }

      var response = await helperFunction.makeRequest('/reload/reload.js', app, { https: { key: testKey, cert: testCrt, ca: testCaPem } })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(response.statusCode, 200)
    })

    it('Should error with bad pxf file', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { p12: { p12Path: path.join('/somethingElse', testP12Location), passphrase: '1234' } } })
      } catch (err) {
        return assert(true)
      }

      await helperFunction.closeReloadSocket(reloadReturned)
      return assert(false)
    })

    it('Should create secure WebSocket on default port', async () => {
      var app = express()

      try {
        var reloadReturned = await reload(app, { https: { p12: { p12Path: testP12Location, passphrase: '1234' } } })
      } catch (err) {
        assert(false, err)
      }

      var result = await helperFunction.testWebSocket(9856, true, { key: testKey, cert: testCrt, ca: testCaPem })

      await helperFunction.closeReloadSocket(reloadReturned)

      assert.strictEqual(result, true)
    })
  })
})
