Skip to content

Commit

Permalink
Fixed unauthorized test
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed May 7, 2024
1 parent af1bbc0 commit 97fbb89
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions test/e2e/unauthorized.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<script src="/.tmp/geckos.io-client.latest.min.js"></script>
Expand All @@ -7,7 +7,7 @@
<script>
'use strict'

var channel = geckos({ port: 4000, authorization: 'some-unique-token' })
var channel = geckos({ port: 4001, authorization: 'some-unique-token' })

channel.onConnect(error => {
if (error) {
Expand Down
38 changes: 19 additions & 19 deletions test/e2e/unauthorized.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import http from 'http'
import path from 'path'

import { __dirname } from './_dirname.js'
import { sleep, serverListenPromise, kill } from '../helpers.js'

const app = express()

const server = http.createServer(app)
const sockets = new Set()
server.on('connection', socket => {
sockets.add(socket)
server.once('close', () => {
sockets.delete(socket)
})
})

let theToken
const io = geckos({
authorization: async token => {
Expand All @@ -19,21 +29,23 @@ const io = geckos({
app.use('/', Static(path.join(__dirname, '../')))

io.addServer(server)
server.listen(4000)
await serverListenPromise(server, 4001)

io.onConnection(ch => {})

describe('connection', () => {
test('should have no connection', done => {
test('should have no connection', async () => {
let connections = 0

io.onConnection(ch => {
connections++
})

setTimeout(() => {
expect(connections).toBe(0)
done()
}, 1000)
await page.goto('http://localhost:4001/e2e/unauthorized.html', { timeout: 2000 })

await sleep(2000)

expect(connections).toBe(0)
})
})

Expand All @@ -43,18 +55,6 @@ describe('unauthorized', () => {
})
})

page.goto('http://localhost:4000/e2e/unauthorized.html')

afterAll(async () => {
const close = () => {
return new Promise(resolve => {
server.close(() => {
resolve()
})
})
}

await close()
// await page.close()
// await browser.close()
await kill(server, sockets)
})
11 changes: 11 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ export const kill = async (httpServer, sockets = new Set()) => {
})
})
}

export const serverListenPromise = (httpServer, port) => {
return new Promise(resolve => {
httpServer.listen(port, () => {
console.log('listening on port', port)
resolve()
})
})
}

export const sleep = (ms = 1000) => new Promise(r => setTimeout(r, ms))

0 comments on commit 97fbb89

Please sign in to comment.