const WebSocketClient = require('websocket').client
const request = require('supertest')
const assert = require('assert')
const uuidv4 = require('uuid/v4')
const settings = require('../../../settings')[process.env.NODE_ENV || 'production']

const mlkccaEndpoint = settings.websocketEndpoint

function Connect (uuid) {
  const dspath = 'accesstoken/websocket/' + uuid + '/connect'
  const grantURL = '/api/grant/' + settings.appId + '/' + settings.apiKey
  let agent = request.agent(settings.endpoint)
  var accessToken = ''

  describe('client.connect(pushEndpoint)', function () {
    this.timeout(10000)
    before(function (done) {
      agent
      .get(grantURL)
      .end(function (__err, __res) {
        if (__err) return done(__err)
        accessToken = JSON.parse(__res.text).content.access_token
        done()
      })
    })
    // it('should issue error if password is incorrect', function (done) {
    //   const client = new WebSocketClient()
    //   client.on('connectFailed', function (error) {
    //     throw error
    //   })
    //   client.on('connect', function (connection) {
    //     connection.on('error', function (error) {
    //       assert.equal(error.code, 'ECONNRESET')
    //       connection.close()
    //       client.abort()
    //       done()
    //     })
    //   })
    //   client.connect(mlkccaEndpoint + '/ws/push/' + settings.appId + '/wrongApiKey?c=' + dspath)
    // })
    it('should connect when user/pass are correct', function (done) {
      const client = new WebSocketClient()
      client.on('connectFailed', function (error) {
        throw error
      })
      client.on('connect', function (connection) {
        connection.on('error', function (error) {
          throw error
        })
        if (connection.connected) {
          assert.ok(true)
          connection.close()
          client.abort()
          done()
        }
      })
      client.connect(mlkccaEndpoint + '/ws/push/' + settings.appId + '?c=' + dspath + '&at=' + encodeURIComponent(accessToken)) // '+' is convert to 'space' in URL
    })
  })
}

Connect(global.uuid || uuidv4())

module.exports = Connect

@@ -836,19 +836,16 @@
   %7D)%0A   
- //
  it('sho
@@ -904,27 +904,24 @@
 (done) %7B%0A   
- //
    const cli
@@ -947,27 +947,24 @@
 Client()%0A   
- //
    client.on
@@ -999,27 +999,24 @@
 error) %7B%0A   
- //
      throw e
@@ -1019,27 +1019,24 @@
 ow error%0A   
- //
    %7D)%0A    //
@@ -1024,35 +1024,32 @@
 ror%0A      %7D)%0A   
- //
    client.on('co
@@ -1079,27 +1079,24 @@
 ction) %7B%0A   
- //
      connect
@@ -1134,25 +1134,22 @@
 ) %7B%0A    
-//
       
- 
 assert.e
@@ -1178,27 +1178,24 @@
 NRESET')%0A   
- //
        conne
@@ -1207,27 +1207,24 @@
 .close()%0A   
- //
        clien
@@ -1232,27 +1232,24 @@
 .abort()%0A   
- //
        done(
@@ -1253,19 +1253,16 @@
 ne()%0A   
- //
      %7D)%0A
@@ -1264,19 +1264,16 @@
   %7D)%0A   
- //
    %7D)%0A  
@@ -1273,19 +1273,16 @@
   %7D)%0A   
- //
    clien
@@ -1344,20 +1344,8 @@
  + '
-/wrongApiKey
 ?c='
@@ -1357,16 +1357,27 @@
 path
+ + '&at=wrong'
 )%0A   
- //
  %7D)%0A

Add websocket accesstoken error test